blob: ec8016f5ec4dc01d5d2b292bf305a4e903b81b0d [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);
Tobin Ehlis7ee9cbd2017-04-26 16:51:48 -0600476 // For a Depth/Stencil image both aspects MUST be set
477 if (FormatIsDepthAndStencil(image_create_info->format)) {
478 auto const aspect_mask = img_barrier->subresourceRange.aspectMask;
479 auto const ds_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
480 if ((aspect_mask & ds_mask) != (ds_mask)) {
481 skip |=
482 log_msg(core_validation::GetReportData(device_data), VK_DEBUG_REPORT_ERROR_BIT_EXT,
483 VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, reinterpret_cast<const uint64_t &>(img_barrier->image), __LINE__,
484 VALIDATION_ERROR_00302, "DS",
485 "%s: Image barrier 0x%p references image 0x%" PRIx64
486 " of format %s that must have the depth and stencil aspects set, but its "
487 "aspectMask is 0x%" PRIx32 ". %s",
488 func_name, img_barrier, reinterpret_cast<const uint64_t &>(img_barrier->image),
489 string_VkFormat(image_create_info->format), aspect_mask, validation_error_map[VALIDATION_ERROR_00302]);
490 }
491 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600492 uint32_t level_count = ResolveRemainingLevels(&img_barrier->subresourceRange, image_create_info->mipLevels);
493 uint32_t layer_count = ResolveRemainingLayers(&img_barrier->subresourceRange, image_create_info->arrayLayers);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700494
Mike Weiblen62d08a32017-03-07 22:18:27 -0700495 for (uint32_t j = 0; j < level_count; j++) {
496 uint32_t level = img_barrier->subresourceRange.baseMipLevel + j;
497 for (uint32_t k = 0; k < layer_count; k++) {
498 uint32_t layer = img_barrier->subresourceRange.baseArrayLayer + k;
499 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
500 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
501 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
502 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700503 }
504 }
Mike Weiblen62d08a32017-03-07 22:18:27 -0700505
506 IMAGE_STATE *image_state = GetImageState(device_data, img_barrier->image);
507 if (image_state) {
508 VkImageUsageFlags usage_flags = image_state->createInfo.usage;
509 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, false, usage_flags, func_name);
510 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, true, usage_flags, func_name);
511 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700512 }
513 return skip;
514}
515
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700516void TransitionImageLayouts(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t memBarrierCount,
517 const VkImageMemoryBarrier *pImgMemBarriers) {
518 GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700519
520 for (uint32_t i = 0; i < memBarrierCount; ++i) {
521 auto mem_barrier = &pImgMemBarriers[i];
522 if (!mem_barrier) continue;
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700523
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600524 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, mem_barrier->image)->createInfo);
525 uint32_t level_count = ResolveRemainingLevels(&mem_barrier->subresourceRange, image_create_info->mipLevels);
526 uint32_t layer_count = ResolveRemainingLayers(&mem_barrier->subresourceRange, image_create_info->arrayLayers);
527
528 for (uint32_t j = 0; j < level_count; j++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700529 uint32_t level = mem_barrier->subresourceRange.baseMipLevel + j;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600530 for (uint32_t k = 0; k < layer_count; k++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700531 uint32_t layer = mem_barrier->subresourceRange.baseArrayLayer + k;
532 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
533 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
534 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
535 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
536 }
537 }
538 }
539}
540
Tobin Ehlisc8266452017-04-07 12:20:30 -0600541bool VerifyImageLayout(layer_data const *device_data, GLOBAL_CB_NODE const *cb_node, IMAGE_STATE *image_state,
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600542 VkImageSubresourceLayers subLayers, VkImageLayout explicit_layout, VkImageLayout optimal_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600543 const char *caller, UNIQUE_VALIDATION_ERROR_CODE msg_code, bool *error) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700544 const auto report_data = core_validation::GetReportData(device_data);
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600545 const auto image = image_state->image;
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600546 bool skip = false;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700547
548 for (uint32_t i = 0; i < subLayers.layerCount; ++i) {
549 uint32_t layer = i + subLayers.baseArrayLayer;
550 VkImageSubresource sub = {subLayers.aspectMask, subLayers.mipLevel, layer};
551 IMAGE_CMD_BUF_LAYOUT_NODE node;
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600552 if (FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
553 if (node.layout != explicit_layout) {
Tobin Ehlisc8266452017-04-07 12:20:30 -0600554 *error = true;
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600555 // TODO: Improve log message in the next pass
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600556 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
557 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
558 "%s: Cannot use image 0x%" PRIxLEAST64
559 " with specific layout %s that doesn't match the actual current layout %s.",
560 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
561 string_VkImageLayout(node.layout));
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600562 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700563 }
564 }
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600565 // If optimal_layout is not UNDEFINED, check that layout matches optimal for this case
566 if ((VK_IMAGE_LAYOUT_UNDEFINED != optimal_layout) && (explicit_layout != optimal_layout)) {
567 if (VK_IMAGE_LAYOUT_GENERAL == explicit_layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700568 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
569 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600570 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
571 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(cb_node->commandBuffer),
572 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
573 "%s: For optimal performance image 0x%" PRIxLEAST64 " layout should be %s instead of GENERAL.",
574 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(optimal_layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700575 }
576 } else {
Tobin Ehlisc8266452017-04-07 12:20:30 -0600577 *error = true;
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600578 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
579 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, msg_code, "DS",
580 "%s: Layout for image 0x%" PRIxLEAST64 " is %s but can only be %s or VK_IMAGE_LAYOUT_GENERAL. %s",
581 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
582 string_VkImageLayout(optimal_layout), validation_error_map[msg_code]);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700583 }
584 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600585 return skip;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700586}
587
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700588void TransitionFinalSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkRenderPassBeginInfo *pRenderPassBegin,
589 FRAMEBUFFER_STATE *framebuffer_state) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700590 auto renderPass = GetRenderPassState(device_data, pRenderPassBegin->renderPass);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700591 if (!renderPass) return;
592
593 const VkRenderPassCreateInfo *pRenderPassInfo = renderPass->createInfo.ptr();
594 if (framebuffer_state) {
595 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
596 auto image_view = framebuffer_state->createInfo.pAttachments[i];
597 SetImageViewLayout(device_data, pCB, image_view, pRenderPassInfo->pAttachments[i].finalLayout);
598 }
599 }
600}
601
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700602bool PreCallValidateCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo,
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700603 const VkAllocationCallbacks *pAllocator, VkImage *pImage) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600604 bool skip = false;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700605 const debug_report_data *report_data = core_validation::GetReportData(device_data);
606
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600607 if (pCreateInfo->format == VK_FORMAT_UNDEFINED) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600608 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
609 VALIDATION_ERROR_00715, "IMAGE", "vkCreateImage: VkFormat for image must not be VK_FORMAT_UNDEFINED. %s",
610 validation_error_map[VALIDATION_ERROR_00715]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600611
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600612 return skip;
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600613 }
614
615 const VkFormatProperties *properties = GetFormatProperties(device_data, pCreateInfo->format);
616
617 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) && (properties->linearTilingFeatures == 0)) {
618 std::stringstream ss;
619 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600620 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
621 VALIDATION_ERROR_02150, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02150]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600622
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600623 return skip;
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600624 }
625
626 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) && (properties->optimalTilingFeatures == 0)) {
627 std::stringstream ss;
628 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600629 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
630 VALIDATION_ERROR_02155, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02155]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600631
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600632 return skip;
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600633 }
634
635 // Validate that format supports usage as color attachment
636 if (pCreateInfo->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
637 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
638 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
639 std::stringstream ss;
640 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
641 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600642 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600643 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
644 VALIDATION_ERROR_02158, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02158]);
645 }
646 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
647 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
648 std::stringstream ss;
649 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
650 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600651 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600652 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
653 VALIDATION_ERROR_02153, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02153]);
654 }
655 }
656
657 // Validate that format supports usage as depth/stencil attachment
658 if (pCreateInfo->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
659 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
660 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
661 std::stringstream ss;
662 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
663 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600664 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600665 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
666 VALIDATION_ERROR_02159, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02159]);
667 }
668 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
669 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
670 std::stringstream ss;
671 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
672 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600673 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600674 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
675 VALIDATION_ERROR_02154, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02154]);
676 }
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700677 }
678
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700679 const VkImageFormatProperties *ImageFormatProperties = GetImageFormatProperties(
680 device_data, pCreateInfo->format, pCreateInfo->imageType, pCreateInfo->tiling, pCreateInfo->usage, pCreateInfo->flags);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700681
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700682 VkDeviceSize imageGranularity = GetPhysicalDeviceProperties(device_data)->limits.bufferImageGranularity;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700683 imageGranularity = imageGranularity == 1 ? 0 : imageGranularity;
Tobin Ehlisa55b1d42017-04-04 12:23:48 -0600684 // TODO : This is also covering 2918 & 2919. Break out into separate checks
Mark Lobodzinski688ed322017-01-27 11:13:21 -0700685 if ((pCreateInfo->extent.width <= 0) || (pCreateInfo->extent.height <= 0) || (pCreateInfo->extent.depth <= 0)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600686 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
687 VALIDATION_ERROR_02917, "Image",
688 "CreateImage extent is 0 for at least one required dimension for image: "
689 "Width = %d Height = %d Depth = %d. %s",
690 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
691 validation_error_map[VALIDATION_ERROR_02917]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700692 }
693
694 // TODO: VALIDATION_ERROR_02125 VALIDATION_ERROR_02126 VALIDATION_ERROR_02128 VALIDATION_ERROR_00720
695 // All these extent-related VUs should be checked here
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700696 if ((pCreateInfo->extent.depth > ImageFormatProperties->maxExtent.depth) ||
697 (pCreateInfo->extent.width > ImageFormatProperties->maxExtent.width) ||
698 (pCreateInfo->extent.height > ImageFormatProperties->maxExtent.height)) {
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 extents exceed allowable limits for format: "
702 "Width = %d Height = %d Depth = %d: Limits for Width = %d Height = %d Depth = %d for format %s.",
703 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
704 ImageFormatProperties->maxExtent.width, ImageFormatProperties->maxExtent.height,
705 ImageFormatProperties->maxExtent.depth, string_VkFormat(pCreateInfo->format));
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700706 }
707
708 uint64_t totalSize = ((uint64_t)pCreateInfo->extent.width * (uint64_t)pCreateInfo->extent.height *
709 (uint64_t)pCreateInfo->extent.depth * (uint64_t)pCreateInfo->arrayLayers *
Dave Houlton1d2022c2017-03-29 11:43:58 -0600710 (uint64_t)pCreateInfo->samples * (uint64_t)FormatSize(pCreateInfo->format) +
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700711 (uint64_t)imageGranularity) &
712 ~(uint64_t)imageGranularity;
713
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700714 if (totalSize > ImageFormatProperties->maxResourceSize) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600715 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
716 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
717 "CreateImage resource size exceeds allowable maximum "
718 "Image resource size = 0x%" PRIxLEAST64 ", maximum resource size = 0x%" PRIxLEAST64 " ",
719 totalSize, ImageFormatProperties->maxResourceSize);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700720 }
721
722 // TODO: VALIDATION_ERROR_02132
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700723 if (pCreateInfo->mipLevels > ImageFormatProperties->maxMipLevels) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600724 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
725 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
726 "CreateImage mipLevels=%d exceeds allowable maximum supported by format of %d", pCreateInfo->mipLevels,
727 ImageFormatProperties->maxMipLevels);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700728 }
729
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700730 if (pCreateInfo->arrayLayers > ImageFormatProperties->maxArrayLayers) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600731 skip |= log_msg(
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700732 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__, VALIDATION_ERROR_02133,
733 "Image", "CreateImage arrayLayers=%d exceeds allowable maximum supported by format of %d. %s", pCreateInfo->arrayLayers,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700734 ImageFormatProperties->maxArrayLayers, validation_error_map[VALIDATION_ERROR_02133]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700735 }
736
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700737 if ((pCreateInfo->samples & ImageFormatProperties->sampleCounts) == 0) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600738 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
739 VALIDATION_ERROR_02138, "Image", "CreateImage samples %s is not supported by format 0x%.8X. %s",
740 string_VkSampleCountFlagBits(pCreateInfo->samples), ImageFormatProperties->sampleCounts,
741 validation_error_map[VALIDATION_ERROR_02138]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700742 }
743
744 if (pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_UNDEFINED && pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_PREINITIALIZED) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600745 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
746 VALIDATION_ERROR_00731, "Image",
747 "vkCreateImage parameter, pCreateInfo->initialLayout, must be VK_IMAGE_LAYOUT_UNDEFINED or "
748 "VK_IMAGE_LAYOUT_PREINITIALIZED. %s",
749 validation_error_map[VALIDATION_ERROR_00731]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700750 }
751
Mark Lobodzinski847b60c2017-03-13 09:32:45 -0600752 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600753 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
754 VALIDATION_ERROR_02143, "DS",
755 "vkCreateImage(): the sparseBinding device feature is disabled: Images cannot be created with the "
756 "VK_IMAGE_CREATE_SPARSE_BINDING_BIT set. %s",
757 validation_error_map[VALIDATION_ERROR_02143]);
Mark Lobodzinski847b60c2017-03-13 09:32:45 -0600758 }
759
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -0600760 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600761 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
762 DRAWSTATE_INVALID_FEATURE, "DS",
763 "vkCreateImage(): the sparseResidencyAliased device feature is disabled: Images cannot be created with the "
764 "VK_IMAGE_CREATE_SPARSE_ALIASED_BIT set.");
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -0600765 }
766
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600767 return skip;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700768}
769
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700770void PostCallRecordCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo, VkImage *pImage) {
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700771 IMAGE_LAYOUT_NODE image_state;
772 image_state.layout = pCreateInfo->initialLayout;
773 image_state.format = pCreateInfo->format;
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700774 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 -0700775 ImageSubresourcePair subpair{*pImage, false, VkImageSubresource()};
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700776 (*core_validation::GetImageSubresourceMap(device_data))[*pImage].push_back(subpair);
777 (*core_validation::GetImageLayoutMap(device_data))[subpair] = image_state;
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700778}
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700779
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700780bool PreCallValidateDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE **image_state, VK_OBJECT *obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700781 const CHECK_DISABLED *disabled = core_validation::GetDisables(device_data);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700782 *image_state = core_validation::GetImageState(device_data, image);
Mark Lobodzinski33826372017-04-13 11:10:11 -0600783 *obj_struct = {reinterpret_cast<uint64_t &>(image), kVulkanObjectTypeImage };
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700784 if (disabled->destroy_image) return false;
785 bool skip = false;
786 if (*image_state) {
787 skip |= core_validation::ValidateObjectNotInUse(device_data, *image_state, *obj_struct, VALIDATION_ERROR_00743);
788 }
789 return skip;
790}
791
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700792void PostCallRecordDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE *image_state, VK_OBJECT obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700793 core_validation::invalidateCommandBuffers(device_data, image_state->cb_bindings, obj_struct);
794 // Clean up memory mapping, bindings and range references for image
795 for (auto mem_binding : image_state->GetBoundMemory()) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700796 auto mem_info = core_validation::GetMemObjInfo(device_data, mem_binding);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700797 if (mem_info) {
798 core_validation::RemoveImageMemoryRange(obj_struct.handle, mem_info);
799 }
800 }
Mark Lobodzinski33826372017-04-13 11:10:11 -0600801 core_validation::ClearMemoryObjectBindings(device_data, obj_struct.handle, kVulkanObjectTypeImage);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700802 // Remove image from imageMap
803 core_validation::GetImageMap(device_data)->erase(image);
804 std::unordered_map<VkImage, std::vector<ImageSubresourcePair>> *imageSubresourceMap =
805 core_validation::GetImageSubresourceMap(device_data);
806
807 const auto &sub_entry = imageSubresourceMap->find(image);
808 if (sub_entry != imageSubresourceMap->end()) {
809 for (const auto &pair : sub_entry->second) {
810 core_validation::GetImageLayoutMap(device_data)->erase(pair);
811 }
812 imageSubresourceMap->erase(sub_entry);
813 }
814}
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700815
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700816bool ValidateImageAttributes(layer_data *device_data, IMAGE_STATE *image_state, VkImageSubresourceRange range) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700817 bool skip = false;
818 const debug_report_data *report_data = core_validation::GetReportData(device_data);
819
820 if (range.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) {
821 char const str[] = "vkCmdClearColorImage aspectMasks for all subresource ranges must be set to VK_IMAGE_ASPECT_COLOR_BIT";
822 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
823 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
824 }
825
Dave Houlton1d2022c2017-03-29 11:43:58 -0600826 if (FormatIsDepthOrStencil(image_state->createInfo.format)) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700827 char const str[] = "vkCmdClearColorImage called with depth/stencil image.";
828 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
829 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
830 validation_error_map[VALIDATION_ERROR_01088]);
Dave Houlton1d2022c2017-03-29 11:43:58 -0600831 } else if (FormatIsCompressed(image_state->createInfo.format)) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700832 char const str[] = "vkCmdClearColorImage called with compressed image.";
833 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
834 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
835 validation_error_map[VALIDATION_ERROR_01088]);
836 }
837
838 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
839 char const str[] = "vkCmdClearColorImage called with image created without VK_IMAGE_USAGE_TRANSFER_DST_BIT.";
840 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
841 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01084, "IMAGE", "%s. %s", str,
842 validation_error_map[VALIDATION_ERROR_01084]);
843 }
844 return skip;
845}
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700846
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600847uint32_t ResolveRemainingLevels(const VkImageSubresourceRange *range, uint32_t mip_levels) {
848 // Return correct number of mip levels taking into account VK_REMAINING_MIP_LEVELS
849 uint32_t mip_level_count = range->levelCount;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700850 if (range->levelCount == VK_REMAINING_MIP_LEVELS) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600851 mip_level_count = mip_levels - range->baseMipLevel;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700852 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600853 return mip_level_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700854}
855
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600856uint32_t ResolveRemainingLayers(const VkImageSubresourceRange *range, uint32_t layers) {
857 // Return correct number of layers taking into account VK_REMAINING_ARRAY_LAYERS
858 uint32_t array_layer_count = range->layerCount;
859 if (range->layerCount == VK_REMAINING_ARRAY_LAYERS) {
860 array_layer_count = layers - range->baseArrayLayer;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700861 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600862 return array_layer_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700863}
864
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700865bool VerifyClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *image_state,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700866 VkImageSubresourceRange range, VkImageLayout dest_image_layout, const char *func_name) {
867 bool skip = false;
868 const debug_report_data *report_data = core_validation::GetReportData(device_data);
869
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600870 uint32_t level_count = ResolveRemainingLevels(&range, image_state->createInfo.mipLevels);
871 uint32_t layer_count = ResolveRemainingLayers(&range, image_state->createInfo.arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700872
873 if (dest_image_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
874 if (dest_image_layout == VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700875 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
876 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600877 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
878 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700879 "%s: Layout for cleared image should be TRANSFER_DST_OPTIMAL instead of GENERAL.", func_name);
880 }
881 } else {
882 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01086;
883 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
884 error_code = VALIDATION_ERROR_01101;
885 } else {
886 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
887 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600888 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
889 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, error_code, "DS",
890 "%s: Layout for cleared image is %s but can only be "
891 "TRANSFER_DST_OPTIMAL or GENERAL. %s",
892 func_name, string_VkImageLayout(dest_image_layout), validation_error_map[error_code]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700893 }
894 }
895
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600896 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
897 uint32_t level = level_index + range.baseMipLevel;
898 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
899 uint32_t layer = layer_index + range.baseArrayLayer;
900 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700901 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700902 if (FindCmdBufLayout(device_data, cb_node, image_state->image, sub, node)) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700903 if (node.layout != dest_image_layout) {
904 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01085;
905 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
906 error_code = VALIDATION_ERROR_01100;
907 } else {
908 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
909 }
910 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
911 __LINE__, error_code, "DS",
912 "%s: Cannot clear an image whose layout is %s and "
913 "doesn't match the current layout %s. %s",
914 func_name, string_VkImageLayout(dest_image_layout), string_VkImageLayout(node.layout),
915 validation_error_map[error_code]);
916 }
917 }
918 }
919 }
920
921 return skip;
922}
923
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700924void RecordClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImage image, VkImageSubresourceRange range,
925 VkImageLayout dest_image_layout) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600926 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, image)->createInfo);
927 uint32_t level_count = ResolveRemainingLevels(&range, image_create_info->mipLevels);
928 uint32_t layer_count = ResolveRemainingLayers(&range, image_create_info->arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700929
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600930 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
931 uint32_t level = level_index + range.baseMipLevel;
932 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
933 uint32_t layer = layer_index + range.baseArrayLayer;
934 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700935 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700936 if (!FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
937 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 -0700938 }
939 }
940 }
941}
942
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700943bool PreCallValidateCmdClearColorImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700944 VkImageLayout imageLayout, uint32_t rangeCount, const VkImageSubresourceRange *pRanges) {
945 bool skip = false;
946 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700947 auto cb_node = GetCBNode(dev_data, commandBuffer);
948 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700949 if (cb_node && image_state) {
950 skip |= ValidateMemoryIsBoundToImage(dev_data, image_state, "vkCmdClearColorImage()", VALIDATION_ERROR_02527);
Mike Schuchardt9c582402017-02-23 15:57:37 -0700951 skip |= ValidateCmdQueueFlags(dev_data, cb_node, "vkCmdClearColorImage()", VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT,
952 VALIDATION_ERROR_01095);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700953 skip |= ValidateCmd(dev_data, cb_node, CMD_CLEARCOLORIMAGE, "vkCmdClearColorImage()");
954 skip |= insideRenderPass(dev_data, cb_node, "vkCmdClearColorImage()", VALIDATION_ERROR_01096);
955 for (uint32_t i = 0; i < rangeCount; ++i) {
956 skip |= ValidateImageAttributes(dev_data, image_state, pRanges[i]);
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700957 skip |= VerifyClearImageLayout(dev_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearColorImage()");
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700958 }
959 }
960 return skip;
961}
962
963// This state recording routine is shared between ClearColorImage and ClearDepthStencilImage
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700964void PreCallRecordCmdClearImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
965 uint32_t rangeCount, const VkImageSubresourceRange *pRanges, CMD_TYPE cmd_type) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700966 auto cb_node = GetCBNode(dev_data, commandBuffer);
967 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700968 if (cb_node && image_state) {
969 AddCommandBufferBindingImage(dev_data, cb_node, image_state);
970 std::function<bool()> function = [=]() {
971 SetImageMemoryValid(dev_data, image_state, true);
972 return false;
973 };
974 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700975 core_validation::UpdateCmdBufferLastCmd(cb_node, cmd_type);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700976 for (uint32_t i = 0; i < rangeCount; ++i) {
977 RecordClearImageLayout(dev_data, cb_node, image, pRanges[i], imageLayout);
978 }
979 }
980}
981
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700982bool PreCallValidateCmdClearDepthStencilImage(layer_data *device_data, VkCommandBuffer commandBuffer, VkImage image,
983 VkImageLayout imageLayout, uint32_t rangeCount,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700984 const VkImageSubresourceRange *pRanges) {
985 bool skip = false;
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700986 const debug_report_data *report_data = core_validation::GetReportData(device_data);
987
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700988 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700989 auto cb_node = GetCBNode(device_data, commandBuffer);
990 auto image_state = GetImageState(device_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700991 if (cb_node && image_state) {
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700992 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_02528);
Mike Schuchardt9c582402017-02-23 15:57:37 -0700993 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearDepthStencilImage()", VK_QUEUE_GRAPHICS_BIT,
994 VALIDATION_ERROR_01110);
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700995 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARDEPTHSTENCILIMAGE, "vkCmdClearDepthStencilImage()");
996 skip |= insideRenderPass(device_data, cb_node, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_01111);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700997 for (uint32_t i = 0; i < rangeCount; ++i) {
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700998 skip |=
999 VerifyClearImageLayout(device_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearDepthStencilImage()");
Mark Lobodzinski1241a312017-02-01 10:57:21 -07001000 // Image aspect must be depth or stencil or both
1001 if (((pRanges[i].aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
1002 ((pRanges[i].aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1003 char const str[] =
1004 "vkCmdClearDepthStencilImage aspectMasks for all subresource ranges must be "
1005 "set to VK_IMAGE_ASPECT_DEPTH_BIT and/or VK_IMAGE_ASPECT_STENCIL_BIT";
1006 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1007 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
1008 }
1009 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001010 if (image_state && !FormatIsDepthOrStencil(image_state->createInfo.format)) {
Mark Lobodzinski1241a312017-02-01 10:57:21 -07001011 char const str[] = "vkCmdClearDepthStencilImage called without a depth/stencil image.";
1012 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
1013 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_01103, "IMAGE", "%s. %s", str,
1014 validation_error_map[VALIDATION_ERROR_01103]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -07001015 }
1016 }
1017 return skip;
1018}
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001019
1020// Returns true if [x, xoffset] and [y, yoffset] overlap
1021static bool RangesIntersect(int32_t start, uint32_t start_offset, int32_t end, uint32_t end_offset) {
1022 bool result = false;
1023 uint32_t intersection_min = std::max(static_cast<uint32_t>(start), static_cast<uint32_t>(end));
1024 uint32_t intersection_max = std::min(static_cast<uint32_t>(start) + start_offset, static_cast<uint32_t>(end) + end_offset);
1025
1026 if (intersection_max > intersection_min) {
1027 result = true;
1028 }
1029 return result;
1030}
1031
1032// Returns true if two VkImageCopy structures overlap
1033static bool RegionIntersects(const VkImageCopy *src, const VkImageCopy *dst, VkImageType type) {
1034 bool result = false;
1035 if ((src->srcSubresource.mipLevel == dst->dstSubresource.mipLevel) &&
1036 (RangesIntersect(src->srcSubresource.baseArrayLayer, src->srcSubresource.layerCount, dst->dstSubresource.baseArrayLayer,
1037 dst->dstSubresource.layerCount))) {
1038 result = true;
1039 switch (type) {
1040 case VK_IMAGE_TYPE_3D:
1041 result &= RangesIntersect(src->srcOffset.z, src->extent.depth, dst->dstOffset.z, dst->extent.depth);
1042 // Intentionally fall through to 2D case
1043 case VK_IMAGE_TYPE_2D:
1044 result &= RangesIntersect(src->srcOffset.y, src->extent.height, dst->dstOffset.y, dst->extent.height);
1045 // Intentionally fall through to 1D case
1046 case VK_IMAGE_TYPE_1D:
1047 result &= RangesIntersect(src->srcOffset.x, src->extent.width, dst->dstOffset.x, dst->extent.width);
1048 break;
1049 default:
1050 // Unrecognized or new IMAGE_TYPE enums will be caught in parameter_validation
1051 assert(false);
1052 }
1053 }
1054 return result;
1055}
1056
1057// Returns true if offset and extent exceed image extents
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001058static bool ExceedsBounds(const VkOffset3D *offset, const VkExtent3D *extent, const VkExtent3D *image_extent) {
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001059 bool result = false;
1060 // Extents/depths cannot be negative but checks left in for clarity
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001061 if ((offset->z + extent->depth > image_extent->depth) || (offset->z < 0) ||
1062 ((offset->z + static_cast<int32_t>(extent->depth)) < 0)) {
1063 result = true;
1064 }
1065 if ((offset->y + extent->height > image_extent->height) || (offset->y < 0) ||
1066 ((offset->y + static_cast<int32_t>(extent->height)) < 0)) {
1067 result = true;
1068 }
1069 if ((offset->x + extent->width > image_extent->width) || (offset->x < 0) ||
1070 ((offset->x + static_cast<int32_t>(extent->width)) < 0)) {
1071 result = true;
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001072 }
1073 return result;
1074}
1075
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001076// Test if two VkExtent3D structs are equivalent
1077static inline bool IsExtentEqual(const VkExtent3D *extent, const VkExtent3D *other_extent) {
1078 bool result = true;
1079 if ((extent->width != other_extent->width) || (extent->height != other_extent->height) ||
1080 (extent->depth != other_extent->depth)) {
1081 result = false;
1082 }
1083 return result;
1084}
1085
1086// Returns the image extent of a specific subresource.
1087static inline VkExtent3D GetImageSubresourceExtent(const IMAGE_STATE *img, const VkImageSubresourceLayers *subresource) {
1088 const uint32_t mip = subresource->mipLevel;
1089 VkExtent3D extent = img->createInfo.extent;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001090 // Don't allow mip adjustment to create 0 dim, but pass along a 0 if that's what subresource specified
1091 extent.width = (0 == extent.width ? 0 : std::max(1U, extent.width >> mip));
1092 extent.height = (0 == extent.height ? 0 : std::max(1U, extent.height >> mip));
1093 extent.depth = (0 == extent.depth ? 0 : std::max(1U, extent.depth >> mip));
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001094 return extent;
1095}
1096
1097// Test if the extent argument has all dimensions set to 0.
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001098static inline bool IsExtentAllZeroes(const VkExtent3D *extent) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001099 return ((extent->width == 0) && (extent->height == 0) && (extent->depth == 0));
1100}
1101
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001102// Test if the extent argument has any dimensions set to 0.
1103static inline bool IsExtentSizeZero(const VkExtent3D *extent) {
1104 return ((extent->width == 0) || (extent->height == 0) || (extent->depth == 0));
1105}
1106
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001107// Returns the image transfer granularity for a specific image scaled by compressed block size if necessary.
1108static inline VkExtent3D GetScaledItg(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *img) {
1109 // Default to (0, 0, 0) granularity in case we can't find the real granularity for the physical device.
1110 VkExtent3D granularity = {0, 0, 0};
1111 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
1112 if (pPool) {
1113 granularity =
1114 GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].minImageTransferGranularity;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001115 if (FormatIsCompressed(img->createInfo.format)) {
1116 auto block_size = FormatCompressedTexelBlockExtent(img->createInfo.format);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001117 granularity.width *= block_size.width;
1118 granularity.height *= block_size.height;
1119 }
1120 }
1121 return granularity;
1122}
1123
1124// Test elements of a VkExtent3D structure against alignment constraints contained in another VkExtent3D structure
1125static inline bool IsExtentAligned(const VkExtent3D *extent, const VkExtent3D *granularity) {
1126 bool valid = true;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001127 if ((SafeModulo(extent->depth, granularity->depth) != 0) || (SafeModulo(extent->width, granularity->width) != 0) ||
1128 (SafeModulo(extent->height, granularity->height) != 0)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001129 valid = false;
1130 }
1131 return valid;
1132}
1133
1134// Check elements of a VkOffset3D structure against a queue family's Image Transfer Granularity values
1135static inline bool CheckItgOffset(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkOffset3D *offset,
1136 const VkExtent3D *granularity, const uint32_t i, const char *function, const char *member) {
1137 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1138 bool skip = false;
1139 VkExtent3D offset_extent = {};
1140 offset_extent.width = static_cast<uint32_t>(abs(offset->x));
1141 offset_extent.height = static_cast<uint32_t>(abs(offset->y));
1142 offset_extent.depth = static_cast<uint32_t>(abs(offset->z));
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001143 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001144 // 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 -07001145 if (IsExtentAllZeroes(&offset_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001146 skip |=
1147 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1148 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1149 "%s: pRegion[%d].%s (x=%d, y=%d, z=%d) must be (x=0, y=0, z=0) "
1150 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1151 function, i, member, offset->x, offset->y, offset->z);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001152 }
1153 } else {
1154 // If the queue family image transfer granularity is not (0, 0, 0), then the offset dimensions must always be even
1155 // integer multiples of the image transfer granularity.
1156 if (IsExtentAligned(&offset_extent, granularity) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001157 skip |= log_msg(
1158 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1159 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1160 "%s: pRegion[%d].%s (x=%d, y=%d, z=%d) dimensions must be even integer "
1161 "multiples of this command buffer's queue family image transfer granularity (w=%d, h=%d, d=%d).",
1162 function, i, member, offset->x, offset->y, offset->z, granularity->width, granularity->height, granularity->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001163 }
1164 }
1165 return skip;
1166}
1167
1168// Check elements of a VkExtent3D structure against a queue family's Image Transfer Granularity values
1169static inline bool CheckItgExtent(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkExtent3D *extent,
1170 const VkOffset3D *offset, const VkExtent3D *granularity, const VkExtent3D *subresource_extent,
1171 const uint32_t i, const char *function, const char *member) {
1172 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1173 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001174 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001175 // If the queue family image transfer granularity is (0, 0, 0), then the extent must always match the image
1176 // subresource extent.
1177 if (IsExtentEqual(extent, subresource_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001178 skip |=
1179 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1180 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1181 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d) "
1182 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1183 function, i, member, extent->width, extent->height, extent->depth, subresource_extent->width,
1184 subresource_extent->height, subresource_extent->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001185 }
1186 } else {
1187 // If the queue family image transfer granularity is not (0, 0, 0), then the extent dimensions must always be even
1188 // integer multiples of the image transfer granularity or the offset + extent dimensions must always match the image
1189 // subresource extent dimensions.
1190 VkExtent3D offset_extent_sum = {};
1191 offset_extent_sum.width = static_cast<uint32_t>(abs(offset->x)) + extent->width;
1192 offset_extent_sum.height = static_cast<uint32_t>(abs(offset->y)) + extent->height;
1193 offset_extent_sum.depth = static_cast<uint32_t>(abs(offset->z)) + extent->depth;
1194 if ((IsExtentAligned(extent, granularity) == false) && (IsExtentEqual(&offset_extent_sum, subresource_extent) == false)) {
1195 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001196 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1197 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001198 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) dimensions must be even integer multiples of this command buffer's "
1199 "queue family image transfer granularity (w=%d, h=%d, d=%d) or offset (x=%d, y=%d, z=%d) + "
1200 "extent (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d).",
1201 function, i, member, extent->width, extent->height, extent->depth, granularity->width, granularity->height,
1202 granularity->depth, offset->x, offset->y, offset->z, extent->width, extent->height, extent->depth,
1203 subresource_extent->width, subresource_extent->height, subresource_extent->depth);
1204 }
1205 }
1206 return skip;
1207}
1208
1209// Check a uint32_t width or stride value against a queue family's Image Transfer Granularity width value
1210static inline bool CheckItgInt(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const uint32_t 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
1214 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001215 if (SafeModulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001216 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1217 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001218 "%s: pRegion[%d].%s (%d) must be an even integer multiple of this command buffer's queue family image "
1219 "transfer granularity width (%d).",
1220 function, i, member, value, granularity);
1221 }
1222 return skip;
1223}
1224
1225// Check a VkDeviceSize value against a queue family's Image Transfer Granularity width value
1226static inline bool CheckItgSize(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkDeviceSize value,
1227 const uint32_t granularity, const uint32_t i, const char *function, const char *member) {
1228 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1229 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001230 if (SafeModulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001231 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1232 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001233 "%s: pRegion[%d].%s (%" PRIdLEAST64
1234 ") must be an even integer multiple of this command buffer's queue family image transfer "
1235 "granularity width (%d).",
1236 function, i, member, value, granularity);
1237 }
1238 return skip;
1239}
1240
1241// Check valid usage Image Tranfer Granularity requirements for elements of a VkBufferImageCopy structure
1242bool ValidateCopyBufferImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1243 const IMAGE_STATE *img, const VkBufferImageCopy *region,
1244 const uint32_t i, const char *function) {
1245 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001246 if (FormatIsCompressed(img->createInfo.format) == true) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001247 // TODO: Add granularity checking for compressed formats
1248
1249 // bufferRowLength must be a multiple of the compressed texel block width
1250 // bufferImageHeight must be a multiple of the compressed texel block height
1251 // all members of imageOffset must be a multiple of the corresponding dimensions of the compressed texel block
1252 // bufferOffset must be a multiple of the compressed texel block size in bytes
1253 // imageExtent.width must be a multiple of the compressed texel block width or (imageExtent.width + imageOffset.x)
1254 // must equal the image subresource width
1255 // imageExtent.height must be a multiple of the compressed texel block height or (imageExtent.height + imageOffset.y)
1256 // must equal the image subresource height
1257 // imageExtent.depth must be a multiple of the compressed texel block depth or (imageExtent.depth + imageOffset.z)
1258 // must equal the image subresource depth
1259 } else {
1260 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1261 skip |= CheckItgSize(device_data, cb_node, region->bufferOffset, granularity.width, i, function, "bufferOffset");
1262 skip |= CheckItgInt(device_data, cb_node, region->bufferRowLength, granularity.width, i, function, "bufferRowLength");
1263 skip |= CheckItgInt(device_data, cb_node, region->bufferImageHeight, granularity.width, i, function, "bufferImageHeight");
1264 skip |= CheckItgOffset(device_data, cb_node, &region->imageOffset, &granularity, i, function, "imageOffset");
1265 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->imageSubresource);
1266 skip |= CheckItgExtent(device_data, cb_node, &region->imageExtent, &region->imageOffset, &granularity, &subresource_extent,
1267 i, function, "imageExtent");
1268 }
1269 return skip;
1270}
1271
1272// Check valid usage Image Tranfer Granularity requirements for elements of a VkImageCopy structure
1273bool ValidateCopyImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1274 const IMAGE_STATE *img, const VkImageCopy *region, const uint32_t i,
1275 const char *function) {
1276 bool skip = false;
1277 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1278 skip |= CheckItgOffset(device_data, cb_node, &region->srcOffset, &granularity, i, function, "srcOffset");
1279 skip |= CheckItgOffset(device_data, cb_node, &region->dstOffset, &granularity, i, function, "dstOffset");
1280 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->dstSubresource);
1281 skip |= CheckItgExtent(device_data, cb_node, &region->extent, &region->dstOffset, &granularity, &subresource_extent, i,
1282 function, "extent");
1283 return skip;
1284}
1285
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001286bool PreCallValidateCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001287 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1288 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001289 bool skip = false;
1290 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1291 VkCommandBuffer command_buffer = cb_node->commandBuffer;
1292
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001293 for (uint32_t i = 0; i < region_count; i++) {
1294 if (regions[i].srcSubresource.layerCount == 0) {
1295 std::stringstream ss;
1296 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] srcSubresource is zero";
1297 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1298 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1299 ss.str().c_str());
1300 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001301
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001302 if (regions[i].dstSubresource.layerCount == 0) {
1303 std::stringstream ss;
1304 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] dstSubresource is zero";
1305 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1306 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1307 ss.str().c_str());
1308 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001309
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001310 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
1311 // For each region the layerCount member of srcSubresource and dstSubresource must match
1312 if (regions[i].srcSubresource.layerCount != regions[i].dstSubresource.layerCount) {
1313 std::stringstream ss;
1314 ss << "vkCmdCopyImage: number of layers in source and destination subresources for pRegions[" << i
1315 << "] do not match";
1316 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1317 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01198, "IMAGE", "%s. %s",
1318 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01198]);
1319 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001320 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001321
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001322 // For each region, the aspectMask member of srcSubresource and dstSubresource must match
1323 if (regions[i].srcSubresource.aspectMask != regions[i].dstSubresource.aspectMask) {
1324 char const str[] = "vkCmdCopyImage: Src and dest aspectMasks for each region must match";
1325 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1326 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01197, "IMAGE", "%s. %s", str,
1327 validation_error_map[VALIDATION_ERROR_01197]);
1328 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001329
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06001330 // For each region, the aspectMask member of srcSubresource must be present in the source image
1331 if (!VerifyAspectsPresent(regions[i].srcSubresource.aspectMask, src_image_state->createInfo.format)) {
1332 std::stringstream ss;
1333 ss << "vkCmdCopyImage: pRegion[" << i
1334 << "] srcSubresource.aspectMask cannot specify aspects not present in source image";
1335 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1336 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01200, "IMAGE", "%s. %s",
1337 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01200]);
1338 }
1339
1340 // For each region, the aspectMask member of dstSubresource must be present in the destination image
1341 if (!VerifyAspectsPresent(regions[i].dstSubresource.aspectMask, dst_image_state->createInfo.format)) {
1342 std::stringstream ss;
1343 ss << "vkCmdCopyImage: pRegion[" << i << "] dstSubresource.aspectMask cannot specify aspects not present in dest image";
1344 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1345 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01201, "IMAGE", "%s. %s",
1346 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01201]);
1347 }
1348
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001349 // AspectMask must not contain VK_IMAGE_ASPECT_METADATA_BIT
1350 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) ||
1351 (regions[i].dstSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT)) {
1352 std::stringstream ss;
1353 ss << "vkCmdCopyImage: pRegions[" << i << "] may not specify aspectMask containing VK_IMAGE_ASPECT_METADATA_BIT";
1354 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1355 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01222, "IMAGE", "%s. %s",
1356 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01222]);
1357 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001358
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001359 // For each region, if aspectMask contains VK_IMAGE_ASPECT_COLOR_BIT, it must not contain either of
1360 // VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT
1361 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) &&
1362 (regions[i].srcSubresource.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
1363 char const str[] = "vkCmdCopyImage aspectMask cannot specify both COLOR and DEPTH/STENCIL aspects";
1364 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1365 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01221, "IMAGE", "%s. %s", str,
1366 validation_error_map[VALIDATION_ERROR_01221]);
1367 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001368
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001369 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
1370 // If either of the calling command's src_image or dst_image parameters are of VkImageType VK_IMAGE_TYPE_3D,
1371 // the baseArrayLayer and layerCount members of both srcSubresource and dstSubresource must be 0 and 1, respectively
1372 if (((src_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) ||
1373 (dst_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D)) &&
1374 ((regions[i].srcSubresource.baseArrayLayer != 0) || (regions[i].srcSubresource.layerCount != 1) ||
1375 (regions[i].dstSubresource.baseArrayLayer != 0) || (regions[i].dstSubresource.layerCount != 1))) {
1376 std::stringstream ss;
1377 ss << "vkCmdCopyImage: src or dstImage type was IMAGE_TYPE_3D, but in subRegion[" << i
1378 << "] baseArrayLayer was not zero or layerCount was not 1.";
1379 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1380 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01199, "IMAGE", "%s. %s",
1381 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01199]);
1382 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001383 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001384
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001385 // MipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
1386 if (regions[i].srcSubresource.mipLevel >= src_image_state->createInfo.mipLevels) {
1387 std::stringstream ss;
1388 ss << "vkCmdCopyImage: pRegions[" << i
1389 << "] specifies a src mipLevel greater than the number specified when the srcImage was created.";
1390 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1391 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
1392 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1393 }
1394 if (regions[i].dstSubresource.mipLevel >= dst_image_state->createInfo.mipLevels) {
1395 std::stringstream ss;
1396 ss << "vkCmdCopyImage: pRegions[" << i
1397 << "] specifies a dst mipLevel greater than the number specified when the dstImage was created.";
1398 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1399 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
1400 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1401 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001402
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001403 // (baseArrayLayer + layerCount) must be less than or equal to the arrayLayers specified in VkImageCreateInfo when the
1404 // image was created
1405 if ((regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount) >
1406 src_image_state->createInfo.arrayLayers) {
1407 std::stringstream ss;
1408 ss << "vkCmdCopyImage: srcImage arrayLayers was " << src_image_state->createInfo.arrayLayers << " but subRegion[" << i
1409 << "] baseArrayLayer + layerCount is "
1410 << (regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount);
1411 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1412 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
1413 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
1414 }
1415 if ((regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount) >
1416 dst_image_state->createInfo.arrayLayers) {
1417 std::stringstream ss;
1418 ss << "vkCmdCopyImage: dstImage arrayLayers was " << dst_image_state->createInfo.arrayLayers << " but subRegion[" << i
1419 << "] baseArrayLayer + layerCount is "
1420 << (regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount);
1421 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1422 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
1423 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
1424 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001425
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001426 // Check region extents for 1D-1D, 2D-2D, and 3D-3D copies
1427 if (src_image_state->createInfo.imageType == dst_image_state->createInfo.imageType) {
1428 // The source region specified by a given element of regions must be a region that is contained within srcImage
1429 if (ExceedsBounds(&regions[i].srcOffset, &regions[i].extent, &(src_image_state->createInfo.extent))) {
1430 std::stringstream ss;
1431 ss << "vkCmdCopyImage: srcSubResource in pRegions[" << i << "] exceeds extents srcImage was created with";
1432 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1433 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01175, "IMAGE", "%s. %s",
1434 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01175]);
1435 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001436
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001437 // The destination region specified by a given element of regions must be a region that is contained within dst_image
1438 if (ExceedsBounds(&regions[i].dstOffset, &regions[i].extent, &(dst_image_state->createInfo.extent))) {
1439 std::stringstream ss;
1440 ss << "vkCmdCopyImage: dstSubResource in pRegions[" << i << "] exceeds extents dstImage was created with";
1441 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1442 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01176, "IMAGE", "%s. %s",
1443 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01176]);
1444 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001445 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001446
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001447 // The union of all source regions, and the union of all destination regions, specified by the elements of regions,
1448 // must not overlap in memory
1449 if (src_image_state->image == dst_image_state->image) {
1450 for (uint32_t j = 0; j < region_count; j++) {
1451 if (RegionIntersects(&regions[i], &regions[j], src_image_state->createInfo.imageType)) {
1452 std::stringstream ss;
1453 ss << "vkCmdCopyImage: pRegions[" << i << "] src overlaps with pRegions[" << j << "].";
1454 skip |= 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__, VALIDATION_ERROR_01177, "IMAGE",
1456 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01177]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001457 }
1458 }
1459 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001460 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001461
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001462 // The formats of src_image and dst_image must be compatible. Formats are considered compatible if their texel size in bytes
1463 // is the same between both formats. For example, VK_FORMAT_R8G8B8A8_UNORM is compatible with VK_FORMAT_R32_UINT because
1464 // because both texels are 4 bytes in size. Depth/stencil formats must match exactly.
Dave Houlton1d2022c2017-03-29 11:43:58 -06001465 if (FormatIsDepthOrStencil(src_image_state->createInfo.format) ||
1466 FormatIsDepthOrStencil(dst_image_state->createInfo.format)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001467 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1468 char const str[] = "vkCmdCopyImage called with unmatched source and dest image depth/stencil formats.";
1469 skip |=
1470 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1471 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT, "IMAGE", str);
1472 }
1473 } else {
Dave Houlton1d2022c2017-03-29 11:43:58 -06001474 size_t srcSize = FormatSize(src_image_state->createInfo.format);
1475 size_t destSize = FormatSize(dst_image_state->createInfo.format);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001476 if (srcSize != destSize) {
1477 char const str[] = "vkCmdCopyImage called with unmatched source and dest image format sizes.";
1478 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1479 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01184, "IMAGE", "%s. %s", str,
1480 validation_error_map[VALIDATION_ERROR_01184]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001481 }
1482 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001483
Dave Houlton33c22b72017-02-28 13:16:02 -07001484 // Source and dest image sample counts must match
1485 if (src_image_state->createInfo.samples != dst_image_state->createInfo.samples) {
1486 char const str[] = "vkCmdCopyImage() called on image pair with non-identical sample counts.";
1487 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 -06001488 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01185, "IMAGE", "%s %s", str,
1489 validation_error_map[VALIDATION_ERROR_01185]);
Dave Houlton33c22b72017-02-28 13:16:02 -07001490 }
1491
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001492 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02533);
1493 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02534);
1494 // Validate that SRC & DST images have correct usage flags set
1495 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01178,
1496 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
1497 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01181,
1498 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001499 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyImage()",
1500 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01193);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001501 skip |= ValidateCmd(device_data, cb_node, CMD_COPYIMAGE, "vkCmdCopyImage()");
1502 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImage()", VALIDATION_ERROR_01194);
Tobin Ehlisc8266452017-04-07 12:20:30 -06001503 bool hit_error = false;
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001504 for (uint32_t i = 0; i < region_count; ++i) {
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06001505 skip |= VerifyImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -06001506 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01180, &hit_error);
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06001507 skip |= VerifyImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -06001508 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01183, &hit_error);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001509 skip |= ValidateCopyImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &regions[i], i,
1510 "vkCmdCopyImage()");
1511 }
1512
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001513 return skip;
1514}
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001515
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001516void PreCallRecordCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06001517 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1518 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
1519 // Make sure that all image slices are updated to correct layout
1520 for (uint32_t i = 0; i < region_count; ++i) {
1521 SetImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout);
1522 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout);
1523 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001524 // Update bindings between images and cmd buffer
1525 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1526 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Dave Houltoneba86e22017-03-02 14:56:23 -07001527 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImage()"); };
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001528 cb_node->validate_functions.push_back(function);
1529 function = [=]() {
1530 SetImageMemoryValid(device_data, dst_image_state, true);
1531 return false;
1532 };
1533 cb_node->validate_functions.push_back(function);
1534 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGE);
1535}
1536
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001537// Returns true if sub_rect is entirely contained within rect
1538static inline bool ContainsRect(VkRect2D rect, VkRect2D sub_rect) {
1539 if ((sub_rect.offset.x < rect.offset.x) || (sub_rect.offset.x + sub_rect.extent.width > rect.offset.x + rect.extent.width) ||
1540 (sub_rect.offset.y < rect.offset.y) || (sub_rect.offset.y + sub_rect.extent.height > rect.offset.y + rect.extent.height))
1541 return false;
1542 return true;
1543}
1544
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001545bool PreCallValidateCmdClearAttachments(layer_data *device_data, VkCommandBuffer commandBuffer, uint32_t attachmentCount,
1546 const VkClearAttachment *pAttachments, uint32_t rectCount, const VkClearRect *pRects) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001547 GLOBAL_CB_NODE *cb_node = GetCBNode(device_data, commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001548 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1549
1550 bool skip = false;
1551 if (cb_node) {
Mike Schuchardt9c582402017-02-23 15:57:37 -07001552 skip |=
1553 ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearAttachments()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01121);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001554 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARATTACHMENTS, "vkCmdClearAttachments()");
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001555 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_CLEARATTACHMENTS);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001556 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Chris Forbes05375e72017-04-21 13:15:15 -07001557 if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001558 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
1559 // 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 -07001560 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
1561 // CmdClearAttachments.
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001562 skip |=
1563 log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1564 reinterpret_cast<uint64_t &>(commandBuffer), 0, DRAWSTATE_CLEAR_CMD_BEFORE_DRAW, "DS",
1565 "vkCmdClearAttachments() issued on command buffer object 0x%p prior to any Draw Cmds."
1566 " It is recommended you use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
1567 commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001568 }
1569 skip |= outsideRenderPass(device_data, cb_node, "vkCmdClearAttachments()", VALIDATION_ERROR_01122);
1570 }
1571
1572 // Validate that attachment is in reference list of active subpass
1573 if (cb_node->activeRenderPass) {
1574 const VkRenderPassCreateInfo *renderpass_create_info = cb_node->activeRenderPass->createInfo.ptr();
1575 const VkSubpassDescription *subpass_desc = &renderpass_create_info->pSubpasses[cb_node->activeSubpass];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001576 auto framebuffer = GetFramebufferState(device_data, cb_node->activeFramebuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001577
1578 for (uint32_t i = 0; i < attachmentCount; i++) {
1579 auto clear_desc = &pAttachments[i];
1580 VkImageView image_view = VK_NULL_HANDLE;
1581
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001582 if (0 == clear_desc->aspectMask) {
1583 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 -06001584 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01128, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001585 validation_error_map[VALIDATION_ERROR_01128]);
1586 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) {
1587 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 -06001588 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01126, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001589 validation_error_map[VALIDATION_ERROR_01126]);
1590 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001591 if (clear_desc->colorAttachment >= subpass_desc->colorAttachmentCount) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001592 skip |=
1593 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 -06001594 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01114, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001595 "vkCmdClearAttachments() color attachment index %d out of range for active subpass %d. %s",
1596 clear_desc->colorAttachment, cb_node->activeSubpass, validation_error_map[VALIDATION_ERROR_01114]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001597 } else if (subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment == VK_ATTACHMENT_UNUSED) {
1598 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001599 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(commandBuffer),
1600 __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001601 "vkCmdClearAttachments() color attachment index %d is VK_ATTACHMENT_UNUSED; ignored.",
1602 clear_desc->colorAttachment);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001603 } else {
1604 image_view = framebuffer->createInfo
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001605 .pAttachments[subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment];
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001606 }
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001607 if ((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) ||
1608 (clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1609 char const str[] =
1610 "vkCmdClearAttachments aspectMask [%d] must set only VK_IMAGE_ASPECT_COLOR_BIT of a color attachment. %s";
1611 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 -06001612 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01125, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001613 validation_error_map[VALIDATION_ERROR_01125]);
1614 }
1615 } else { // Must be depth and/or stencil
1616 if (((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
1617 ((clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1618 char const str[] = "vkCmdClearAttachments aspectMask [%d] is not a valid combination of bits. %s";
1619 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 -06001620 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01127, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001621 validation_error_map[VALIDATION_ERROR_01127]);
1622 }
1623 if (!subpass_desc->pDepthStencilAttachment ||
1624 (subpass_desc->pDepthStencilAttachment->attachment == VK_ATTACHMENT_UNUSED)) {
1625 skip |= log_msg(
1626 report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001627 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001628 "vkCmdClearAttachments() depth/stencil clear with no depth/stencil attachment in subpass; ignored");
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001629 } else {
1630 image_view = framebuffer->createInfo.pAttachments[subpass_desc->pDepthStencilAttachment->attachment];
1631 }
1632 }
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001633 if (image_view) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001634 auto image_view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001635 for (uint32_t j = 0; j < rectCount; j++) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001636 // The rectangular region specified by a given element of pRects must be contained within the render area of
1637 // the current render pass instance
Mark Lobodzinskid833bb72017-02-22 10:55:30 -07001638 // TODO: This check should be moved to CmdExecuteCommands or QueueSubmit to cover secondary CB cases
1639 if ((cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) &&
1640 (false == ContainsRect(cb_node->activeRenderPassBeginInfo.renderArea, pRects[j].rect))) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001641 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1642 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01115, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001643 "vkCmdClearAttachments(): The area defined by pRects[%d] is not contained in the area of "
1644 "the current render pass instance. %s",
1645 j, validation_error_map[VALIDATION_ERROR_01115]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001646 }
1647 // The layers specified by a given element of pRects must be contained within every attachment that
1648 // pAttachments refers to
1649 auto attachment_base_array_layer = image_view_state->create_info.subresourceRange.baseArrayLayer;
1650 auto attachment_layer_count = image_view_state->create_info.subresourceRange.layerCount;
1651 if ((pRects[j].baseArrayLayer < attachment_base_array_layer) || pRects[j].layerCount > attachment_layer_count) {
1652 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001653 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1654 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01116, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001655 "vkCmdClearAttachments(): The layers defined in pRects[%d] are not contained in the layers of "
1656 "pAttachment[%d]. %s",
1657 j, i, validation_error_map[VALIDATION_ERROR_01116]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001658 }
1659 }
1660 }
1661 }
1662 }
1663 return skip;
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001664}
1665
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001666bool PreCallValidateCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001667 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageResolve *pRegions) {
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001668 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001669 bool skip = false;
1670 if (cb_node && src_image_state && dst_image_state) {
1671 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02541);
1672 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02542);
Mike Schuchardt9c582402017-02-23 15:57:37 -07001673 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdResolveImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01334);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001674 skip |= ValidateCmd(device_data, cb_node, CMD_RESOLVEIMAGE, "vkCmdResolveImage()");
1675 skip |= insideRenderPass(device_data, cb_node, "vkCmdResolveImage()", VALIDATION_ERROR_01335);
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001676
1677 // For each region, the number of layers in the image subresource should not be zero
1678 // For each region, src and dest image aspect must be color only
1679 for (uint32_t i = 0; i < regionCount; i++) {
1680 if (pRegions[i].srcSubresource.layerCount == 0) {
1681 char const str[] = "vkCmdResolveImage: number of layers in source subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001682 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 -07001683 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1684 "IMAGE", str);
1685 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001686 if (pRegions[i].dstSubresource.layerCount == 0) {
1687 char const str[] = "vkCmdResolveImage: number of layers in destination subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001688 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 -07001689 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1690 "IMAGE", str);
1691 }
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001692 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1693 skip |= log_msg(
1694 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1695 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01339, "IMAGE",
1696 "vkCmdResolveImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s", i,
1697 validation_error_map[VALIDATION_ERROR_01339]);
1698 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001699 if ((pRegions[i].srcSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) ||
1700 (pRegions[i].dstSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT)) {
1701 char const str[] =
1702 "vkCmdResolveImage: src and dest aspectMasks for each region must specify only VK_IMAGE_ASPECT_COLOR_BIT";
1703 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1704 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01338, "IMAGE",
1705 "%s. %s", str, validation_error_map[VALIDATION_ERROR_01338]);
1706 }
1707 }
1708
1709 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1710 char const str[] = "vkCmdResolveImage called with unmatched source and dest formats.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001711 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 -07001712 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT,
1713 "IMAGE", str);
1714 }
1715 if (src_image_state->createInfo.imageType != dst_image_state->createInfo.imageType) {
1716 char const str[] = "vkCmdResolveImage called with unmatched source and dest image types.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001717 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 -07001718 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_TYPE, "IMAGE",
1719 str);
1720 }
1721 if (src_image_state->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
1722 char const str[] = "vkCmdResolveImage called with source sample count less than 2.";
1723 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1724 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01320, "IMAGE", "%s. %s",
1725 str, validation_error_map[VALIDATION_ERROR_01320]);
1726 }
1727 if (dst_image_state->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
1728 char const str[] = "vkCmdResolveImage called with dest sample count greater than 1.";
1729 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1730 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01321, "IMAGE", "%s. %s",
1731 str, validation_error_map[VALIDATION_ERROR_01321]);
1732 }
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001733 } else {
1734 assert(0);
1735 }
1736 return skip;
1737}
1738
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001739void PreCallRecordCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
1740 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001741 // Update bindings between images and cmd buffer
1742 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1743 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
1744
1745 std::function<bool()> function = [=]() {
1746 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdResolveImage()");
1747 };
1748 cb_node->validate_functions.push_back(function);
1749 function = [=]() {
1750 SetImageMemoryValid(device_data, dst_image_state, true);
1751 return false;
1752 };
1753 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001754 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_RESOLVEIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001755}
1756
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001757bool PreCallValidateCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001758 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) {
1759 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1760
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001761 bool skip = false;
1762 if (cb_node && src_image_state && dst_image_state) {
1763 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): srcImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001764 VALIDATION_ERROR_02194);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001765 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): dstImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001766 VALIDATION_ERROR_02195);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001767 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02539);
1768 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02540);
1769 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_02182,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001770 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001771 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_02186,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001772 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001773 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdBlitImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01299);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001774 skip |= ValidateCmd(device_data, cb_node, CMD_BLITIMAGE, "vkCmdBlitImage()");
1775 skip |= insideRenderPass(device_data, cb_node, "vkCmdBlitImage()", VALIDATION_ERROR_01300);
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001776
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001777 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001778 // Warn for zero-sized regions
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001779 if ((pRegions[i].srcOffsets[0].x == pRegions[i].srcOffsets[1].x) ||
1780 (pRegions[i].srcOffsets[0].y == pRegions[i].srcOffsets[1].y) ||
1781 (pRegions[i].srcOffsets[0].z == pRegions[i].srcOffsets[1].z)) {
1782 std::stringstream ss;
1783 ss << "vkCmdBlitImage: pRegions[" << i << "].srcOffsets specify a zero-volume area.";
1784 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1785 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1786 "%s", ss.str().c_str());
1787 }
1788 if ((pRegions[i].dstOffsets[0].x == pRegions[i].dstOffsets[1].x) ||
1789 (pRegions[i].dstOffsets[0].y == pRegions[i].dstOffsets[1].y) ||
1790 (pRegions[i].dstOffsets[0].z == pRegions[i].dstOffsets[1].z)) {
1791 std::stringstream ss;
1792 ss << "vkCmdBlitImage: pRegions[" << i << "].dstOffsets specify a zero-volume area.";
1793 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1794 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1795 "%s", ss.str().c_str());
1796 }
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001797 if (pRegions[i].srcSubresource.layerCount == 0) {
1798 char const str[] = "vkCmdBlitImage: number of layers in source subresource is zero";
1799 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 -07001800 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1801 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001802 }
1803 if (pRegions[i].dstSubresource.layerCount == 0) {
1804 char const str[] = "vkCmdBlitImage: number of layers in destination subresource is zero";
1805 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 -07001806 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1807 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001808 }
1809
1810 // Check that src/dst layercounts match
1811 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1812 skip |=
1813 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1814 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01304, "IMAGE",
1815 "vkCmdBlitImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s",
1816 i, validation_error_map[VALIDATION_ERROR_01304]);
1817 }
Mark Lobodzinskie7e85fd2017-02-07 13:44:57 -07001818
1819 if (pRegions[i].srcSubresource.aspectMask != pRegions[i].dstSubresource.aspectMask) {
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_01303, "IMAGE",
1822 "vkCmdBlitImage: aspectMask members for pRegion[%d] do not match. %s", i,
1823 validation_error_map[VALIDATION_ERROR_01303]);
1824 }
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001825 }
1826
1827 VkFormat src_format = src_image_state->createInfo.format;
1828 VkFormat dst_format = dst_image_state->createInfo.format;
1829
1830 // Validate consistency for unsigned formats
Dave Houlton1d2022c2017-03-29 11:43:58 -06001831 if (FormatIsUInt(src_format) != FormatIsUInt(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001832 std::stringstream ss;
1833 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has unsigned integer format, "
1834 << "the other one must also have unsigned integer format. "
1835 << "Source format is " << string_VkFormat(src_format) << " Destination format is " << string_VkFormat(dst_format);
1836 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1837 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02191, "IMAGE", "%s. %s",
1838 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02191]);
1839 }
1840
1841 // Validate consistency for signed formats
Dave Houlton1d2022c2017-03-29 11:43:58 -06001842 if (FormatIsSInt(src_format) != FormatIsSInt(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001843 std::stringstream ss;
1844 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has signed integer format, "
1845 << "the other one must also have signed integer format. "
1846 << "Source format is " << string_VkFormat(src_format) << " Destination format is " << string_VkFormat(dst_format);
1847 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1848 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02190, "IMAGE", "%s. %s",
1849 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02190]);
1850 }
1851
1852 // Validate aspect bits and formats for depth/stencil images
Dave Houlton1d2022c2017-03-29 11:43:58 -06001853 if (FormatIsDepthOrStencil(src_format) || FormatIsDepthOrStencil(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001854 if (src_format != dst_format) {
1855 std::stringstream ss;
1856 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has a format of depth, stencil or depth "
1857 << "stencil, the other one must have exactly the same format. "
1858 << "Source format is " << string_VkFormat(src_format) << " Destination format is "
1859 << string_VkFormat(dst_format);
1860 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1861 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02192, "IMAGE",
1862 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02192]);
1863 }
1864
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001865 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001866 VkImageAspectFlags srcAspect = pRegions[i].srcSubresource.aspectMask;
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001867
Dave Houlton1d2022c2017-03-29 11:43:58 -06001868 if (FormatIsDepthAndStencil(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001869 if ((srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1870 std::stringstream ss;
1871 ss << "vkCmdBlitImage: Combination depth/stencil image formats must have only one of "
1872 "VK_IMAGE_ASPECT_DEPTH_BIT "
1873 << "and VK_IMAGE_ASPECT_STENCIL_BIT set in srcImage and dstImage";
1874 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1875 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1876 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1877 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001878 } else if (FormatIsStencilOnly(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001879 if (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT) {
1880 std::stringstream ss;
1881 ss << "vkCmdBlitImage: Stencil-only image formats must have only the VK_IMAGE_ASPECT_STENCIL_BIT "
1882 << "set in both the srcImage and dstImage";
1883 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1884 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1885 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1886 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001887 } else if (FormatIsDepthOnly(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001888 if (srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) {
1889 std::stringstream ss;
1890 ss << "vkCmdBlitImage: Depth-only image formats must have only the VK_IMAGE_ASPECT_DEPTH "
1891 << "set in both the srcImage and dstImage";
1892 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1893 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1894 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1895 }
1896 }
1897 }
1898 }
1899
1900 // Validate filter
Dave Houlton1d2022c2017-03-29 11:43:58 -06001901 if (FormatIsDepthOrStencil(src_format) && (filter != VK_FILTER_NEAREST)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001902 std::stringstream ss;
1903 ss << "vkCmdBlitImage: If the format of srcImage is a depth, stencil, or depth stencil "
1904 << "then filter must be VK_FILTER_NEAREST.";
1905 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1906 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02193, "IMAGE", "%s. %s",
1907 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02193]);
1908 }
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001909 } else {
1910 assert(0);
1911 }
1912 return skip;
1913}
1914
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001915void PreCallRecordCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
1916 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001917 // Update bindings between images and cmd buffer
1918 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1919 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
1920
1921 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdBlitImage()"); };
1922 cb_node->validate_functions.push_back(function);
1923 function = [=]() {
1924 SetImageMemoryValid(device_data, dst_image_state, true);
1925 return false;
1926 };
1927 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001928 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_BLITIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001929}
1930
Tony Barbourdf013b92017-01-25 12:53:48 -07001931// This validates that the initial layout specified in the command buffer for
1932// the IMAGE is the same
1933// as the global IMAGE layout
Tony Barboure0c5cc92017-02-08 13:53:39 -07001934bool ValidateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB,
1935 std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap) {
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07001936 bool skip = false;
Tony Barbourdf013b92017-01-25 12:53:48 -07001937 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001938 for (auto cb_image_data : pCB->imageLayoutMap) {
1939 VkImageLayout imageLayout;
Tony Barbourdf013b92017-01-25 12:53:48 -07001940
Jeremy Hayes55b6c292017-02-28 09:44:45 -07001941 if (FindLayout(imageLayoutMap, cb_image_data.first, imageLayout)) {
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001942 if (cb_image_data.second.initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
1943 // TODO: Set memory invalid which is in mem_tracker currently
1944 } else if (imageLayout != cb_image_data.second.initialLayout) {
1945 if (cb_image_data.first.hasSubresource) {
Dave Houltoneba86e22017-03-02 14:56:23 -07001946 skip |= log_msg(
1947 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1948 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
1949 "Cannot submit cmd buffer using image (0x%" PRIx64
1950 ") [sub-resource: aspectMask 0x%X array layer %u, mip level %u], "
1951 "with layout %s when first use is %s.",
1952 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), cb_image_data.first.subresource.aspectMask,
1953 cb_image_data.first.subresource.arrayLayer, cb_image_data.first.subresource.mipLevel,
1954 string_VkImageLayout(imageLayout), string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001955 } else {
Dave Houltoneba86e22017-03-02 14:56:23 -07001956 skip |=
1957 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1958 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
1959 "Cannot submit cmd buffer using image (0x%" PRIx64
1960 ") with layout %s when "
1961 "first use is %s.",
1962 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), string_VkImageLayout(imageLayout),
1963 string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001964 }
1965 }
Tony Barbourdf013b92017-01-25 12:53:48 -07001966 SetLayout(imageLayoutMap, cb_image_data.first, cb_image_data.second.layout);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001967 }
1968 }
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07001969 return skip;
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001970}
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001971
Tony Barbourdf013b92017-01-25 12:53:48 -07001972void UpdateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB) {
1973 for (auto cb_image_data : pCB->imageLayoutMap) {
1974 VkImageLayout imageLayout;
1975 FindGlobalLayout(device_data, cb_image_data.first, imageLayout);
1976 SetGlobalLayout(device_data, cb_image_data.first, cb_image_data.second.layout);
1977 }
1978}
1979
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001980// Print readable FlagBits in FlagMask
1981static std::string string_VkAccessFlags(VkAccessFlags accessMask) {
1982 std::string result;
1983 std::string separator;
1984
1985 if (accessMask == 0) {
1986 result = "[None]";
1987 } else {
1988 result = "[";
1989 for (auto i = 0; i < 32; i++) {
1990 if (accessMask & (1 << i)) {
1991 result = result + separator + string_VkAccessFlagBits((VkAccessFlagBits)(1 << i));
1992 separator = " | ";
1993 }
1994 }
1995 result = result + "]";
1996 }
1997 return result;
1998}
1999
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002000// AccessFlags MUST have 'required_bit' set, and may have one or more of 'optional_bits' set. If required_bit is zero, accessMask
2001// must have at least one of 'optional_bits' set
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002002// TODO: Add tracking to ensure that at least one barrier has been set for these layout transitions
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002003static bool ValidateMaskBits(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer, const VkAccessFlags &accessMask,
2004 const VkImageLayout &layout, VkAccessFlags required_bit, VkAccessFlags optional_bits,
2005 const char *type) {
2006 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2007 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002008
2009 if ((accessMask & required_bit) || (!required_bit && (accessMask & optional_bits))) {
2010 if (accessMask & ~(required_bit | optional_bits)) {
2011 // TODO: Verify against Valid Use
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002012 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2013 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002014 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
2015 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002016 }
2017 } else {
2018 if (!required_bit) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002019 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2020 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002021 "%s AccessMask %d %s must contain at least one of access bits %d "
2022 "%s when layout is %s, unless the app has previously added a "
2023 "barrier for this transition.",
2024 type, accessMask, string_VkAccessFlags(accessMask).c_str(), optional_bits,
2025 string_VkAccessFlags(optional_bits).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002026 } else {
2027 std::string opt_bits;
2028 if (optional_bits != 0) {
2029 std::stringstream ss;
2030 ss << optional_bits;
2031 opt_bits = "and may have optional bits " + ss.str() + ' ' + string_VkAccessFlags(optional_bits);
2032 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002033 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2034 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002035 "%s AccessMask %d %s must have required access bit %d %s %s when "
2036 "layout is %s, unless the app has previously added a barrier for "
2037 "this transition.",
2038 type, accessMask, string_VkAccessFlags(accessMask).c_str(), required_bit,
2039 string_VkAccessFlags(required_bit).c_str(), opt_bits.c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002040 }
2041 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002042 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002043}
2044
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002045bool ValidateMaskBitsFromLayouts(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer,
2046 const VkAccessFlags &accessMask, const VkImageLayout &layout, const char *type) {
2047 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002048
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002049 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002050 switch (layout) {
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002051 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: {
2052 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
2053 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2054 break;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002055 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002056 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: {
2057 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
2058 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2059 break;
2060 }
2061 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: {
2062 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_WRITE_BIT, 0, type);
2063 break;
2064 }
2065 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL: {
2066 skip |= ValidateMaskBits(
2067 device_data, cmdBuffer, accessMask, layout, 0,
2068 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
2069 type);
2070 break;
2071 }
2072 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: {
2073 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, 0,
2074 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
2075 break;
2076 }
2077 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: {
2078 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_READ_BIT, 0, type);
2079 break;
2080 }
2081 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: {
2082 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_MEMORY_READ_BIT, 0, type);
2083 break;
2084 }
2085 case VK_IMAGE_LAYOUT_UNDEFINED: {
2086 if (accessMask != 0) {
2087 // TODO: Verify against Valid Use section spec
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002088 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2089 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002090 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
2091 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
2092 }
2093 break;
2094 }
2095 case VK_IMAGE_LAYOUT_GENERAL:
2096 default: { break; }
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002097 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002098 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002099}
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002100
2101// ValidateLayoutVsAttachmentDescription is a general function where we can validate various state associated with the
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002102// VkAttachmentDescription structs that are used by the sub-passes of a renderpass. Initial check is to make sure that READ_ONLY
2103// layout attachments don't have CLEAR as their loadOp.
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002104bool ValidateLayoutVsAttachmentDescription(const debug_report_data *report_data, const VkImageLayout first_layout,
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002105 const uint32_t attachment, const VkAttachmentDescription &attachment_description) {
2106 bool skip = false;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002107 // Verify that initial loadOp on READ_ONLY attachments is not CLEAR
2108 if (attachment_description.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2109 if ((first_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) ||
2110 (first_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)) {
Mark Lobodzinskieb9e73f2017-04-13 10:06:48 -06002111 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2112 VALIDATION_ERROR_02351, "DS", "Cannot clear attachment %d with invalid first layout %s. %s", attachment,
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002113 string_VkImageLayout(first_layout), validation_error_map[VALIDATION_ERROR_02351]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002114 }
2115 }
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002116 return skip;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002117}
2118
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002119bool ValidateLayouts(core_validation::layer_data *device_data, VkDevice device, const VkRenderPassCreateInfo *pCreateInfo) {
2120 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002121 bool skip = false;
2122
2123 // Track when we're observing the first use of an attachment
2124 std::vector<bool> attach_first_use(pCreateInfo->attachmentCount, true);
2125 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
2126 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
2127 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
2128 auto attach_index = subpass.pColorAttachments[j].attachment;
2129 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2130
2131 switch (subpass.pColorAttachments[j].layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002132 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
2133 // This is ideal.
2134 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002135
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002136 case VK_IMAGE_LAYOUT_GENERAL:
2137 // May not be optimal; TODO: reconsider this warning based on other constraints?
2138 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2139 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2140 "Layout for color attachment is GENERAL but should be COLOR_ATTACHMENT_OPTIMAL.");
2141 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002142
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002143 default:
2144 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2145 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2146 "Layout for color attachment is %s but can only be COLOR_ATTACHMENT_OPTIMAL or GENERAL.",
2147 string_VkImageLayout(subpass.pColorAttachments[j].layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002148 }
2149
2150 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002151 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pColorAttachments[j].layout, attach_index,
2152 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002153 }
2154 attach_first_use[attach_index] = false;
2155 }
2156 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
2157 switch (subpass.pDepthStencilAttachment->layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002158 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
2159 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2160 // These are ideal.
2161 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002162
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002163 case VK_IMAGE_LAYOUT_GENERAL:
2164 // May not be optimal; TODO: reconsider this warning based on other constraints? GENERAL can be better than
2165 // doing a bunch of transitions.
2166 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2167 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2168 "GENERAL layout for depth attachment may not give optimal performance.");
2169 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002170
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002171 default:
2172 // No other layouts are acceptable
2173 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2174 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2175 "Layout for depth attachment is %s but can only be DEPTH_STENCIL_ATTACHMENT_OPTIMAL, "
2176 "DEPTH_STENCIL_READ_ONLY_OPTIMAL or GENERAL.",
2177 string_VkImageLayout(subpass.pDepthStencilAttachment->layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002178 }
2179
2180 auto attach_index = subpass.pDepthStencilAttachment->attachment;
2181 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002182 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pDepthStencilAttachment->layout, attach_index,
2183 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002184 }
2185 attach_first_use[attach_index] = false;
2186 }
2187 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
2188 auto attach_index = subpass.pInputAttachments[j].attachment;
2189 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2190
2191 switch (subpass.pInputAttachments[j].layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002192 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2193 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
2194 // These are ideal.
2195 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002196
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002197 case VK_IMAGE_LAYOUT_GENERAL:
2198 // May not be optimal. TODO: reconsider this warning based on other constraints.
2199 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2200 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2201 "Layout for input attachment is GENERAL but should be READ_ONLY_OPTIMAL.");
2202 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002203
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002204 default:
2205 // No other layouts are acceptable
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002206 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2207 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002208 "Layout for input attachment is %s but can only be READ_ONLY_OPTIMAL or GENERAL.",
2209 string_VkImageLayout(subpass.pInputAttachments[j].layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002210 }
2211
2212 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002213 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pInputAttachments[j].layout, attach_index,
2214 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002215 }
2216 attach_first_use[attach_index] = false;
2217 }
2218 }
2219 return skip;
2220}
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002221
2222// For any image objects that overlap mapped memory, verify that their layouts are PREINIT or GENERAL
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002223bool ValidateMapImageLayouts(core_validation::layer_data *device_data, VkDevice device, DEVICE_MEM_INFO const *mem_info,
2224 VkDeviceSize offset, VkDeviceSize end_offset) {
2225 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2226 bool skip = false;
2227 // Iterate over all bound image ranges and verify that for any that overlap the map ranges, the layouts are
2228 // VK_IMAGE_LAYOUT_PREINITIALIZED or VK_IMAGE_LAYOUT_GENERAL
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002229 // TODO : This can be optimized if we store ranges based on starting address and early exit when we pass our range
2230 for (auto image_handle : mem_info->bound_images) {
2231 auto img_it = mem_info->bound_ranges.find(image_handle);
2232 if (img_it != mem_info->bound_ranges.end()) {
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002233 if (rangesIntersect(device_data, &img_it->second, offset, end_offset)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002234 std::vector<VkImageLayout> layouts;
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002235 if (FindLayouts(device_data, VkImage(image_handle), layouts)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002236 for (auto layout : layouts) {
2237 if (layout != VK_IMAGE_LAYOUT_PREINITIALIZED && layout != VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002238 skip |= log_msg(
2239 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
2240 reinterpret_cast<const uint64_t &>(mem_info->mem), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2241 "Mapping an image with layout %s can result in undefined behavior if this memory is "
2242 "used by the device. Only GENERAL or PREINITIALIZED should be used.",
2243 string_VkImageLayout(layout));
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002244 }
2245 }
2246 }
2247 }
2248 }
2249 }
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002250 return skip;
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002251}
Mark Lobodzinski96210742017-02-09 10:33:46 -07002252
2253// Helper function to validate correct usage bits set for buffers or images. Verify that (actual & desired) flags != 0 or, if strict
2254// is true, verify that (actual & desired) flags == desired
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002255static 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 -06002256 VulkanObjectType obj_type, int32_t const msgCode, char const *func_name, char const *usage_str) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002257 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002258
2259 bool correct_usage = false;
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002260 bool skip = false;
Mark Lobodzinski33826372017-04-13 11:10:11 -06002261 const char *type_str = object_string[obj_type];
Mark Lobodzinski96210742017-02-09 10:33:46 -07002262 if (strict) {
2263 correct_usage = ((actual & desired) == desired);
2264 } else {
2265 correct_usage = ((actual & desired) != 0);
2266 }
2267 if (!correct_usage) {
2268 if (msgCode == -1) {
2269 // TODO: Fix callers with msgCode == -1 to use correct validation checks.
Mark Lobodzinskic51dbb72017-04-13 14:25:39 -06002270 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 -06002271 MEMTRACK_INVALID_USAGE_FLAG, "MEM",
2272 "Invalid usage flag for %s 0x%" PRIxLEAST64
2273 " used by %s. In this case, %s should have %s set during creation.",
2274 type_str, obj_handle, func_name, type_str, usage_str);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002275 } else {
2276 const char *valid_usage = (msgCode == -1) ? "" : validation_error_map[msgCode];
Mark Lobodzinski33826372017-04-13 11:10:11 -06002277 skip = log_msg(
Mark Lobodzinskic51dbb72017-04-13 14:25:39 -06002278 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 -06002279 "Invalid usage flag for %s 0x%" PRIxLEAST64 " used by %s. In this case, %s should have %s set during creation. %s",
2280 type_str, obj_handle, func_name, type_str, usage_str, valid_usage);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002281 }
2282 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002283 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002284}
2285
2286// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2287// where an error will be flagged if usage is not correct
Chris Forbes8fdba302017-04-24 18:34:28 -07002288bool ValidateImageUsageFlags(layer_data *device_data, IMAGE_STATE const *image_state, VkFlags desired, bool strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002289 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002290 return validate_usage_flags(device_data, image_state->createInfo.usage, desired, strict,
Mark Lobodzinski33826372017-04-13 11:10:11 -06002291 reinterpret_cast<const uint64_t &>(image_state->image), kVulkanObjectTypeImage, msgCode, func_name,
2292 usage_string);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002293}
2294
2295// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2296// where an error will be flagged if usage is not correct
Chris Forbes8fdba302017-04-24 18:34:28 -07002297bool ValidateBufferUsageFlags(layer_data *device_data, BUFFER_STATE const *buffer_state, VkFlags desired, bool strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002298 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002299 return validate_usage_flags(device_data, buffer_state->createInfo.usage, desired, strict,
Mark Lobodzinski33826372017-04-13 11:10:11 -06002300 reinterpret_cast<const uint64_t &>(buffer_state->buffer), kVulkanObjectTypeBuffer, msgCode,
2301 func_name, usage_string);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002302}
2303
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002304bool PreCallValidateCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo) {
Mark Lobodzinski96210742017-02-09 10:33:46 -07002305 bool skip = false;
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002306 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2307
Mark Lobodzinski96210742017-02-09 10:33:46 -07002308 // TODO: Add check for VALIDATION_ERROR_00658
Mark Lobodzinski96210742017-02-09 10:33:46 -07002309 // TODO: Add check for VALIDATION_ERROR_00667
2310 // TODO: Add check for VALIDATION_ERROR_00668
2311 // TODO: Add check for VALIDATION_ERROR_00669
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002312
2313 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
2314 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2315 VALIDATION_ERROR_00666, "DS",
2316 "vkCreateBuffer(): the sparseBinding device feature is disabled: Buffers cannot be created with the "
2317 "VK_BUFFER_CREATE_SPARSE_BINDING_BIT set. %s",
2318 validation_error_map[VALIDATION_ERROR_00666]);
2319 }
Mark Lobodzinskiaf355062017-03-13 09:35:01 -06002320
2321 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyBuffer)) {
2322 skip |=
2323 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2324 DRAWSTATE_INVALID_FEATURE, "DS",
2325 "vkCreateBuffer(): the sparseResidencyBuffer device feature is disabled: Buffers cannot be created with the "
2326 "VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT set.");
2327 }
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -06002328
2329 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
2330 skip |=
2331 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2332 DRAWSTATE_INVALID_FEATURE, "DS",
2333 "vkCreateBuffer(): the sparseResidencyAliased device feature is disabled: Buffers cannot be created with the "
2334 "VK_BUFFER_CREATE_SPARSE_ALIASED_BIT set.");
2335 }
Mark Lobodzinski96210742017-02-09 10:33:46 -07002336 return skip;
2337}
2338
2339void PostCallRecordCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo, VkBuffer *pBuffer) {
2340 // TODO : This doesn't create deep copy of pQueueFamilyIndices so need to fix that if/when we want that data to be valid
2341 GetBufferMap(device_data)
2342 ->insert(std::make_pair(*pBuffer, std::unique_ptr<BUFFER_STATE>(new BUFFER_STATE(*pBuffer, pCreateInfo))));
2343}
2344
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002345bool PreCallValidateCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo) {
2346 bool skip = false;
2347 BUFFER_STATE *buffer_state = GetBufferState(device_data, pCreateInfo->buffer);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002348 // If this isn't a sparse buffer, it needs to have memory backing it at CreateBufferView time
2349 if (buffer_state) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002350 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCreateBufferView()", VALIDATION_ERROR_02522);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002351 // In order to create a valid buffer view, the buffer must have been created with at least one of the following flags:
2352 // UNIFORM_TEXEL_BUFFER_BIT or STORAGE_TEXEL_BUFFER_BIT
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002353 skip |= ValidateBufferUsageFlags(
2354 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 -07002355 VALIDATION_ERROR_00694, "vkCreateBufferView()", "VK_BUFFER_USAGE_[STORAGE|UNIFORM]_TEXEL_BUFFER_BIT");
2356 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002357 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002358}
2359
2360void PostCallRecordCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo, VkBufferView *pView) {
2361 (*GetBufferViewMap(device_data))[*pView] = std::unique_ptr<BUFFER_VIEW_STATE>(new BUFFER_VIEW_STATE(*pView, pCreateInfo));
2362}
Mark Lobodzinski602de982017-02-09 11:01:33 -07002363
2364// For the given format verify that the aspect masks make sense
2365bool ValidateImageAspectMask(layer_data *device_data, VkImage image, VkFormat format, VkImageAspectFlags aspect_mask,
2366 const char *func_name) {
2367 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2368 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06002369 if (FormatIsColor(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002370 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
2371 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2372 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2373 "%s: Color image formats must have the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2374 validation_error_map[VALIDATION_ERROR_00741]);
2375 } else if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != aspect_mask) {
2376 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2377 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2378 "%s: Color image formats must have ONLY the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2379 validation_error_map[VALIDATION_ERROR_00741]);
2380 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002381 } else if (FormatIsDepthAndStencil(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002382 if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) == 0) {
2383 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2384 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2385 "%s: Depth/stencil image formats must have "
2386 "at least one of VK_IMAGE_ASPECT_DEPTH_BIT "
2387 "and VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2388 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2389 } else if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) != aspect_mask) {
2390 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2391 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2392 "%s: Combination depth/stencil image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT and "
2393 "VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2394 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2395 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002396 } else if (FormatIsDepthOnly(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002397 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) {
2398 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2399 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2400 "%s: Depth-only image formats must have the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2401 validation_error_map[VALIDATION_ERROR_00741]);
2402 } else if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != aspect_mask) {
2403 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2404 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2405 "%s: Depth-only image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2406 validation_error_map[VALIDATION_ERROR_00741]);
2407 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002408 } else if (FormatIsStencilOnly(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002409 if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT) {
2410 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2411 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2412 "%s: Stencil-only image formats must have the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2413 validation_error_map[VALIDATION_ERROR_00741]);
2414 } else if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != aspect_mask) {
2415 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2416 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2417 "%s: Stencil-only image formats can have only the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2418 validation_error_map[VALIDATION_ERROR_00741]);
2419 }
2420 }
2421 return skip;
2422}
2423
2424bool ValidateImageSubrangeLevelLayerCounts(layer_data *device_data, const VkImageSubresourceRange &subresourceRange,
2425 const char *func_name) {
2426 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2427 bool skip = false;
2428 if (subresourceRange.levelCount == 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002429 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski602de982017-02-09 11:01:33 -07002430 VALIDATION_ERROR_00768, "IMAGE", "%s called with 0 in subresourceRange.levelCount. %s", func_name,
2431 validation_error_map[VALIDATION_ERROR_00768]);
2432 }
2433 if (subresourceRange.layerCount == 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002434 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 -07002435 VALIDATION_ERROR_00769, "IMAGE", "%s called with 0 in subresourceRange.layerCount. %s", func_name,
2436 validation_error_map[VALIDATION_ERROR_00769]);
2437 }
2438 return skip;
2439}
2440
2441bool PreCallValidateCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info) {
2442 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2443 bool skip = false;
2444 IMAGE_STATE *image_state = GetImageState(device_data, create_info->image);
2445 if (image_state) {
2446 skip |= ValidateImageUsageFlags(
2447 device_data, image_state,
2448 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
2449 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
2450 false, -1, "vkCreateImageView()",
2451 "VK_IMAGE_USAGE_[SAMPLED|STORAGE|COLOR_ATTACHMENT|DEPTH_STENCIL_ATTACHMENT|INPUT_ATTACHMENT]_BIT");
2452 // If this isn't a sparse image, it needs to have memory backing it at CreateImageView time
2453 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCreateImageView()", VALIDATION_ERROR_02524);
2454 // Checks imported from image layer
Jeremy Hayesf6bfa6b2017-04-04 15:05:52 -06002455 if ((create_info->subresourceRange.baseMipLevel + create_info->subresourceRange.levelCount) >
2456 image_state->createInfo.mipLevels) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002457 std::stringstream ss;
2458 ss << "vkCreateImageView called with baseMipLevel " << create_info->subresourceRange.baseMipLevel << " for image "
2459 << create_info->image << " that only has " << image_state->createInfo.mipLevels << " mip levels.";
2460 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002461 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 -07002462 VALIDATION_ERROR_00768, "IMAGE", "%s %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_00768]);
2463 }
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002464 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
2465 if (create_info->subresourceRange.baseArrayLayer >= image_state->createInfo.arrayLayers) {
2466 std::stringstream ss;
2467 ss << "vkCreateImageView called with baseArrayLayer " << create_info->subresourceRange.baseArrayLayer
2468 << " for image " << create_info->image << " that only has " << image_state->createInfo.arrayLayers
2469 << " array layers.";
2470 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2471 VALIDATION_ERROR_00769, "IMAGE", "%s %s", ss.str().c_str(),
2472 validation_error_map[VALIDATION_ERROR_00769]);
2473 }
Mark Lobodzinski602de982017-02-09 11:01:33 -07002474 }
2475 // TODO: Need new valid usage language for levelCount == 0 & layerCount == 0
2476 skip |= ValidateImageSubrangeLevelLayerCounts(device_data, create_info->subresourceRange, "vkCreateImageView()");
2477
2478 VkImageCreateFlags image_flags = image_state->createInfo.flags;
2479 VkFormat image_format = image_state->createInfo.format;
2480 VkFormat view_format = create_info->format;
2481 VkImageAspectFlags aspect_mask = create_info->subresourceRange.aspectMask;
2482
2483 // Validate VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT state
2484 if (image_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
2485 // 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 -06002486 if (FormatCompatibilityClass(image_format) != FormatCompatibilityClass(view_format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002487 std::stringstream ss;
2488 ss << "vkCreateImageView(): ImageView format " << string_VkFormat(view_format)
2489 << " is not in the same format compatibility class as image (" << (uint64_t)create_info->image << ") format "
2490 << string_VkFormat(image_format) << ". Images created with the VK_IMAGE_CREATE_MUTABLE_FORMAT BIT "
2491 << "can support ImageViews with differing formats but they must be in the same compatibility class.";
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002492 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 -07002493 VALIDATION_ERROR_02171, "IMAGE", "%s %s", ss.str().c_str(),
2494 validation_error_map[VALIDATION_ERROR_02171]);
2495 }
2496 } else {
2497 // Format MUST be IDENTICAL to the format the image was created with
2498 if (image_format != view_format) {
2499 std::stringstream ss;
2500 ss << "vkCreateImageView() format " << string_VkFormat(view_format) << " differs from image "
2501 << (uint64_t)create_info->image << " format " << string_VkFormat(image_format)
2502 << ". Formats MUST be IDENTICAL unless VK_IMAGE_CREATE_MUTABLE_FORMAT BIT was set on image creation.";
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002503 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 -07002504 VALIDATION_ERROR_02172, "IMAGE", "%s %s", ss.str().c_str(),
2505 validation_error_map[VALIDATION_ERROR_02172]);
2506 }
2507 }
2508
2509 // Validate correct image aspect bits for desired formats and format consistency
2510 skip |= ValidateImageAspectMask(device_data, image_state->image, image_format, aspect_mask, "vkCreateImageView()");
2511 }
2512 return skip;
2513}
2514
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -07002515void PostCallRecordCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info, VkImageView view) {
2516 auto image_view_map = GetImageViewMap(device_data);
2517 (*image_view_map)[view] = std::unique_ptr<IMAGE_VIEW_STATE>(new IMAGE_VIEW_STATE(view, create_info));
2518
2519 auto image_state = GetImageState(device_data, create_info->image);
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002520 auto &sub_res_range = (*image_view_map)[view].get()->create_info.subresourceRange;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -06002521 sub_res_range.levelCount = ResolveRemainingLevels(&sub_res_range, image_state->createInfo.mipLevels);
2522 sub_res_range.layerCount = ResolveRemainingLayers(&sub_res_range, image_state->createInfo.arrayLayers);
Mark Lobodzinski602de982017-02-09 11:01:33 -07002523}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002524
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002525bool PreCallValidateCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2526 BUFFER_STATE *dst_buffer_state) {
2527 bool skip = false;
2528 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02531);
2529 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02532);
2530 // Validate that SRC & DST buffers have correct usage flags set
2531 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01164,
2532 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
2533 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01165,
2534 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07002535 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyBuffer()",
2536 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01171);
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002537 skip |= ValidateCmd(device_data, cb_node, CMD_COPYBUFFER, "vkCmdCopyBuffer()");
2538 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBuffer()", VALIDATION_ERROR_01172);
2539 return skip;
2540}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002541
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002542void PreCallRecordCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2543 BUFFER_STATE *dst_buffer_state) {
2544 // Update bindings between buffers and cmd buffer
2545 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
2546 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
2547
2548 std::function<bool()> function = [=]() {
2549 return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBuffer()");
2550 };
2551 cb_node->validate_functions.push_back(function);
2552 function = [=]() {
2553 SetBufferMemoryValid(device_data, dst_buffer_state, true);
2554 return false;
2555 };
2556 cb_node->validate_functions.push_back(function);
2557 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFER);
2558}
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002559
2560static bool validateIdleBuffer(layer_data *device_data, VkBuffer buffer) {
2561 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2562 bool skip = false;
2563 auto buffer_state = GetBufferState(device_data, buffer);
2564 if (!buffer_state) {
2565 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2566 __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
2567 "Cannot free buffer 0x%" PRIxLEAST64 " that has not been allocated.", (uint64_t)(buffer));
2568 } else {
2569 if (buffer_state->in_use.load()) {
2570 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2571 __LINE__, VALIDATION_ERROR_00676, "DS",
2572 "Cannot free buffer 0x%" PRIxLEAST64 " that is in use by a command buffer. %s", (uint64_t)(buffer),
2573 validation_error_map[VALIDATION_ERROR_00676]);
2574 }
2575 }
2576 return skip;
2577}
2578
2579bool PreCallValidateDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE **image_view_state,
2580 VK_OBJECT *obj_struct) {
2581 *image_view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski33826372017-04-13 11:10:11 -06002582 *obj_struct = {reinterpret_cast<uint64_t &>(image_view), kVulkanObjectTypeImageView};
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002583 if (GetDisables(device_data)->destroy_image_view) return false;
2584 bool skip = false;
2585 if (*image_view_state) {
2586 skip |= ValidateObjectNotInUse(device_data, *image_view_state, *obj_struct, VALIDATION_ERROR_00776);
2587 }
2588 return skip;
2589}
2590
2591void PostCallRecordDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE *image_view_state,
2592 VK_OBJECT obj_struct) {
2593 // Any bound cmd buffers are now invalid
2594 invalidateCommandBuffers(device_data, image_view_state->cb_bindings, obj_struct);
2595 (*GetImageViewMap(device_data)).erase(image_view);
2596}
2597
2598bool PreCallValidateDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE **buffer_state, VK_OBJECT *obj_struct) {
2599 *buffer_state = GetBufferState(device_data, buffer);
Mark Lobodzinski33826372017-04-13 11:10:11 -06002600 *obj_struct = {reinterpret_cast<uint64_t &>(buffer), kVulkanObjectTypeBuffer };
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002601 if (GetDisables(device_data)->destroy_buffer) return false;
2602 bool skip = false;
2603 if (*buffer_state) {
2604 skip |= validateIdleBuffer(device_data, buffer);
2605 }
2606 return skip;
2607}
2608
2609void PostCallRecordDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE *buffer_state, VK_OBJECT obj_struct) {
2610 invalidateCommandBuffers(device_data, buffer_state->cb_bindings, obj_struct);
2611 for (auto mem_binding : buffer_state->GetBoundMemory()) {
2612 auto mem_info = GetMemObjInfo(device_data, mem_binding);
2613 if (mem_info) {
2614 core_validation::RemoveBufferMemoryRange(reinterpret_cast<uint64_t &>(buffer), mem_info);
2615 }
2616 }
Mark Lobodzinski33826372017-04-13 11:10:11 -06002617 ClearMemoryObjectBindings(device_data, reinterpret_cast<uint64_t &>(buffer), kVulkanObjectTypeBuffer);
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002618 GetBufferMap(device_data)->erase(buffer_state->buffer);
2619}
2620
2621bool PreCallValidateDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE **buffer_view_state,
2622 VK_OBJECT *obj_struct) {
2623 *buffer_view_state = GetBufferViewState(device_data, buffer_view);
Mark Lobodzinski33826372017-04-13 11:10:11 -06002624 *obj_struct = {reinterpret_cast<uint64_t &>(buffer_view), kVulkanObjectTypeBufferView };
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002625 if (GetDisables(device_data)->destroy_buffer_view) return false;
2626 bool skip = false;
2627 if (*buffer_view_state) {
2628 skip |= ValidateObjectNotInUse(device_data, *buffer_view_state, *obj_struct, VALIDATION_ERROR_00701);
2629 }
2630 return skip;
2631}
2632
2633void PostCallRecordDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE *buffer_view_state,
2634 VK_OBJECT obj_struct) {
2635 // Any bound cmd buffers are now invalid
2636 invalidateCommandBuffers(device_data, buffer_view_state->cb_bindings, obj_struct);
2637 GetBufferViewMap(device_data)->erase(buffer_view);
2638}
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002639
2640bool PreCallValidateCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2641 bool skip = false;
2642 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCmdFillBuffer()", VALIDATION_ERROR_02529);
Mike Schuchardt9c582402017-02-23 15:57:37 -07002643 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdFillBuffer()",
2644 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01141);
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002645 skip |= ValidateCmd(device_data, cb_node, CMD_FILLBUFFER, "vkCmdFillBuffer()");
2646 // Validate that DST buffer has correct usage flags set
2647 skip |= ValidateBufferUsageFlags(device_data, buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01137,
2648 "vkCmdFillBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
2649 skip |= insideRenderPass(device_data, cb_node, "vkCmdFillBuffer()", VALIDATION_ERROR_01142);
2650 return skip;
2651}
2652
2653void PreCallRecordCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2654 std::function<bool()> function = [=]() {
2655 SetBufferMemoryValid(device_data, buffer_state, true);
2656 return false;
2657 };
2658 cb_node->validate_functions.push_back(function);
2659 // Update bindings between buffer and cmd buffer
2660 AddCommandBufferBindingBuffer(device_data, cb_node, buffer_state);
2661 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_FILLBUFFER);
2662}
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002663
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002664bool ValidateBufferImageCopyData(const debug_report_data *report_data, uint32_t regionCount, const VkBufferImageCopy *pRegions,
2665 IMAGE_STATE *image_state, const char *function) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002666 bool skip = false;
2667
2668 for (uint32_t i = 0; i < regionCount; i++) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002669 if (image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) {
2670 if ((pRegions[i].imageOffset.y != 0) || (pRegions[i].imageExtent.height != 1)) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002671 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 -07002672 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01746, "IMAGE",
2673 "%s(): pRegion[%d] imageOffset.y is %d and imageExtent.height is %d. For 1D images these "
2674 "must be 0 and 1, respectively. %s",
2675 function, i, pRegions[i].imageOffset.y, pRegions[i].imageExtent.height,
2676 validation_error_map[VALIDATION_ERROR_01746]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002677 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002678 }
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002679
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002680 if ((image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) || (image_state->createInfo.imageType == VK_IMAGE_TYPE_2D)) {
2681 if ((pRegions[i].imageOffset.z != 0) || (pRegions[i].imageExtent.depth != 1)) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002682 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 -07002683 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01747, "IMAGE",
2684 "%s(): pRegion[%d] imageOffset.z is %d and imageExtent.depth is %d. For 1D and 2D images these "
2685 "must be 0 and 1, respectively. %s",
2686 function, i, pRegions[i].imageOffset.z, pRegions[i].imageExtent.depth,
2687 validation_error_map[VALIDATION_ERROR_01747]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002688 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002689 }
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002690
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002691 if (image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) {
2692 if ((0 != pRegions[i].imageSubresource.baseArrayLayer) || (1 != pRegions[i].imageSubresource.layerCount)) {
2693 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2694 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01281, "IMAGE",
2695 "%s(): pRegion[%d] imageSubresource.baseArrayLayer is %d and imageSubresource.layerCount is "
2696 "%d. For 3D images these must be 0 and 1, respectively. %s",
2697 function, i, pRegions[i].imageSubresource.baseArrayLayer, pRegions[i].imageSubresource.layerCount,
2698 validation_error_map[VALIDATION_ERROR_01281]);
2699 }
2700 }
2701
2702 // If the the calling command's VkImage parameter's format is not a depth/stencil format,
2703 // then bufferOffset must be a multiple of the calling command's VkImage parameter's texel size
Dave Houlton1d2022c2017-03-29 11:43:58 -06002704 auto texel_size = FormatSize(image_state->createInfo.format);
2705 if (!FormatIsDepthAndStencil(image_state->createInfo.format) &&
2706 SafeModulo(pRegions[i].bufferOffset, texel_size) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002707 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2708 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01263, "IMAGE",
2709 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64
2710 " must be a multiple of this format's texel size (" PRINTF_SIZE_T_SPECIFIER "). %s",
2711 function, i, pRegions[i].bufferOffset, texel_size, validation_error_map[VALIDATION_ERROR_01263]);
2712 }
2713
2714 // BufferOffset must be a multiple of 4
Dave Houlton1d2022c2017-03-29 11:43:58 -06002715 if (SafeModulo(pRegions[i].bufferOffset, 4) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002716 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2717 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01264, "IMAGE",
2718 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64 " must be a multiple of 4. %s", function, i,
2719 pRegions[i].bufferOffset, validation_error_map[VALIDATION_ERROR_01264]);
2720 }
2721
2722 // BufferRowLength must be 0, or greater than or equal to the width member of imageExtent
2723 if ((pRegions[i].bufferRowLength != 0) && (pRegions[i].bufferRowLength < pRegions[i].imageExtent.width)) {
2724 skip |= log_msg(
2725 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2726 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01265, "IMAGE",
2727 "%s(): pRegion[%d] bufferRowLength (%d) must be zero or greater-than-or-equal-to imageExtent.width (%d). %s",
2728 function, i, pRegions[i].bufferRowLength, pRegions[i].imageExtent.width,
2729 validation_error_map[VALIDATION_ERROR_01265]);
2730 }
2731
2732 // BufferImageHeight must be 0, or greater than or equal to the height member of imageExtent
2733 if ((pRegions[i].bufferImageHeight != 0) && (pRegions[i].bufferImageHeight < pRegions[i].imageExtent.height)) {
2734 skip |= log_msg(
2735 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2736 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01266, "IMAGE",
2737 "%s(): pRegion[%d] bufferImageHeight (%d) must be zero or greater-than-or-equal-to imageExtent.height (%d). %s",
2738 function, i, pRegions[i].bufferImageHeight, pRegions[i].imageExtent.height,
2739 validation_error_map[VALIDATION_ERROR_01266]);
2740 }
2741
2742 // subresource aspectMask must have exactly 1 bit set
2743 const int num_bits = sizeof(VkFlags) * CHAR_BIT;
2744 std::bitset<num_bits> aspect_mask_bits(pRegions[i].imageSubresource.aspectMask);
2745 if (aspect_mask_bits.count() != 1) {
2746 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2747 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01280, "IMAGE",
2748 "%s: aspectMasks for imageSubresource in each region must have only a single bit set. %s", function,
2749 validation_error_map[VALIDATION_ERROR_01280]);
2750 }
2751
2752 // image subresource aspect bit must match format
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002753 if (!VerifyAspectsPresent(pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002754 skip |= log_msg(
2755 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2756 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01279, "IMAGE",
2757 "%s(): pRegion[%d] subresource aspectMask 0x%x specifies aspects that are not present in image format 0x%x. %s",
2758 function, i, pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format,
2759 validation_error_map[VALIDATION_ERROR_01279]);
2760 }
2761
2762 // Checks that apply only to compressed images
2763 // TODO: there is a comment in ValidateCopyBufferImageTransferGranularityRequirements() in core_validation.cpp that
2764 // reserves a place for these compressed image checks. This block of code could move there once the image
2765 // stuff is moved into core validation.
Dave Houlton1d2022c2017-03-29 11:43:58 -06002766 if (FormatIsCompressed(image_state->createInfo.format)) {
2767 auto block_size = FormatCompressedTexelBlockExtent(image_state->createInfo.format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002768
2769 // BufferRowLength must be a multiple of block width
Dave Houlton1d2022c2017-03-29 11:43:58 -06002770 if (SafeModulo(pRegions[i].bufferRowLength, block_size.width) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002771 skip |= log_msg(
2772 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002773 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01271, "IMAGE",
2774 "%s(): pRegion[%d] bufferRowLength (%d) must be a multiple of the compressed image's texel width (%d). %s.",
2775 function, i, pRegions[i].bufferRowLength, block_size.width, validation_error_map[VALIDATION_ERROR_01271]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002776 }
2777
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002778 // BufferRowHeight must be a multiple of block height
Dave Houlton1d2022c2017-03-29 11:43:58 -06002779 if (SafeModulo(pRegions[i].bufferImageHeight, block_size.height) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002780 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 -07002781 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01272, "IMAGE",
2782 "%s(): pRegion[%d] bufferImageHeight (%d) must be a multiple of the compressed image's texel "
2783 "height (%d). %s.",
2784 function, i, pRegions[i].bufferImageHeight, block_size.height,
2785 validation_error_map[VALIDATION_ERROR_01272]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002786 }
2787
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002788 // image offsets must be multiples of block dimensions
Dave Houlton1d2022c2017-03-29 11:43:58 -06002789 if ((SafeModulo(pRegions[i].imageOffset.x, block_size.width) != 0) ||
2790 (SafeModulo(pRegions[i].imageOffset.y, block_size.height) != 0) ||
2791 (SafeModulo(pRegions[i].imageOffset.z, block_size.depth) != 0)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002792 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2793 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01273, "IMAGE",
2794 "%s(): pRegion[%d] imageOffset(x,y) (%d, %d) must be multiples of the compressed image's texel "
2795 "width & height (%d, %d). %s.",
2796 function, i, pRegions[i].imageOffset.x, pRegions[i].imageOffset.y, block_size.width,
2797 block_size.height, validation_error_map[VALIDATION_ERROR_01273]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002798 }
2799
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002800 // bufferOffset must be a multiple of block size (linear bytes)
Dave Houlton1d2022c2017-03-29 11:43:58 -06002801 size_t block_size_in_bytes = FormatSize(image_state->createInfo.format);
2802 if (SafeModulo(pRegions[i].bufferOffset, block_size_in_bytes) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002803 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2804 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01274, "IMAGE",
2805 "%s(): pRegion[%d] bufferOffset (0x%" PRIxLEAST64
2806 ") must be a multiple of the compressed image's texel block "
2807 "size (" PRINTF_SIZE_T_SPECIFIER "). %s.",
2808 function, i, pRegions[i].bufferOffset, block_size_in_bytes,
2809 validation_error_map[VALIDATION_ERROR_01274]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002810 }
Dave Houlton67e9b532017-03-02 17:00:10 -07002811
2812 // imageExtent width must be a multiple of block width, or extent+offset width must equal subresource width
Dave Houlton75967fc2017-03-06 17:21:16 -07002813 VkExtent3D mip_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
Dave Houlton1d2022c2017-03-29 11:43:58 -06002814 if ((SafeModulo(pRegions[i].imageExtent.width, block_size.width) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002815 (pRegions[i].imageExtent.width + pRegions[i].imageOffset.x != mip_extent.width)) {
2816 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2817 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01275, "IMAGE",
2818 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block width "
2819 "(%d), or when added to offset.x (%d) must equal the image subresource width (%d). %s.",
2820 function, i, pRegions[i].imageExtent.width, block_size.width, pRegions[i].imageOffset.x,
2821 mip_extent.width, validation_error_map[VALIDATION_ERROR_01275]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002822 }
2823
2824 // imageExtent height must be a multiple of block height, or extent+offset height must equal subresource height
Dave Houlton1d2022c2017-03-29 11:43:58 -06002825 if ((SafeModulo(pRegions[i].imageExtent.height, block_size.height) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002826 (pRegions[i].imageExtent.height + pRegions[i].imageOffset.y != mip_extent.height)) {
2827 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2828 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01276, "IMAGE",
2829 "%s(): pRegion[%d] extent height (%d) must be a multiple of the compressed texture block height "
2830 "(%d), or when added to offset.y (%d) must equal the image subresource height (%d). %s.",
2831 function, i, pRegions[i].imageExtent.height, block_size.height, pRegions[i].imageOffset.y,
2832 mip_extent.height, validation_error_map[VALIDATION_ERROR_01276]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002833 }
2834
2835 // imageExtent depth must be a multiple of block depth, or extent+offset depth must equal subresource depth
Dave Houlton1d2022c2017-03-29 11:43:58 -06002836 if ((SafeModulo(pRegions[i].imageExtent.depth, block_size.depth) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002837 (pRegions[i].imageExtent.depth + pRegions[i].imageOffset.z != mip_extent.depth)) {
2838 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2839 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01277, "IMAGE",
2840 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block depth "
2841 "(%d), or when added to offset.z (%d) must equal the image subresource depth (%d). %s.",
2842 function, i, pRegions[i].imageExtent.depth, block_size.depth, pRegions[i].imageOffset.z,
2843 mip_extent.depth, validation_error_map[VALIDATION_ERROR_01277]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002844 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002845 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002846 }
2847
2848 return skip;
2849}
2850
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002851static bool ValidateImageBounds(const debug_report_data *report_data, const IMAGE_STATE *image_state, const uint32_t regionCount,
2852 const VkBufferImageCopy *pRegions, const char *func_name, UNIQUE_VALIDATION_ERROR_CODE msg_code) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002853 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002854 const VkImageCreateInfo *image_info = &(image_state->createInfo);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002855
2856 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002857 VkExtent3D extent = pRegions[i].imageExtent;
2858 VkOffset3D offset = pRegions[i].imageOffset;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002859
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002860 if (IsExtentSizeZero(&extent)) // Warn on zero area subresource
2861 {
2862 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2863 (uint64_t)0, __LINE__, IMAGE_ZERO_AREA_SUBREGION, "IMAGE",
2864 "%s: pRegion[%d] imageExtent of {%1d, %1d, %1d} has zero area", func_name, i, extent.width,
2865 extent.height, extent.depth);
2866 }
2867
2868 VkExtent3D image_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
2869
2870 // 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 -06002871 if (FormatIsCompressed(image_info->format)) {
2872 auto block_extent = FormatCompressedTexelBlockExtent(image_info->format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002873 if (image_extent.width % block_extent.width) {
2874 image_extent.width += (block_extent.width - (image_extent.width % block_extent.width));
2875 }
2876 if (image_extent.height % block_extent.height) {
2877 image_extent.height += (block_extent.height - (image_extent.height % block_extent.height));
2878 }
2879 if (image_extent.depth % block_extent.depth) {
2880 image_extent.depth += (block_extent.depth - (image_extent.depth % block_extent.depth));
2881 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002882 }
2883
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002884 if (ExceedsBounds(&offset, &extent, &image_extent)) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002885 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 -07002886 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds image bounds. %s.", func_name, i,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002887 validation_error_map[msg_code]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002888 }
2889 }
2890
2891 return skip;
2892}
2893
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002894static inline bool ValidtateBufferBounds(const debug_report_data *report_data, IMAGE_STATE *image_state, BUFFER_STATE *buff_state,
2895 uint32_t regionCount, const VkBufferImageCopy *pRegions, const char *func_name,
2896 UNIQUE_VALIDATION_ERROR_CODE msg_code) {
2897 bool skip = false;
2898
2899 VkDeviceSize buffer_size = buff_state->createInfo.size;
2900
2901 for (uint32_t i = 0; i < regionCount; i++) {
2902 VkExtent3D copy_extent = pRegions[i].imageExtent;
2903
2904 VkDeviceSize buffer_width = (0 == pRegions[i].bufferRowLength ? copy_extent.width : pRegions[i].bufferRowLength);
2905 VkDeviceSize buffer_height = (0 == pRegions[i].bufferImageHeight ? copy_extent.height : pRegions[i].bufferImageHeight);
Dave Houlton1d2022c2017-03-29 11:43:58 -06002906 VkDeviceSize unit_size = FormatSize(image_state->createInfo.format); // size (bytes) of texel or block
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002907
Dave Houltonf3229d52017-02-21 15:59:08 -07002908 // Handle special buffer packing rules for specific depth/stencil formats
2909 if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Dave Houlton1d2022c2017-03-29 11:43:58 -06002910 unit_size = FormatSize(VK_FORMAT_S8_UINT);
Dave Houltonf3229d52017-02-21 15:59:08 -07002911 } else if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
2912 switch (image_state->createInfo.format) {
2913 case VK_FORMAT_D16_UNORM_S8_UINT:
Dave Houlton1d2022c2017-03-29 11:43:58 -06002914 unit_size = FormatSize(VK_FORMAT_D16_UNORM);
Dave Houltonf3229d52017-02-21 15:59:08 -07002915 break;
2916 case VK_FORMAT_D32_SFLOAT_S8_UINT:
Dave Houlton1d2022c2017-03-29 11:43:58 -06002917 unit_size = FormatSize(VK_FORMAT_D32_SFLOAT);
Dave Houltonf3229d52017-02-21 15:59:08 -07002918 break;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002919 case VK_FORMAT_X8_D24_UNORM_PACK32: // Fall through
Dave Houltonf3229d52017-02-21 15:59:08 -07002920 case VK_FORMAT_D24_UNORM_S8_UINT:
2921 unit_size = 4;
2922 break;
2923 default:
2924 break;
2925 }
2926 }
2927
Dave Houlton1d2022c2017-03-29 11:43:58 -06002928 if (FormatIsCompressed(image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002929 // Switch to texel block units, rounding up for any partially-used blocks
Dave Houlton1d2022c2017-03-29 11:43:58 -06002930 auto block_dim = FormatCompressedTexelBlockExtent(image_state->createInfo.format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002931 buffer_width = (buffer_width + block_dim.width - 1) / block_dim.width;
2932 buffer_height = (buffer_height + block_dim.height - 1) / block_dim.height;
2933
2934 copy_extent.width = (copy_extent.width + block_dim.width - 1) / block_dim.width;
2935 copy_extent.height = (copy_extent.height + block_dim.height - 1) / block_dim.height;
2936 copy_extent.depth = (copy_extent.depth + block_dim.depth - 1) / block_dim.depth;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002937 }
2938
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002939 // Either depth or layerCount may be greater than 1 (not both). This is the number of 'slices' to copy
2940 uint32_t z_copies = std::max(copy_extent.depth, pRegions[i].imageSubresource.layerCount);
2941 if (IsExtentSizeZero(&copy_extent) || (0 == z_copies)) {
2942 // TODO: Issure warning here? Already warned in ValidateImageBounds()...
2943 } else {
2944 // Calculate buffer offset of final copied byte, + 1.
2945 VkDeviceSize max_buffer_offset = (z_copies - 1) * buffer_height * buffer_width; // offset to slice
2946 max_buffer_offset += ((copy_extent.height - 1) * buffer_width) + copy_extent.width; // add row,col
2947 max_buffer_offset *= unit_size; // convert to bytes
2948 max_buffer_offset += pRegions[i].bufferOffset; // add initial offset (bytes)
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002949
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002950 if (buffer_size < max_buffer_offset) {
2951 skip |=
2952 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)0,
2953 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds buffer size of %" PRIu64 " bytes. %s.", func_name,
2954 i, buffer_size, validation_error_map[msg_code]);
2955 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002956 }
2957 }
2958
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002959 return skip;
2960}
2961
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002962bool PreCallValidateCmdCopyImageToBuffer(layer_data *device_data, VkImageLayout srcImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002963 IMAGE_STATE *src_image_state, BUFFER_STATE *dst_buffer_state, uint32_t regionCount,
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002964 const VkBufferImageCopy *pRegions, const char *func_name) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002965 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2966 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, src_image_state, "vkCmdCopyImageToBuffer");
2967
2968 // Validate command buffer state
2969 if (CB_RECORDING != cb_node->state) {
2970 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2971 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01258, "DS",
2972 "Cannot call vkCmdCopyImageToBuffer() on command buffer which is not in recording state. %s.",
2973 validation_error_map[VALIDATION_ERROR_01258]);
2974 } else {
2975 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYIMAGETOBUFFER);
2976 }
2977
2978 // Command pool must support graphics, compute, or transfer operations
2979 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
2980
2981 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
2982 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
2983 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2984 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01259, "DS",
2985 "Cannot call vkCmdCopyImageToBuffer() on a command buffer allocated from a pool without graphics, compute, "
2986 "or transfer capabilities. %s.",
2987 validation_error_map[VALIDATION_ERROR_01259]);
2988 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002989 skip |= ValidateImageBounds(report_data, src_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002990 VALIDATION_ERROR_01245);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002991 skip |= ValidtateBufferBounds(report_data, src_image_state, dst_buffer_state, regionCount, pRegions, "vkCmdCopyImageToBuffer()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002992 VALIDATION_ERROR_01246);
2993
2994 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyImageToBuffer(): srcImage",
2995 VALIDATION_ERROR_01249);
2996 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02537);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002997 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02538);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002998
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002999 // Validate that SRC image & DST buffer have correct usage flags set
3000 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01248,
3001 "vkCmdCopyImageToBuffer()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003002 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01252,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003003 "vkCmdCopyImageToBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003004 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01260);
Tobin Ehlisc8266452017-04-07 12:20:30 -06003005 bool hit_error = false;
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003006 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlisc8266452017-04-07 12:20:30 -06003007 skip |=
3008 VerifyImageLayout(device_data, cb_node, src_image_state, pRegions[i].imageSubresource, srcImageLayout,
3009 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01251, &hit_error);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003010 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, src_image_state, &pRegions[i], i,
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06003011 "vkCmdCopyImageToBuffer()");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003012 }
3013 return skip;
3014}
3015
3016void PreCallRecordCmdCopyImageToBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003017 BUFFER_STATE *dst_buffer_state, uint32_t region_count, const VkBufferImageCopy *regions,
3018 VkImageLayout src_image_layout) {
3019 // Make sure that all image slices are updated to correct layout
3020 for (uint32_t i = 0; i < region_count; ++i) {
3021 SetImageLayout(device_data, cb_node, src_image_state, regions[i].imageSubresource, src_image_layout);
3022 }
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003023 // Update bindings between buffer/image and cmd buffer
3024 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003025 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003026
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003027 std::function<bool()> function = [=]() {
3028 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImageToBuffer()");
3029 };
3030 cb_node->validate_functions.push_back(function);
3031 function = [=]() {
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003032 SetBufferMemoryValid(device_data, dst_buffer_state, true);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003033 return false;
3034 };
3035 cb_node->validate_functions.push_back(function);
3036
3037 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGETOBUFFER);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003038}
3039
3040bool PreCallValidateCmdCopyBufferToImage(layer_data *device_data, VkImageLayout dstImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003041 BUFFER_STATE *src_buffer_state, IMAGE_STATE *dst_image_state, uint32_t regionCount,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003042 const VkBufferImageCopy *pRegions, const char *func_name) {
3043 const debug_report_data *report_data = core_validation::GetReportData(device_data);
3044 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, dst_image_state, "vkCmdCopyBufferToImage");
3045
3046 // Validate command buffer state
3047 if (CB_RECORDING != cb_node->state) {
3048 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3049 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01240, "DS",
3050 "Cannot call vkCmdCopyBufferToImage() on command buffer which is not in recording state. %s.",
3051 validation_error_map[VALIDATION_ERROR_01240]);
3052 } else {
3053 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYBUFFERTOIMAGE);
3054 }
3055
3056 // Command pool must support graphics, compute, or transfer operations
3057 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
3058 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
3059 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
3060 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3061 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01241, "DS",
3062 "Cannot call vkCmdCopyBufferToImage() on a command buffer allocated from a pool without graphics, compute, "
3063 "or transfer capabilities. %s.",
3064 validation_error_map[VALIDATION_ERROR_01241]);
3065 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003066 skip |= ValidateImageBounds(report_data, dst_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003067 VALIDATION_ERROR_01228);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003068 skip |= ValidtateBufferBounds(report_data, dst_image_state, src_buffer_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003069 VALIDATION_ERROR_01227);
3070 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyBufferToImage(): dstImage",
3071 VALIDATION_ERROR_01232);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003072 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02535);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003073 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02536);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003074 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01230,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003075 "vkCmdCopyBufferToImage()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
3076 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01231,
3077 "vkCmdCopyBufferToImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003078 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01242);
Tobin Ehlisc8266452017-04-07 12:20:30 -06003079 bool hit_error = false;
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003080 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlisc8266452017-04-07 12:20:30 -06003081 skip |=
3082 VerifyImageLayout(device_data, cb_node, dst_image_state, pRegions[i].imageSubresource, dstImageLayout,
3083 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01234, &hit_error);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003084 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &pRegions[i], i,
3085 "vkCmdCopyBufferToImage()");
3086 }
3087 return skip;
3088}
3089
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003090void PreCallRecordCmdCopyBufferToImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003091 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkBufferImageCopy *regions,
3092 VkImageLayout dst_image_layout) {
3093 // Make sure that all image slices are updated to correct layout
3094 for (uint32_t i = 0; i < region_count; ++i) {
3095 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].imageSubresource, dst_image_layout);
3096 }
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003097 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003098 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003099 std::function<bool()> function = [=]() {
3100 SetImageMemoryValid(device_data, dst_image_state, true);
3101 return false;
3102 };
3103 cb_node->validate_functions.push_back(function);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003104 function = [=]() { return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBufferToImage()"); };
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003105 cb_node->validate_functions.push_back(function);
3106
3107 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFERTOIMAGE);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07003108}
Mike Weiblen672b58b2017-02-21 14:32:53 -07003109
3110bool PreCallValidateGetImageSubresourceLayout(layer_data *device_data, VkImage image, const VkImageSubresource *pSubresource) {
3111 const auto report_data = core_validation::GetReportData(device_data);
3112 bool skip = false;
3113 const VkImageAspectFlags sub_aspect = pSubresource->aspectMask;
3114
3115 // VU 00733: The aspectMask member of pSubresource must only have a single bit set
3116 const int num_bits = sizeof(sub_aspect) * CHAR_BIT;
3117 std::bitset<num_bits> aspect_mask_bits(sub_aspect);
3118 if (aspect_mask_bits.count() != 1) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003119 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3120 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00733, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003121 "vkGetImageSubresourceLayout(): VkImageSubresource.aspectMask must have exactly 1 bit set. %s",
3122 validation_error_map[VALIDATION_ERROR_00733]);
3123 }
3124
3125 IMAGE_STATE *image_entry = GetImageState(device_data, image);
3126 if (!image_entry) {
3127 return skip;
3128 }
3129
3130 // VU 00732: image must have been created with tiling equal to VK_IMAGE_TILING_LINEAR
3131 if (image_entry->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003132 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3133 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00732, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003134 "vkGetImageSubresourceLayout(): Image must have tiling of VK_IMAGE_TILING_LINEAR. %s",
3135 validation_error_map[VALIDATION_ERROR_00732]);
3136 }
3137
3138 // VU 00739: mipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
3139 if (pSubresource->mipLevel >= image_entry->createInfo.mipLevels) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003140 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3141 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00739, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003142 "vkGetImageSubresourceLayout(): pSubresource.mipLevel (%d) must be less than %d. %s",
3143 pSubresource->mipLevel, image_entry->createInfo.mipLevels, validation_error_map[VALIDATION_ERROR_00739]);
3144 }
3145
3146 // VU 00740: arrayLayer must be less than the arrayLayers specified in VkImageCreateInfo when the image was created
3147 if (pSubresource->arrayLayer >= image_entry->createInfo.arrayLayers) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003148 skip |=
3149 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3150 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00740, "IMAGE",
3151 "vkGetImageSubresourceLayout(): pSubresource.arrayLayer (%d) must be less than %d. %s",
3152 pSubresource->arrayLayer, image_entry->createInfo.arrayLayers, validation_error_map[VALIDATION_ERROR_00740]);
Mike Weiblen672b58b2017-02-21 14:32:53 -07003153 }
3154
3155 // VU 00741: subresource's aspect must be compatible with image's format.
3156 const VkFormat img_format = image_entry->createInfo.format;
Dave Houlton1d2022c2017-03-29 11:43:58 -06003157 if (FormatIsColor(img_format)) {
Mike Weiblen672b58b2017-02-21 14:32:53 -07003158 if (sub_aspect != VK_IMAGE_ASPECT_COLOR_BIT) {
3159 skip |= log_msg(
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003160 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3161 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003162 "vkGetImageSubresourceLayout(): For color formats, VkImageSubresource.aspectMask must be VK_IMAGE_ASPECT_COLOR. %s",
3163 validation_error_map[VALIDATION_ERROR_00741]);
3164 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06003165 } else if (FormatIsDepthOrStencil(img_format)) {
Mike Weiblen672b58b2017-02-21 14:32:53 -07003166 if ((sub_aspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (sub_aspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003167 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3168 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003169 "vkGetImageSubresourceLayout(): For depth/stencil formats, VkImageSubresource.aspectMask must be "
3170 "either VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT. %s",
3171 validation_error_map[VALIDATION_ERROR_00741]);
3172 }
3173 }
3174 return skip;
3175}