blob: 3fd4e9abc9dca6c46a1998db0edabf2f15f32a8d [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
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -0600475 auto image_state = GetImageState(device_data, img_barrier->image);
476 if (image_state) {
477 VkImageUsageFlags usage_flags = image_state->createInfo.usage;
478 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, false, usage_flags, func_name);
479 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, true, usage_flags, func_name);
480
481 // Make sure layout is able to be transitioned, currently only presented shared presentable images are locked
482 if (image_state->layout_locked) {
483 // TODO: Add unique id for error when available
484 skip |= log_msg(core_validation::GetReportData(device_data), VK_DEBUG_REPORT_ERROR_BIT_EXT,
485 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, 0, "DS",
486 "Attempting to transition shared presentable image 0x%" PRIxLEAST64
487 " from layout %s to layout %s, but image has already been presented and cannot have its layout transitioned.",
488 reinterpret_cast<const uint64_t &>(img_barrier->image), string_VkImageLayout(img_barrier->oldLayout),
489 string_VkImageLayout(img_barrier->newLayout));
490 }
491 }
492
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600493 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, img_barrier->image)->createInfo);
Tobin Ehlis7ee9cbd2017-04-26 16:51:48 -0600494 // For a Depth/Stencil image both aspects MUST be set
495 if (FormatIsDepthAndStencil(image_create_info->format)) {
496 auto const aspect_mask = img_barrier->subresourceRange.aspectMask;
497 auto const ds_mask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
498 if ((aspect_mask & ds_mask) != (ds_mask)) {
499 skip |=
500 log_msg(core_validation::GetReportData(device_data), VK_DEBUG_REPORT_ERROR_BIT_EXT,
501 VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, reinterpret_cast<const uint64_t &>(img_barrier->image), __LINE__,
502 VALIDATION_ERROR_00302, "DS",
503 "%s: Image barrier 0x%p references image 0x%" PRIx64
504 " of format %s that must have the depth and stencil aspects set, but its "
505 "aspectMask is 0x%" PRIx32 ". %s",
506 func_name, img_barrier, reinterpret_cast<const uint64_t &>(img_barrier->image),
507 string_VkFormat(image_create_info->format), aspect_mask, validation_error_map[VALIDATION_ERROR_00302]);
508 }
509 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600510 uint32_t level_count = ResolveRemainingLevels(&img_barrier->subresourceRange, image_create_info->mipLevels);
511 uint32_t layer_count = ResolveRemainingLayers(&img_barrier->subresourceRange, image_create_info->arrayLayers);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700512
Mike Weiblen62d08a32017-03-07 22:18:27 -0700513 for (uint32_t j = 0; j < level_count; j++) {
514 uint32_t level = img_barrier->subresourceRange.baseMipLevel + j;
515 for (uint32_t k = 0; k < layer_count; k++) {
516 uint32_t layer = img_barrier->subresourceRange.baseArrayLayer + k;
517 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
518 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
519 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
520 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700521 }
522 }
523 }
524 return skip;
525}
526
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700527void TransitionImageLayouts(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t memBarrierCount,
528 const VkImageMemoryBarrier *pImgMemBarriers) {
529 GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700530
531 for (uint32_t i = 0; i < memBarrierCount; ++i) {
532 auto mem_barrier = &pImgMemBarriers[i];
533 if (!mem_barrier) continue;
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700534
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600535 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, mem_barrier->image)->createInfo);
536 uint32_t level_count = ResolveRemainingLevels(&mem_barrier->subresourceRange, image_create_info->mipLevels);
537 uint32_t layer_count = ResolveRemainingLayers(&mem_barrier->subresourceRange, image_create_info->arrayLayers);
538
539 for (uint32_t j = 0; j < level_count; j++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700540 uint32_t level = mem_barrier->subresourceRange.baseMipLevel + j;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600541 for (uint32_t k = 0; k < layer_count; k++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700542 uint32_t layer = mem_barrier->subresourceRange.baseArrayLayer + k;
543 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
544 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
545 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
546 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
547 }
548 }
549 }
550}
551
Tobin Ehlisc8266452017-04-07 12:20:30 -0600552bool VerifyImageLayout(layer_data const *device_data, GLOBAL_CB_NODE const *cb_node, IMAGE_STATE *image_state,
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600553 VkImageSubresourceLayers subLayers, VkImageLayout explicit_layout, VkImageLayout optimal_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600554 const char *caller, UNIQUE_VALIDATION_ERROR_CODE msg_code, bool *error) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700555 const auto report_data = core_validation::GetReportData(device_data);
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600556 const auto image = image_state->image;
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600557 bool skip = false;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700558
559 for (uint32_t i = 0; i < subLayers.layerCount; ++i) {
560 uint32_t layer = i + subLayers.baseArrayLayer;
561 VkImageSubresource sub = {subLayers.aspectMask, subLayers.mipLevel, layer};
562 IMAGE_CMD_BUF_LAYOUT_NODE node;
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600563 if (FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
564 if (node.layout != explicit_layout) {
Tobin Ehlisc8266452017-04-07 12:20:30 -0600565 *error = true;
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600566 // TODO: Improve log message in the next pass
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600567 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
568 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
569 "%s: Cannot use image 0x%" PRIxLEAST64
570 " with specific layout %s that doesn't match the actual current layout %s.",
571 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
572 string_VkImageLayout(node.layout));
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600573 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700574 }
575 }
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600576 // If optimal_layout is not UNDEFINED, check that layout matches optimal for this case
577 if ((VK_IMAGE_LAYOUT_UNDEFINED != optimal_layout) && (explicit_layout != optimal_layout)) {
578 if (VK_IMAGE_LAYOUT_GENERAL == explicit_layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700579 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
580 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600581 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
582 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(cb_node->commandBuffer),
583 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
584 "%s: For optimal performance image 0x%" PRIxLEAST64 " layout should be %s instead of GENERAL.",
585 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(optimal_layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700586 }
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -0600587 } else if (image_state->shared_presentable) {
588 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != explicit_layout) {
589 // TODO: Add unique error id when available.
590 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
591 __LINE__, msg_code, "DS",
592 "Layout for shared presentable image is %s but must be VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR.",
593 string_VkImageLayout(optimal_layout));
594 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700595 } else {
Tobin Ehlisc8266452017-04-07 12:20:30 -0600596 *error = true;
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600597 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
598 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, msg_code, "DS",
599 "%s: Layout for image 0x%" PRIxLEAST64 " is %s but can only be %s or VK_IMAGE_LAYOUT_GENERAL. %s",
600 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
601 string_VkImageLayout(optimal_layout), validation_error_map[msg_code]);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700602 }
603 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600604 return skip;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700605}
606
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700607void TransitionFinalSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkRenderPassBeginInfo *pRenderPassBegin,
608 FRAMEBUFFER_STATE *framebuffer_state) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700609 auto renderPass = GetRenderPassState(device_data, pRenderPassBegin->renderPass);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700610 if (!renderPass) return;
611
612 const VkRenderPassCreateInfo *pRenderPassInfo = renderPass->createInfo.ptr();
613 if (framebuffer_state) {
614 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
615 auto image_view = framebuffer_state->createInfo.pAttachments[i];
616 SetImageViewLayout(device_data, pCB, image_view, pRenderPassInfo->pAttachments[i].finalLayout);
617 }
618 }
619}
620
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700621bool PreCallValidateCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo,
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700622 const VkAllocationCallbacks *pAllocator, VkImage *pImage) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600623 bool skip = false;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700624 const debug_report_data *report_data = core_validation::GetReportData(device_data);
625
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600626 if (pCreateInfo->format == VK_FORMAT_UNDEFINED) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600627 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
628 VALIDATION_ERROR_00715, "IMAGE", "vkCreateImage: VkFormat for image must not be VK_FORMAT_UNDEFINED. %s",
629 validation_error_map[VALIDATION_ERROR_00715]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600630
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600631 return skip;
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600632 }
633
634 const VkFormatProperties *properties = GetFormatProperties(device_data, pCreateInfo->format);
635
636 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) && (properties->linearTilingFeatures == 0)) {
637 std::stringstream ss;
638 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600639 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
640 VALIDATION_ERROR_02150, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02150]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600641
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600642 return skip;
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600643 }
644
645 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) && (properties->optimalTilingFeatures == 0)) {
646 std::stringstream ss;
647 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600648 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
649 VALIDATION_ERROR_02155, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02155]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600650
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600651 return skip;
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600652 }
653
654 // Validate that format supports usage as color attachment
655 if (pCreateInfo->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
656 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
657 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
658 std::stringstream ss;
659 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
660 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600661 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600662 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
663 VALIDATION_ERROR_02158, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02158]);
664 }
665 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
666 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
667 std::stringstream ss;
668 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
669 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600670 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600671 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
672 VALIDATION_ERROR_02153, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02153]);
673 }
674 }
675
676 // Validate that format supports usage as depth/stencil attachment
677 if (pCreateInfo->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
678 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
679 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
680 std::stringstream ss;
681 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
682 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600683 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600684 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
685 VALIDATION_ERROR_02159, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02159]);
686 }
687 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
688 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
689 std::stringstream ss;
690 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
691 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600692 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600693 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
694 VALIDATION_ERROR_02154, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02154]);
695 }
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700696 }
697
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700698 const VkImageFormatProperties *ImageFormatProperties = GetImageFormatProperties(
699 device_data, pCreateInfo->format, pCreateInfo->imageType, pCreateInfo->tiling, pCreateInfo->usage, pCreateInfo->flags);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700700
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700701 VkDeviceSize imageGranularity = GetPhysicalDeviceProperties(device_data)->limits.bufferImageGranularity;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700702 imageGranularity = imageGranularity == 1 ? 0 : imageGranularity;
Tobin Ehlisa55b1d42017-04-04 12:23:48 -0600703 // TODO : This is also covering 2918 & 2919. Break out into separate checks
Mark Lobodzinski688ed322017-01-27 11:13:21 -0700704 if ((pCreateInfo->extent.width <= 0) || (pCreateInfo->extent.height <= 0) || (pCreateInfo->extent.depth <= 0)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600705 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
706 VALIDATION_ERROR_02917, "Image",
707 "CreateImage extent is 0 for at least one required dimension for image: "
708 "Width = %d Height = %d Depth = %d. %s",
709 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
710 validation_error_map[VALIDATION_ERROR_02917]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700711 }
712
713 // TODO: VALIDATION_ERROR_02125 VALIDATION_ERROR_02126 VALIDATION_ERROR_02128 VALIDATION_ERROR_00720
714 // All these extent-related VUs should be checked here
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700715 if ((pCreateInfo->extent.depth > ImageFormatProperties->maxExtent.depth) ||
716 (pCreateInfo->extent.width > ImageFormatProperties->maxExtent.width) ||
717 (pCreateInfo->extent.height > ImageFormatProperties->maxExtent.height)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600718 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
719 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
720 "CreateImage extents exceed allowable limits for format: "
721 "Width = %d Height = %d Depth = %d: Limits for Width = %d Height = %d Depth = %d for format %s.",
722 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
723 ImageFormatProperties->maxExtent.width, ImageFormatProperties->maxExtent.height,
724 ImageFormatProperties->maxExtent.depth, string_VkFormat(pCreateInfo->format));
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700725 }
726
Dave Houlton1150cf52017-04-27 14:38:11 -0600727 uint64_t totalSize =
728 ((uint64_t)pCreateInfo->extent.width * (uint64_t)pCreateInfo->extent.height * (uint64_t)pCreateInfo->extent.depth *
729 (uint64_t)pCreateInfo->arrayLayers * (uint64_t)pCreateInfo->samples * (uint64_t)FormatSize(pCreateInfo->format) +
730 (uint64_t)imageGranularity) &
731 ~(uint64_t)imageGranularity;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700732
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700733 if (totalSize > ImageFormatProperties->maxResourceSize) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600734 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
735 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
736 "CreateImage resource size exceeds allowable maximum "
737 "Image resource size = 0x%" PRIxLEAST64 ", maximum resource size = 0x%" PRIxLEAST64 " ",
738 totalSize, ImageFormatProperties->maxResourceSize);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700739 }
740
741 // TODO: VALIDATION_ERROR_02132
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700742 if (pCreateInfo->mipLevels > ImageFormatProperties->maxMipLevels) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600743 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
744 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
745 "CreateImage mipLevels=%d exceeds allowable maximum supported by format of %d", pCreateInfo->mipLevels,
746 ImageFormatProperties->maxMipLevels);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700747 }
748
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700749 if (pCreateInfo->arrayLayers > ImageFormatProperties->maxArrayLayers) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600750 skip |= log_msg(
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700751 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__, VALIDATION_ERROR_02133,
752 "Image", "CreateImage arrayLayers=%d exceeds allowable maximum supported by format of %d. %s", pCreateInfo->arrayLayers,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700753 ImageFormatProperties->maxArrayLayers, validation_error_map[VALIDATION_ERROR_02133]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700754 }
755
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700756 if ((pCreateInfo->samples & ImageFormatProperties->sampleCounts) == 0) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600757 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
758 VALIDATION_ERROR_02138, "Image", "CreateImage samples %s is not supported by format 0x%.8X. %s",
759 string_VkSampleCountFlagBits(pCreateInfo->samples), ImageFormatProperties->sampleCounts,
760 validation_error_map[VALIDATION_ERROR_02138]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700761 }
762
763 if (pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_UNDEFINED && pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_PREINITIALIZED) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600764 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
765 VALIDATION_ERROR_00731, "Image",
766 "vkCreateImage parameter, pCreateInfo->initialLayout, must be VK_IMAGE_LAYOUT_UNDEFINED or "
767 "VK_IMAGE_LAYOUT_PREINITIALIZED. %s",
768 validation_error_map[VALIDATION_ERROR_00731]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700769 }
770
Mark Lobodzinski847b60c2017-03-13 09:32:45 -0600771 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600772 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
773 VALIDATION_ERROR_02143, "DS",
774 "vkCreateImage(): the sparseBinding device feature is disabled: Images cannot be created with the "
775 "VK_IMAGE_CREATE_SPARSE_BINDING_BIT set. %s",
776 validation_error_map[VALIDATION_ERROR_02143]);
Mark Lobodzinski847b60c2017-03-13 09:32:45 -0600777 }
778
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -0600779 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600780 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
781 DRAWSTATE_INVALID_FEATURE, "DS",
782 "vkCreateImage(): the sparseResidencyAliased device feature is disabled: Images cannot be created with the "
783 "VK_IMAGE_CREATE_SPARSE_ALIASED_BIT set.");
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -0600784 }
785
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600786 return skip;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700787}
788
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700789void PostCallRecordCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo, VkImage *pImage) {
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700790 IMAGE_LAYOUT_NODE image_state;
791 image_state.layout = pCreateInfo->initialLayout;
792 image_state.format = pCreateInfo->format;
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700793 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 -0700794 ImageSubresourcePair subpair{*pImage, false, VkImageSubresource()};
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700795 (*core_validation::GetImageSubresourceMap(device_data))[*pImage].push_back(subpair);
796 (*core_validation::GetImageLayoutMap(device_data))[subpair] = image_state;
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700797}
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700798
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700799bool PreCallValidateDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE **image_state, VK_OBJECT *obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700800 const CHECK_DISABLED *disabled = core_validation::GetDisables(device_data);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700801 *image_state = core_validation::GetImageState(device_data, image);
Dave Houlton1150cf52017-04-27 14:38:11 -0600802 *obj_struct = {reinterpret_cast<uint64_t &>(image), kVulkanObjectTypeImage};
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700803 if (disabled->destroy_image) return false;
804 bool skip = false;
805 if (*image_state) {
806 skip |= core_validation::ValidateObjectNotInUse(device_data, *image_state, *obj_struct, VALIDATION_ERROR_00743);
807 }
808 return skip;
809}
810
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700811void PostCallRecordDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE *image_state, VK_OBJECT obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700812 core_validation::invalidateCommandBuffers(device_data, image_state->cb_bindings, obj_struct);
813 // Clean up memory mapping, bindings and range references for image
814 for (auto mem_binding : image_state->GetBoundMemory()) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700815 auto mem_info = core_validation::GetMemObjInfo(device_data, mem_binding);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700816 if (mem_info) {
817 core_validation::RemoveImageMemoryRange(obj_struct.handle, mem_info);
818 }
819 }
Mark Lobodzinski33826372017-04-13 11:10:11 -0600820 core_validation::ClearMemoryObjectBindings(device_data, obj_struct.handle, kVulkanObjectTypeImage);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700821 // Remove image from imageMap
822 core_validation::GetImageMap(device_data)->erase(image);
823 std::unordered_map<VkImage, std::vector<ImageSubresourcePair>> *imageSubresourceMap =
824 core_validation::GetImageSubresourceMap(device_data);
825
826 const auto &sub_entry = imageSubresourceMap->find(image);
827 if (sub_entry != imageSubresourceMap->end()) {
828 for (const auto &pair : sub_entry->second) {
829 core_validation::GetImageLayoutMap(device_data)->erase(pair);
830 }
831 imageSubresourceMap->erase(sub_entry);
832 }
833}
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700834
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700835bool ValidateImageAttributes(layer_data *device_data, IMAGE_STATE *image_state, VkImageSubresourceRange range) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700836 bool skip = false;
837 const debug_report_data *report_data = core_validation::GetReportData(device_data);
838
839 if (range.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) {
840 char const str[] = "vkCmdClearColorImage aspectMasks for all subresource ranges must be set to VK_IMAGE_ASPECT_COLOR_BIT";
841 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
842 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
843 }
844
Dave Houlton1d2022c2017-03-29 11:43:58 -0600845 if (FormatIsDepthOrStencil(image_state->createInfo.format)) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700846 char const str[] = "vkCmdClearColorImage called with depth/stencil image.";
847 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
848 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
849 validation_error_map[VALIDATION_ERROR_01088]);
Dave Houlton1d2022c2017-03-29 11:43:58 -0600850 } else if (FormatIsCompressed(image_state->createInfo.format)) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700851 char const str[] = "vkCmdClearColorImage called with compressed image.";
852 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
853 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
854 validation_error_map[VALIDATION_ERROR_01088]);
855 }
856
857 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
858 char const str[] = "vkCmdClearColorImage called with image created without VK_IMAGE_USAGE_TRANSFER_DST_BIT.";
859 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
860 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01084, "IMAGE", "%s. %s", str,
861 validation_error_map[VALIDATION_ERROR_01084]);
862 }
863 return skip;
864}
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700865
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600866uint32_t ResolveRemainingLevels(const VkImageSubresourceRange *range, uint32_t mip_levels) {
867 // Return correct number of mip levels taking into account VK_REMAINING_MIP_LEVELS
868 uint32_t mip_level_count = range->levelCount;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700869 if (range->levelCount == VK_REMAINING_MIP_LEVELS) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600870 mip_level_count = mip_levels - range->baseMipLevel;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700871 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600872 return mip_level_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700873}
874
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600875uint32_t ResolveRemainingLayers(const VkImageSubresourceRange *range, uint32_t layers) {
876 // Return correct number of layers taking into account VK_REMAINING_ARRAY_LAYERS
877 uint32_t array_layer_count = range->layerCount;
878 if (range->layerCount == VK_REMAINING_ARRAY_LAYERS) {
879 array_layer_count = layers - range->baseArrayLayer;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700880 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600881 return array_layer_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700882}
883
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700884bool VerifyClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *image_state,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700885 VkImageSubresourceRange range, VkImageLayout dest_image_layout, const char *func_name) {
886 bool skip = false;
887 const debug_report_data *report_data = core_validation::GetReportData(device_data);
888
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600889 uint32_t level_count = ResolveRemainingLevels(&range, image_state->createInfo.mipLevels);
890 uint32_t layer_count = ResolveRemainingLayers(&range, image_state->createInfo.arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700891
892 if (dest_image_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
893 if (dest_image_layout == VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700894 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
895 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600896 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
897 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700898 "%s: Layout for cleared image should be TRANSFER_DST_OPTIMAL instead of GENERAL.", func_name);
899 }
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -0600900 } else if (image_state->shared_presentable) {
901 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != dest_image_layout) {
902 // TODO: Add unique error id when available.
903 skip |=
904 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 0, "DS",
905 "Layout for shared presentable cleared image is %s but can only be VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR.",
906 string_VkImageLayout(dest_image_layout));
907 }
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700908 } else {
909 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01086;
910 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
911 error_code = VALIDATION_ERROR_01101;
912 } else {
913 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
914 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600915 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
916 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, error_code, "DS",
917 "%s: Layout for cleared image is %s but can only be "
918 "TRANSFER_DST_OPTIMAL or GENERAL. %s",
919 func_name, string_VkImageLayout(dest_image_layout), validation_error_map[error_code]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700920 }
921 }
922
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600923 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
924 uint32_t level = level_index + range.baseMipLevel;
925 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
926 uint32_t layer = layer_index + range.baseArrayLayer;
927 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700928 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700929 if (FindCmdBufLayout(device_data, cb_node, image_state->image, sub, node)) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700930 if (node.layout != dest_image_layout) {
931 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01085;
932 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
933 error_code = VALIDATION_ERROR_01100;
934 } else {
935 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
936 }
937 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
938 __LINE__, error_code, "DS",
939 "%s: Cannot clear an image whose layout is %s and "
940 "doesn't match the current layout %s. %s",
941 func_name, string_VkImageLayout(dest_image_layout), string_VkImageLayout(node.layout),
942 validation_error_map[error_code]);
943 }
944 }
945 }
946 }
947
948 return skip;
949}
950
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700951void RecordClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImage image, VkImageSubresourceRange range,
952 VkImageLayout dest_image_layout) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600953 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, image)->createInfo);
954 uint32_t level_count = ResolveRemainingLevels(&range, image_create_info->mipLevels);
955 uint32_t layer_count = ResolveRemainingLayers(&range, image_create_info->arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700956
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600957 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
958 uint32_t level = level_index + range.baseMipLevel;
959 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
960 uint32_t layer = layer_index + range.baseArrayLayer;
961 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700962 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700963 if (!FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
964 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 -0700965 }
966 }
967 }
968}
969
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700970bool PreCallValidateCmdClearColorImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700971 VkImageLayout imageLayout, uint32_t rangeCount, const VkImageSubresourceRange *pRanges) {
972 bool skip = false;
973 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700974 auto cb_node = GetCBNode(dev_data, commandBuffer);
975 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700976 if (cb_node && image_state) {
977 skip |= ValidateMemoryIsBoundToImage(dev_data, image_state, "vkCmdClearColorImage()", VALIDATION_ERROR_02527);
Mike Schuchardt9c582402017-02-23 15:57:37 -0700978 skip |= ValidateCmdQueueFlags(dev_data, cb_node, "vkCmdClearColorImage()", VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT,
979 VALIDATION_ERROR_01095);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700980 skip |= ValidateCmd(dev_data, cb_node, CMD_CLEARCOLORIMAGE, "vkCmdClearColorImage()");
981 skip |= insideRenderPass(dev_data, cb_node, "vkCmdClearColorImage()", VALIDATION_ERROR_01096);
982 for (uint32_t i = 0; i < rangeCount; ++i) {
983 skip |= ValidateImageAttributes(dev_data, image_state, pRanges[i]);
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700984 skip |= VerifyClearImageLayout(dev_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearColorImage()");
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700985 }
986 }
987 return skip;
988}
989
990// This state recording routine is shared between ClearColorImage and ClearDepthStencilImage
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700991void PreCallRecordCmdClearImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
992 uint32_t rangeCount, const VkImageSubresourceRange *pRanges, CMD_TYPE cmd_type) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700993 auto cb_node = GetCBNode(dev_data, commandBuffer);
994 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700995 if (cb_node && image_state) {
996 AddCommandBufferBindingImage(dev_data, cb_node, image_state);
997 std::function<bool()> function = [=]() {
998 SetImageMemoryValid(dev_data, image_state, true);
999 return false;
1000 };
1001 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001002 core_validation::UpdateCmdBufferLastCmd(cb_node, cmd_type);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -07001003 for (uint32_t i = 0; i < rangeCount; ++i) {
1004 RecordClearImageLayout(dev_data, cb_node, image, pRanges[i], imageLayout);
1005 }
1006 }
1007}
1008
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001009bool PreCallValidateCmdClearDepthStencilImage(layer_data *device_data, VkCommandBuffer commandBuffer, VkImage image,
1010 VkImageLayout imageLayout, uint32_t rangeCount,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -07001011 const VkImageSubresourceRange *pRanges) {
1012 bool skip = false;
Mark Lobodzinski1241a312017-02-01 10:57:21 -07001013 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1014
Mark Lobodzinskid81d1012017-02-01 09:03:06 -07001015 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001016 auto cb_node = GetCBNode(device_data, commandBuffer);
1017 auto image_state = GetImageState(device_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -07001018 if (cb_node && image_state) {
Mark Lobodzinski1241a312017-02-01 10:57:21 -07001019 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_02528);
Mike Schuchardt9c582402017-02-23 15:57:37 -07001020 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearDepthStencilImage()", VK_QUEUE_GRAPHICS_BIT,
1021 VALIDATION_ERROR_01110);
Mark Lobodzinski1241a312017-02-01 10:57:21 -07001022 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARDEPTHSTENCILIMAGE, "vkCmdClearDepthStencilImage()");
1023 skip |= insideRenderPass(device_data, cb_node, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_01111);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -07001024 for (uint32_t i = 0; i < rangeCount; ++i) {
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -07001025 skip |=
1026 VerifyClearImageLayout(device_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearDepthStencilImage()");
Mark Lobodzinski1241a312017-02-01 10:57:21 -07001027 // Image aspect must be depth or stencil or both
1028 if (((pRanges[i].aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
1029 ((pRanges[i].aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1030 char const str[] =
1031 "vkCmdClearDepthStencilImage aspectMasks for all subresource ranges must be "
1032 "set to VK_IMAGE_ASPECT_DEPTH_BIT and/or VK_IMAGE_ASPECT_STENCIL_BIT";
1033 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1034 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
1035 }
1036 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001037 if (image_state && !FormatIsDepthOrStencil(image_state->createInfo.format)) {
Mark Lobodzinski1241a312017-02-01 10:57:21 -07001038 char const str[] = "vkCmdClearDepthStencilImage called without a depth/stencil image.";
1039 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
1040 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_01103, "IMAGE", "%s. %s", str,
1041 validation_error_map[VALIDATION_ERROR_01103]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -07001042 }
1043 }
1044 return skip;
1045}
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001046
1047// Returns true if [x, xoffset] and [y, yoffset] overlap
1048static bool RangesIntersect(int32_t start, uint32_t start_offset, int32_t end, uint32_t end_offset) {
1049 bool result = false;
1050 uint32_t intersection_min = std::max(static_cast<uint32_t>(start), static_cast<uint32_t>(end));
1051 uint32_t intersection_max = std::min(static_cast<uint32_t>(start) + start_offset, static_cast<uint32_t>(end) + end_offset);
1052
1053 if (intersection_max > intersection_min) {
1054 result = true;
1055 }
1056 return result;
1057}
1058
1059// Returns true if two VkImageCopy structures overlap
1060static bool RegionIntersects(const VkImageCopy *src, const VkImageCopy *dst, VkImageType type) {
1061 bool result = false;
1062 if ((src->srcSubresource.mipLevel == dst->dstSubresource.mipLevel) &&
1063 (RangesIntersect(src->srcSubresource.baseArrayLayer, src->srcSubresource.layerCount, dst->dstSubresource.baseArrayLayer,
1064 dst->dstSubresource.layerCount))) {
1065 result = true;
1066 switch (type) {
1067 case VK_IMAGE_TYPE_3D:
1068 result &= RangesIntersect(src->srcOffset.z, src->extent.depth, dst->dstOffset.z, dst->extent.depth);
1069 // Intentionally fall through to 2D case
1070 case VK_IMAGE_TYPE_2D:
1071 result &= RangesIntersect(src->srcOffset.y, src->extent.height, dst->dstOffset.y, dst->extent.height);
1072 // Intentionally fall through to 1D case
1073 case VK_IMAGE_TYPE_1D:
1074 result &= RangesIntersect(src->srcOffset.x, src->extent.width, dst->dstOffset.x, dst->extent.width);
1075 break;
1076 default:
1077 // Unrecognized or new IMAGE_TYPE enums will be caught in parameter_validation
1078 assert(false);
1079 }
1080 }
1081 return result;
1082}
1083
Dave Houltonfc1a4052017-04-27 14:32:45 -06001084// Returns non-zero if offset and extent exceed image extents
1085static const uint32_t x_bit = 1;
1086static const uint32_t y_bit = 2;
1087static const uint32_t z_bit = 4;
Dave Houlton1150cf52017-04-27 14:38:11 -06001088static uint32_t ExceedsBounds(const VkOffset3D *offset, const VkExtent3D *extent, const VkExtent3D *image_extent) {
Dave Houltonfc1a4052017-04-27 14:32:45 -06001089 uint32_t result = 0;
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001090 // Extents/depths cannot be negative but checks left in for clarity
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001091 if ((offset->z + extent->depth > image_extent->depth) || (offset->z < 0) ||
1092 ((offset->z + static_cast<int32_t>(extent->depth)) < 0)) {
Dave Houltonfc1a4052017-04-27 14:32:45 -06001093 result |= z_bit;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001094 }
1095 if ((offset->y + extent->height > image_extent->height) || (offset->y < 0) ||
1096 ((offset->y + static_cast<int32_t>(extent->height)) < 0)) {
Dave Houltonfc1a4052017-04-27 14:32:45 -06001097 result |= y_bit;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001098 }
1099 if ((offset->x + extent->width > image_extent->width) || (offset->x < 0) ||
1100 ((offset->x + static_cast<int32_t>(extent->width)) < 0)) {
Dave Houltonfc1a4052017-04-27 14:32:45 -06001101 result |= x_bit;
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001102 }
1103 return result;
1104}
1105
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001106// Test if two VkExtent3D structs are equivalent
1107static inline bool IsExtentEqual(const VkExtent3D *extent, const VkExtent3D *other_extent) {
1108 bool result = true;
1109 if ((extent->width != other_extent->width) || (extent->height != other_extent->height) ||
1110 (extent->depth != other_extent->depth)) {
1111 result = false;
1112 }
1113 return result;
1114}
1115
1116// Returns the image extent of a specific subresource.
1117static inline VkExtent3D GetImageSubresourceExtent(const IMAGE_STATE *img, const VkImageSubresourceLayers *subresource) {
1118 const uint32_t mip = subresource->mipLevel;
Dave Houltonfc1a4052017-04-27 14:32:45 -06001119
1120 // Return zero extent if mip level doesn't exist
Dave Houlton1150cf52017-04-27 14:38:11 -06001121 if (mip >= img->createInfo.mipLevels) {
1122 return VkExtent3D{0, 0, 0};
Dave Houltonfc1a4052017-04-27 14:32:45 -06001123 }
Dave Houlton1150cf52017-04-27 14:38:11 -06001124
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001125 // Don't allow mip adjustment to create 0 dim, but pass along a 0 if that's what subresource specified
Dave Houltonfc1a4052017-04-27 14:32:45 -06001126 VkExtent3D extent = img->createInfo.extent;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001127 extent.width = (0 == extent.width ? 0 : std::max(1U, extent.width >> mip));
1128 extent.height = (0 == extent.height ? 0 : std::max(1U, extent.height >> mip));
1129 extent.depth = (0 == extent.depth ? 0 : std::max(1U, extent.depth >> mip));
Dave Houltonfc1a4052017-04-27 14:32:45 -06001130
1131 // For 2D images, the number of layers present becomes the effective depth (for 2D <-> 3D copies)
1132 // In this case the depth extent is not diminished with mip level
Dave Houlton1150cf52017-04-27 14:38:11 -06001133 if (VK_IMAGE_TYPE_2D == img->createInfo.imageType) {
Dave Houltonfc1a4052017-04-27 14:32:45 -06001134 extent.depth = img->createInfo.arrayLayers;
1135 }
1136
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001137 return extent;
1138}
1139
1140// Test if the extent argument has all dimensions set to 0.
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001141static inline bool IsExtentAllZeroes(const VkExtent3D *extent) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001142 return ((extent->width == 0) && (extent->height == 0) && (extent->depth == 0));
1143}
1144
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001145// Test if the extent argument has any dimensions set to 0.
1146static inline bool IsExtentSizeZero(const VkExtent3D *extent) {
1147 return ((extent->width == 0) || (extent->height == 0) || (extent->depth == 0));
1148}
1149
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001150// Returns the image transfer granularity for a specific image scaled by compressed block size if necessary.
1151static inline VkExtent3D GetScaledItg(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *img) {
1152 // Default to (0, 0, 0) granularity in case we can't find the real granularity for the physical device.
1153 VkExtent3D granularity = {0, 0, 0};
1154 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
1155 if (pPool) {
1156 granularity =
1157 GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].minImageTransferGranularity;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001158 if (FormatIsCompressed(img->createInfo.format)) {
1159 auto block_size = FormatCompressedTexelBlockExtent(img->createInfo.format);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001160 granularity.width *= block_size.width;
1161 granularity.height *= block_size.height;
1162 }
1163 }
1164 return granularity;
1165}
1166
1167// Test elements of a VkExtent3D structure against alignment constraints contained in another VkExtent3D structure
1168static inline bool IsExtentAligned(const VkExtent3D *extent, const VkExtent3D *granularity) {
1169 bool valid = true;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001170 if ((SafeModulo(extent->depth, granularity->depth) != 0) || (SafeModulo(extent->width, granularity->width) != 0) ||
1171 (SafeModulo(extent->height, granularity->height) != 0)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001172 valid = false;
1173 }
1174 return valid;
1175}
1176
1177// Check elements of a VkOffset3D structure against a queue family's Image Transfer Granularity values
1178static inline bool CheckItgOffset(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkOffset3D *offset,
1179 const VkExtent3D *granularity, const uint32_t i, const char *function, const char *member) {
1180 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1181 bool skip = false;
1182 VkExtent3D offset_extent = {};
1183 offset_extent.width = static_cast<uint32_t>(abs(offset->x));
1184 offset_extent.height = static_cast<uint32_t>(abs(offset->y));
1185 offset_extent.depth = static_cast<uint32_t>(abs(offset->z));
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001186 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001187 // 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 -07001188 if (IsExtentAllZeroes(&offset_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001189 skip |=
1190 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1191 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1192 "%s: pRegion[%d].%s (x=%d, y=%d, z=%d) must be (x=0, y=0, z=0) "
1193 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1194 function, i, member, offset->x, offset->y, offset->z);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001195 }
1196 } else {
1197 // If the queue family image transfer granularity is not (0, 0, 0), then the offset dimensions must always be even
1198 // integer multiples of the image transfer granularity.
1199 if (IsExtentAligned(&offset_extent, granularity) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001200 skip |= log_msg(
1201 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1202 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1203 "%s: pRegion[%d].%s (x=%d, y=%d, z=%d) dimensions must be even integer "
1204 "multiples of this command buffer's queue family image transfer granularity (w=%d, h=%d, d=%d).",
1205 function, i, member, offset->x, offset->y, offset->z, granularity->width, granularity->height, granularity->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001206 }
1207 }
1208 return skip;
1209}
1210
1211// Check elements of a VkExtent3D structure against a queue family's Image Transfer Granularity values
1212static inline bool CheckItgExtent(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkExtent3D *extent,
1213 const VkOffset3D *offset, const VkExtent3D *granularity, const VkExtent3D *subresource_extent,
1214 const uint32_t i, const char *function, const char *member) {
1215 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1216 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001217 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001218 // If the queue family image transfer granularity is (0, 0, 0), then the extent must always match the image
1219 // subresource extent.
1220 if (IsExtentEqual(extent, subresource_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001221 skip |=
1222 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1223 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1224 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d) "
1225 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1226 function, i, member, extent->width, extent->height, extent->depth, subresource_extent->width,
1227 subresource_extent->height, subresource_extent->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001228 }
1229 } else {
1230 // If the queue family image transfer granularity is not (0, 0, 0), then the extent dimensions must always be even
1231 // integer multiples of the image transfer granularity or the offset + extent dimensions must always match the image
1232 // subresource extent dimensions.
1233 VkExtent3D offset_extent_sum = {};
1234 offset_extent_sum.width = static_cast<uint32_t>(abs(offset->x)) + extent->width;
1235 offset_extent_sum.height = static_cast<uint32_t>(abs(offset->y)) + extent->height;
1236 offset_extent_sum.depth = static_cast<uint32_t>(abs(offset->z)) + extent->depth;
1237 if ((IsExtentAligned(extent, granularity) == false) && (IsExtentEqual(&offset_extent_sum, subresource_extent) == false)) {
1238 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001239 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1240 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001241 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) dimensions must be even integer multiples of this command buffer's "
1242 "queue family image transfer granularity (w=%d, h=%d, d=%d) or offset (x=%d, y=%d, z=%d) + "
1243 "extent (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d).",
1244 function, i, member, extent->width, extent->height, extent->depth, granularity->width, granularity->height,
1245 granularity->depth, offset->x, offset->y, offset->z, extent->width, extent->height, extent->depth,
1246 subresource_extent->width, subresource_extent->height, subresource_extent->depth);
1247 }
1248 }
1249 return skip;
1250}
1251
1252// Check a uint32_t width or stride value against a queue family's Image Transfer Granularity width value
1253static inline bool CheckItgInt(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const uint32_t value,
1254 const uint32_t granularity, const uint32_t i, const char *function, const char *member) {
1255 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1256
1257 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001258 if (SafeModulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001259 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1260 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001261 "%s: pRegion[%d].%s (%d) must be an even integer multiple of this command buffer's queue family image "
1262 "transfer granularity width (%d).",
1263 function, i, member, value, granularity);
1264 }
1265 return skip;
1266}
1267
1268// Check a VkDeviceSize value against a queue family's Image Transfer Granularity width value
1269static inline bool CheckItgSize(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkDeviceSize value,
1270 const uint32_t granularity, const uint32_t i, const char *function, const char *member) {
1271 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1272 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001273 if (SafeModulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001274 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1275 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001276 "%s: pRegion[%d].%s (%" PRIdLEAST64
1277 ") must be an even integer multiple of this command buffer's queue family image transfer "
1278 "granularity width (%d).",
1279 function, i, member, value, granularity);
1280 }
1281 return skip;
1282}
1283
1284// Check valid usage Image Tranfer Granularity requirements for elements of a VkBufferImageCopy structure
1285bool ValidateCopyBufferImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1286 const IMAGE_STATE *img, const VkBufferImageCopy *region,
1287 const uint32_t i, const char *function) {
1288 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001289 if (FormatIsCompressed(img->createInfo.format) == true) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001290 // TODO: Add granularity checking for compressed formats
1291
1292 // bufferRowLength must be a multiple of the compressed texel block width
1293 // bufferImageHeight must be a multiple of the compressed texel block height
1294 // all members of imageOffset must be a multiple of the corresponding dimensions of the compressed texel block
1295 // bufferOffset must be a multiple of the compressed texel block size in bytes
1296 // imageExtent.width must be a multiple of the compressed texel block width or (imageExtent.width + imageOffset.x)
1297 // must equal the image subresource width
1298 // imageExtent.height must be a multiple of the compressed texel block height or (imageExtent.height + imageOffset.y)
1299 // must equal the image subresource height
1300 // imageExtent.depth must be a multiple of the compressed texel block depth or (imageExtent.depth + imageOffset.z)
1301 // must equal the image subresource depth
1302 } else {
1303 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1304 skip |= CheckItgSize(device_data, cb_node, region->bufferOffset, granularity.width, i, function, "bufferOffset");
1305 skip |= CheckItgInt(device_data, cb_node, region->bufferRowLength, granularity.width, i, function, "bufferRowLength");
1306 skip |= CheckItgInt(device_data, cb_node, region->bufferImageHeight, granularity.width, i, function, "bufferImageHeight");
1307 skip |= CheckItgOffset(device_data, cb_node, &region->imageOffset, &granularity, i, function, "imageOffset");
1308 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->imageSubresource);
1309 skip |= CheckItgExtent(device_data, cb_node, &region->imageExtent, &region->imageOffset, &granularity, &subresource_extent,
1310 i, function, "imageExtent");
1311 }
1312 return skip;
1313}
1314
1315// Check valid usage Image Tranfer Granularity requirements for elements of a VkImageCopy structure
1316bool ValidateCopyImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1317 const IMAGE_STATE *img, const VkImageCopy *region, const uint32_t i,
1318 const char *function) {
1319 bool skip = false;
1320 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1321 skip |= CheckItgOffset(device_data, cb_node, &region->srcOffset, &granularity, i, function, "srcOffset");
1322 skip |= CheckItgOffset(device_data, cb_node, &region->dstOffset, &granularity, i, function, "dstOffset");
1323 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->dstSubresource);
1324 skip |= CheckItgExtent(device_data, cb_node, &region->extent, &region->dstOffset, &granularity, &subresource_extent, i,
1325 function, "extent");
1326 return skip;
1327}
1328
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001329bool PreCallValidateCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001330 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1331 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001332 bool skip = false;
1333 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1334 VkCommandBuffer command_buffer = cb_node->commandBuffer;
1335
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001336 for (uint32_t i = 0; i < region_count; i++) {
1337 if (regions[i].srcSubresource.layerCount == 0) {
1338 std::stringstream ss;
1339 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] srcSubresource is zero";
1340 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1341 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1342 ss.str().c_str());
1343 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001344
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001345 if (regions[i].dstSubresource.layerCount == 0) {
1346 std::stringstream ss;
1347 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] dstSubresource is zero";
1348 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1349 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1350 ss.str().c_str());
1351 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001352
Chris Forbes481df4f2017-05-02 14:18:07 -07001353 if (!GetDeviceExtensions(device_data)->khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001354 // For each region the layerCount member of srcSubresource and dstSubresource must match
1355 if (regions[i].srcSubresource.layerCount != regions[i].dstSubresource.layerCount) {
1356 std::stringstream ss;
1357 ss << "vkCmdCopyImage: number of layers in source and destination subresources for pRegions[" << i
1358 << "] do not match";
1359 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1360 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01198, "IMAGE", "%s. %s",
1361 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01198]);
1362 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001363 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001364
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001365 // For each region, the aspectMask member of srcSubresource and dstSubresource must match
1366 if (regions[i].srcSubresource.aspectMask != regions[i].dstSubresource.aspectMask) {
1367 char const str[] = "vkCmdCopyImage: Src and dest aspectMasks for each region must match";
1368 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1369 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01197, "IMAGE", "%s. %s", str,
1370 validation_error_map[VALIDATION_ERROR_01197]);
1371 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001372
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06001373 // For each region, the aspectMask member of srcSubresource must be present in the source image
1374 if (!VerifyAspectsPresent(regions[i].srcSubresource.aspectMask, src_image_state->createInfo.format)) {
1375 std::stringstream ss;
1376 ss << "vkCmdCopyImage: pRegion[" << i
1377 << "] srcSubresource.aspectMask cannot specify aspects not present in source image";
1378 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1379 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01200, "IMAGE", "%s. %s",
1380 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01200]);
1381 }
1382
1383 // For each region, the aspectMask member of dstSubresource must be present in the destination image
1384 if (!VerifyAspectsPresent(regions[i].dstSubresource.aspectMask, dst_image_state->createInfo.format)) {
1385 std::stringstream ss;
1386 ss << "vkCmdCopyImage: pRegion[" << i << "] dstSubresource.aspectMask cannot specify aspects not present in dest image";
1387 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1388 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01201, "IMAGE", "%s. %s",
1389 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01201]);
1390 }
1391
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001392 // AspectMask must not contain VK_IMAGE_ASPECT_METADATA_BIT
1393 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) ||
1394 (regions[i].dstSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT)) {
1395 std::stringstream ss;
1396 ss << "vkCmdCopyImage: pRegions[" << i << "] may not specify aspectMask containing VK_IMAGE_ASPECT_METADATA_BIT";
1397 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1398 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01222, "IMAGE", "%s. %s",
1399 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01222]);
1400 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001401
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001402 // For each region, if aspectMask contains VK_IMAGE_ASPECT_COLOR_BIT, it must not contain either of
1403 // VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT
1404 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) &&
1405 (regions[i].srcSubresource.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
1406 char const str[] = "vkCmdCopyImage aspectMask cannot specify both COLOR and DEPTH/STENCIL aspects";
1407 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1408 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01221, "IMAGE", "%s. %s", str,
1409 validation_error_map[VALIDATION_ERROR_01221]);
1410 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001411
Chris Forbes481df4f2017-05-02 14:18:07 -07001412 if (!GetDeviceExtensions(device_data)->khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001413 // If either of the calling command's src_image or dst_image parameters are of VkImageType VK_IMAGE_TYPE_3D,
1414 // the baseArrayLayer and layerCount members of both srcSubresource and dstSubresource must be 0 and 1, respectively
1415 if (((src_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) ||
1416 (dst_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D)) &&
1417 ((regions[i].srcSubresource.baseArrayLayer != 0) || (regions[i].srcSubresource.layerCount != 1) ||
1418 (regions[i].dstSubresource.baseArrayLayer != 0) || (regions[i].dstSubresource.layerCount != 1))) {
1419 std::stringstream ss;
1420 ss << "vkCmdCopyImage: src or dstImage type was IMAGE_TYPE_3D, but in subRegion[" << i
1421 << "] baseArrayLayer was not zero or layerCount was not 1.";
1422 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1423 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01199, "IMAGE", "%s. %s",
1424 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01199]);
1425 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001426 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001427
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001428 // MipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
1429 if (regions[i].srcSubresource.mipLevel >= src_image_state->createInfo.mipLevels) {
1430 std::stringstream ss;
1431 ss << "vkCmdCopyImage: pRegions[" << i
1432 << "] specifies a src mipLevel greater than the number specified when the srcImage was created.";
1433 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1434 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
1435 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1436 }
1437 if (regions[i].dstSubresource.mipLevel >= dst_image_state->createInfo.mipLevels) {
1438 std::stringstream ss;
1439 ss << "vkCmdCopyImage: pRegions[" << i
1440 << "] specifies a dst mipLevel greater than the number specified when the dstImage was created.";
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_01223, "IMAGE", "%s. %s",
1443 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1444 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001445
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001446 // (baseArrayLayer + layerCount) must be less than or equal to the arrayLayers specified in VkImageCreateInfo when the
1447 // image was created
1448 if ((regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount) >
1449 src_image_state->createInfo.arrayLayers) {
1450 std::stringstream ss;
1451 ss << "vkCmdCopyImage: srcImage arrayLayers was " << src_image_state->createInfo.arrayLayers << " but subRegion[" << i
1452 << "] baseArrayLayer + layerCount is "
1453 << (regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount);
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_01224, "IMAGE", "%s. %s",
1456 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
1457 }
1458 if ((regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount) >
1459 dst_image_state->createInfo.arrayLayers) {
1460 std::stringstream ss;
1461 ss << "vkCmdCopyImage: dstImage arrayLayers was " << dst_image_state->createInfo.arrayLayers << " but subRegion[" << i
1462 << "] baseArrayLayer + layerCount is "
1463 << (regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount);
1464 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1465 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
1466 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
1467 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001468
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001469 // Check region extents for 1D-1D, 2D-2D, and 3D-3D copies
1470 if (src_image_state->createInfo.imageType == dst_image_state->createInfo.imageType) {
1471 // The source region specified by a given element of regions must be a region that is contained within srcImage
Dave Houltonfc1a4052017-04-27 14:32:45 -06001472 VkExtent3D img_extent = GetImageSubresourceExtent(src_image_state, &(regions[i].srcSubresource));
1473 if (0 != ExceedsBounds(&regions[i].srcOffset, &regions[i].extent, &img_extent)) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001474 std::stringstream ss;
Dave Houltonfc1a4052017-04-27 14:32:45 -06001475 ss << "vkCmdCopyImage: Source pRegion[" << i << "] with mipLevel [ " << regions[i].srcSubresource.mipLevel
1476 << " ], offset [ " << regions[i].srcOffset.x << ", " << regions[i].srcOffset.y << ", " << regions[i].srcOffset.z
1477 << " ], extent [ " << regions[i].extent.width << ", " << regions[i].extent.height << ", "
1478 << regions[i].extent.depth << " ] exceeds the source image dimensions";
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001479 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1480 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01175, "IMAGE", "%s. %s",
1481 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01175]);
1482 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001483
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001484 // The destination region specified by a given element of regions must be a region that is contained within dst_image
Dave Houltonfc1a4052017-04-27 14:32:45 -06001485 img_extent = GetImageSubresourceExtent(dst_image_state, &(regions[i].dstSubresource));
1486 if (0 != ExceedsBounds(&regions[i].dstOffset, &regions[i].extent, &img_extent)) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001487 std::stringstream ss;
Dave Houltonfc1a4052017-04-27 14:32:45 -06001488 ss << "vkCmdCopyImage: Dest pRegion[" << i << "] with mipLevel [ " << regions[i].dstSubresource.mipLevel
1489 << " ], offset [ " << regions[i].dstOffset.x << ", " << regions[i].dstOffset.y << ", " << regions[i].dstOffset.z
1490 << " ], extent [ " << regions[i].extent.width << ", " << regions[i].extent.height << ", "
1491 << regions[i].extent.depth << " ] exceeds the destination image dimensions";
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001492 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1493 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01176, "IMAGE", "%s. %s",
1494 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01176]);
1495 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001496 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001497
Dave Houltonfc1a4052017-04-27 14:32:45 -06001498 // Each dimension offset + extent limits must fall with image subresource extent
1499 VkExtent3D subresource_extent = GetImageSubresourceExtent(src_image_state, &(regions[i].srcSubresource));
1500 uint32_t extent_check = ExceedsBounds(&(regions[i].srcOffset), &regions[i].extent, &subresource_extent);
1501 if (extent_check & x_bit) {
1502 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1503 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01202, "IMAGE",
1504 "vkCmdCopyImage: Source image pRegion %1d x-dimension offset [%1d] + extent [%1d] exceeds subResource "
1505 "width [%1d]. %s",
1506 i, regions[i].srcOffset.x, regions[i].extent.width, subresource_extent.width,
1507 validation_error_map[VALIDATION_ERROR_01202]);
1508 }
1509
1510 if (extent_check & y_bit) {
1511 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1512 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01203, "IMAGE",
1513 "vkCmdCopyImage: Source image pRegion %1d y-dimension offset [%1d] + extent [%1d] exceeds subResource "
1514 "height [%1d]. %s",
1515 i, regions[i].srcOffset.y, regions[i].extent.height, subresource_extent.height,
1516 validation_error_map[VALIDATION_ERROR_01203]);
1517 }
1518 if (extent_check & z_bit) {
1519 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1520 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01204, "IMAGE",
1521 "vkCmdCopyImage: Source image pRegion %1d z-dimension offset [%1d] + extent [%1d] exceeds subResource "
1522 "depth [%1d]. %s",
1523 i, regions[i].srcOffset.z, regions[i].extent.depth, subresource_extent.depth,
1524 validation_error_map[VALIDATION_ERROR_01204]);
1525 }
1526
1527 subresource_extent = GetImageSubresourceExtent(dst_image_state, &(regions[i].dstSubresource));
1528 extent_check = ExceedsBounds(&(regions[i].dstOffset), &regions[i].extent, &subresource_extent);
1529 if (extent_check & x_bit) {
1530 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1531 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01205, "IMAGE",
1532 "vkCmdCopyImage: Dest image pRegion %1d x-dimension offset [%1d] + extent [%1d] exceeds subResource "
1533 "width [%1d]. %s",
1534 i, regions[i].dstOffset.x, regions[i].extent.width, subresource_extent.width,
1535 validation_error_map[VALIDATION_ERROR_01205]);
1536 }
1537 if (extent_check & y_bit) {
1538 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1539 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01206, "IMAGE",
1540 "vkCmdCopyImage: Dest image pRegion %1d y-dimension offset [%1d] + extent [%1d] exceeds subResource "
1541 "height [%1d]. %s",
1542 i, regions[i].dstOffset.y, regions[i].extent.height, subresource_extent.height,
1543 validation_error_map[VALIDATION_ERROR_01206]);
1544 }
1545 if (extent_check & z_bit) {
1546 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1547 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01207, "IMAGE",
1548 "vkCmdCopyImage: Dest image pRegion %1d z-dimension offset [%1d] + extent [%1d] exceeds subResource "
1549 "depth [%1d]. %s",
1550 i, regions[i].dstOffset.z, regions[i].extent.depth, subresource_extent.depth,
1551 validation_error_map[VALIDATION_ERROR_01207]);
1552 }
1553
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001554 // The union of all source regions, and the union of all destination regions, specified by the elements of regions,
1555 // must not overlap in memory
1556 if (src_image_state->image == dst_image_state->image) {
1557 for (uint32_t j = 0; j < region_count; j++) {
1558 if (RegionIntersects(&regions[i], &regions[j], src_image_state->createInfo.imageType)) {
1559 std::stringstream ss;
1560 ss << "vkCmdCopyImage: pRegions[" << i << "] src overlaps with pRegions[" << j << "].";
1561 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1562 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01177, "IMAGE",
1563 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01177]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001564 }
1565 }
1566 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001567 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001568
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001569 // The formats of src_image and dst_image must be compatible. Formats are considered compatible if their texel size in bytes
1570 // is the same between both formats. For example, VK_FORMAT_R8G8B8A8_UNORM is compatible with VK_FORMAT_R32_UINT because
1571 // because both texels are 4 bytes in size. Depth/stencil formats must match exactly.
Dave Houlton1150cf52017-04-27 14:38:11 -06001572 if (FormatIsDepthOrStencil(src_image_state->createInfo.format) || FormatIsDepthOrStencil(dst_image_state->createInfo.format)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001573 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1574 char const str[] = "vkCmdCopyImage called with unmatched source and dest image depth/stencil formats.";
1575 skip |=
1576 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1577 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT, "IMAGE", str);
1578 }
1579 } else {
Dave Houlton1d2022c2017-03-29 11:43:58 -06001580 size_t srcSize = FormatSize(src_image_state->createInfo.format);
1581 size_t destSize = FormatSize(dst_image_state->createInfo.format);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001582 if (srcSize != destSize) {
1583 char const str[] = "vkCmdCopyImage called with unmatched source and dest image format sizes.";
1584 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1585 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01184, "IMAGE", "%s. %s", str,
1586 validation_error_map[VALIDATION_ERROR_01184]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001587 }
1588 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001589
Dave Houlton33c22b72017-02-28 13:16:02 -07001590 // Source and dest image sample counts must match
1591 if (src_image_state->createInfo.samples != dst_image_state->createInfo.samples) {
1592 char const str[] = "vkCmdCopyImage() called on image pair with non-identical sample counts.";
1593 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 -06001594 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01185, "IMAGE", "%s %s", str,
1595 validation_error_map[VALIDATION_ERROR_01185]);
Dave Houlton33c22b72017-02-28 13:16:02 -07001596 }
1597
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001598 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02533);
1599 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02534);
1600 // Validate that SRC & DST images have correct usage flags set
1601 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01178,
1602 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
1603 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01181,
1604 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001605 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyImage()",
1606 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01193);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001607 skip |= ValidateCmd(device_data, cb_node, CMD_COPYIMAGE, "vkCmdCopyImage()");
1608 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImage()", VALIDATION_ERROR_01194);
Tobin Ehlisc8266452017-04-07 12:20:30 -06001609 bool hit_error = false;
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001610 for (uint32_t i = 0; i < region_count; ++i) {
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06001611 skip |= VerifyImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -06001612 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01180, &hit_error);
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06001613 skip |= VerifyImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -06001614 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01183, &hit_error);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001615 skip |= ValidateCopyImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &regions[i], i,
1616 "vkCmdCopyImage()");
1617 }
1618
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001619 return skip;
1620}
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001621
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001622void PreCallRecordCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06001623 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1624 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
1625 // Make sure that all image slices are updated to correct layout
1626 for (uint32_t i = 0; i < region_count; ++i) {
1627 SetImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout);
1628 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout);
1629 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001630 // Update bindings between images and cmd buffer
1631 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1632 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Dave Houltoneba86e22017-03-02 14:56:23 -07001633 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImage()"); };
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001634 cb_node->validate_functions.push_back(function);
1635 function = [=]() {
1636 SetImageMemoryValid(device_data, dst_image_state, true);
1637 return false;
1638 };
1639 cb_node->validate_functions.push_back(function);
1640 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGE);
1641}
1642
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001643// Returns true if sub_rect is entirely contained within rect
1644static inline bool ContainsRect(VkRect2D rect, VkRect2D sub_rect) {
1645 if ((sub_rect.offset.x < rect.offset.x) || (sub_rect.offset.x + sub_rect.extent.width > rect.offset.x + rect.extent.width) ||
1646 (sub_rect.offset.y < rect.offset.y) || (sub_rect.offset.y + sub_rect.extent.height > rect.offset.y + rect.extent.height))
1647 return false;
1648 return true;
1649}
1650
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001651bool PreCallValidateCmdClearAttachments(layer_data *device_data, VkCommandBuffer commandBuffer, uint32_t attachmentCount,
1652 const VkClearAttachment *pAttachments, uint32_t rectCount, const VkClearRect *pRects) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001653 GLOBAL_CB_NODE *cb_node = GetCBNode(device_data, commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001654 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1655
1656 bool skip = false;
1657 if (cb_node) {
Mike Schuchardt9c582402017-02-23 15:57:37 -07001658 skip |=
1659 ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearAttachments()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01121);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001660 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARATTACHMENTS, "vkCmdClearAttachments()");
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001661 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_CLEARATTACHMENTS);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001662 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Chris Forbes05375e72017-04-21 13:15:15 -07001663 if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001664 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
1665 // 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 -07001666 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
1667 // CmdClearAttachments.
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001668 skip |=
1669 log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1670 reinterpret_cast<uint64_t &>(commandBuffer), 0, DRAWSTATE_CLEAR_CMD_BEFORE_DRAW, "DS",
1671 "vkCmdClearAttachments() issued on command buffer object 0x%p prior to any Draw Cmds."
1672 " It is recommended you use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
1673 commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001674 }
1675 skip |= outsideRenderPass(device_data, cb_node, "vkCmdClearAttachments()", VALIDATION_ERROR_01122);
1676 }
1677
1678 // Validate that attachment is in reference list of active subpass
1679 if (cb_node->activeRenderPass) {
1680 const VkRenderPassCreateInfo *renderpass_create_info = cb_node->activeRenderPass->createInfo.ptr();
1681 const VkSubpassDescription *subpass_desc = &renderpass_create_info->pSubpasses[cb_node->activeSubpass];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001682 auto framebuffer = GetFramebufferState(device_data, cb_node->activeFramebuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001683
1684 for (uint32_t i = 0; i < attachmentCount; i++) {
1685 auto clear_desc = &pAttachments[i];
1686 VkImageView image_view = VK_NULL_HANDLE;
1687
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001688 if (0 == clear_desc->aspectMask) {
1689 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 -06001690 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01128, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001691 validation_error_map[VALIDATION_ERROR_01128]);
1692 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) {
1693 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 -06001694 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01126, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001695 validation_error_map[VALIDATION_ERROR_01126]);
1696 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001697 if (clear_desc->colorAttachment >= subpass_desc->colorAttachmentCount) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001698 skip |=
1699 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 -06001700 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01114, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001701 "vkCmdClearAttachments() color attachment index %d out of range for active subpass %d. %s",
1702 clear_desc->colorAttachment, cb_node->activeSubpass, validation_error_map[VALIDATION_ERROR_01114]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001703 } else if (subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment == VK_ATTACHMENT_UNUSED) {
1704 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001705 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(commandBuffer),
1706 __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001707 "vkCmdClearAttachments() color attachment index %d is VK_ATTACHMENT_UNUSED; ignored.",
1708 clear_desc->colorAttachment);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001709 } else {
1710 image_view = framebuffer->createInfo
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001711 .pAttachments[subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment];
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001712 }
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001713 if ((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) ||
1714 (clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1715 char const str[] =
1716 "vkCmdClearAttachments aspectMask [%d] must set only VK_IMAGE_ASPECT_COLOR_BIT of a color attachment. %s";
1717 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 -06001718 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01125, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001719 validation_error_map[VALIDATION_ERROR_01125]);
1720 }
1721 } else { // Must be depth and/or stencil
1722 if (((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
1723 ((clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1724 char const str[] = "vkCmdClearAttachments aspectMask [%d] is not a valid combination of bits. %s";
1725 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 -06001726 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01127, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001727 validation_error_map[VALIDATION_ERROR_01127]);
1728 }
1729 if (!subpass_desc->pDepthStencilAttachment ||
1730 (subpass_desc->pDepthStencilAttachment->attachment == VK_ATTACHMENT_UNUSED)) {
1731 skip |= log_msg(
1732 report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001733 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001734 "vkCmdClearAttachments() depth/stencil clear with no depth/stencil attachment in subpass; ignored");
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001735 } else {
1736 image_view = framebuffer->createInfo.pAttachments[subpass_desc->pDepthStencilAttachment->attachment];
1737 }
1738 }
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001739 if (image_view) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001740 auto image_view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001741 for (uint32_t j = 0; j < rectCount; j++) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001742 // The rectangular region specified by a given element of pRects must be contained within the render area of
1743 // the current render pass instance
Mark Lobodzinskid833bb72017-02-22 10:55:30 -07001744 // TODO: This check should be moved to CmdExecuteCommands or QueueSubmit to cover secondary CB cases
1745 if ((cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) &&
1746 (false == ContainsRect(cb_node->activeRenderPassBeginInfo.renderArea, pRects[j].rect))) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001747 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1748 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01115, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001749 "vkCmdClearAttachments(): The area defined by pRects[%d] is not contained in the area of "
1750 "the current render pass instance. %s",
1751 j, validation_error_map[VALIDATION_ERROR_01115]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001752 }
1753 // The layers specified by a given element of pRects must be contained within every attachment that
1754 // pAttachments refers to
1755 auto attachment_base_array_layer = image_view_state->create_info.subresourceRange.baseArrayLayer;
1756 auto attachment_layer_count = image_view_state->create_info.subresourceRange.layerCount;
1757 if ((pRects[j].baseArrayLayer < attachment_base_array_layer) || pRects[j].layerCount > attachment_layer_count) {
1758 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001759 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1760 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01116, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001761 "vkCmdClearAttachments(): The layers defined in pRects[%d] are not contained in the layers of "
1762 "pAttachment[%d]. %s",
1763 j, i, validation_error_map[VALIDATION_ERROR_01116]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001764 }
1765 }
1766 }
1767 }
1768 }
1769 return skip;
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001770}
1771
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001772bool PreCallValidateCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001773 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageResolve *pRegions) {
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001774 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001775 bool skip = false;
1776 if (cb_node && src_image_state && dst_image_state) {
1777 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02541);
1778 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02542);
Mike Schuchardt9c582402017-02-23 15:57:37 -07001779 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdResolveImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01334);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001780 skip |= ValidateCmd(device_data, cb_node, CMD_RESOLVEIMAGE, "vkCmdResolveImage()");
1781 skip |= insideRenderPass(device_data, cb_node, "vkCmdResolveImage()", VALIDATION_ERROR_01335);
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001782
1783 // For each region, the number of layers in the image subresource should not be zero
1784 // For each region, src and dest image aspect must be color only
1785 for (uint32_t i = 0; i < regionCount; i++) {
1786 if (pRegions[i].srcSubresource.layerCount == 0) {
1787 char const str[] = "vkCmdResolveImage: number of layers in source subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001788 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 -07001789 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1790 "IMAGE", str);
1791 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001792 if (pRegions[i].dstSubresource.layerCount == 0) {
1793 char const str[] = "vkCmdResolveImage: number of layers in destination subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001794 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 -07001795 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1796 "IMAGE", str);
1797 }
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001798 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1799 skip |= log_msg(
1800 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1801 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01339, "IMAGE",
1802 "vkCmdResolveImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s", i,
1803 validation_error_map[VALIDATION_ERROR_01339]);
1804 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001805 if ((pRegions[i].srcSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) ||
1806 (pRegions[i].dstSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT)) {
1807 char const str[] =
1808 "vkCmdResolveImage: src and dest aspectMasks for each region must specify only VK_IMAGE_ASPECT_COLOR_BIT";
1809 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1810 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01338, "IMAGE",
1811 "%s. %s", str, validation_error_map[VALIDATION_ERROR_01338]);
1812 }
1813 }
1814
1815 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1816 char const str[] = "vkCmdResolveImage called with unmatched source and dest formats.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001817 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 -07001818 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT,
1819 "IMAGE", str);
1820 }
1821 if (src_image_state->createInfo.imageType != dst_image_state->createInfo.imageType) {
1822 char const str[] = "vkCmdResolveImage called with unmatched source and dest image types.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001823 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 -07001824 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_TYPE, "IMAGE",
1825 str);
1826 }
1827 if (src_image_state->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
1828 char const str[] = "vkCmdResolveImage called with source sample count less than 2.";
1829 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1830 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01320, "IMAGE", "%s. %s",
1831 str, validation_error_map[VALIDATION_ERROR_01320]);
1832 }
1833 if (dst_image_state->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
1834 char const str[] = "vkCmdResolveImage called with dest sample count greater than 1.";
1835 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1836 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01321, "IMAGE", "%s. %s",
1837 str, validation_error_map[VALIDATION_ERROR_01321]);
1838 }
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001839 // TODO: Need to validate image layouts, which will include layout validation for shared presentable images
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001840 } else {
1841 assert(0);
1842 }
1843 return skip;
1844}
1845
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001846void PreCallRecordCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
1847 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001848 // Update bindings between images and cmd buffer
1849 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1850 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
1851
1852 std::function<bool()> function = [=]() {
1853 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdResolveImage()");
1854 };
1855 cb_node->validate_functions.push_back(function);
1856 function = [=]() {
1857 SetImageMemoryValid(device_data, dst_image_state, true);
1858 return false;
1859 };
1860 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001861 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_RESOLVEIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001862}
1863
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001864bool PreCallValidateCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001865 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) {
1866 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1867
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001868 bool skip = false;
1869 if (cb_node && src_image_state && dst_image_state) {
1870 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): srcImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001871 VALIDATION_ERROR_02194);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001872 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): dstImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001873 VALIDATION_ERROR_02195);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001874 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02539);
1875 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02540);
1876 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_02182,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001877 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001878 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_02186,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001879 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001880 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdBlitImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01299);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001881 skip |= ValidateCmd(device_data, cb_node, CMD_BLITIMAGE, "vkCmdBlitImage()");
1882 skip |= insideRenderPass(device_data, cb_node, "vkCmdBlitImage()", VALIDATION_ERROR_01300);
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001883 // TODO: Need to validate image layouts, which will include layout validation for shared presentable images
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001884
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001885 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001886 // Warn for zero-sized regions
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001887 if ((pRegions[i].srcOffsets[0].x == pRegions[i].srcOffsets[1].x) ||
1888 (pRegions[i].srcOffsets[0].y == pRegions[i].srcOffsets[1].y) ||
1889 (pRegions[i].srcOffsets[0].z == pRegions[i].srcOffsets[1].z)) {
1890 std::stringstream ss;
1891 ss << "vkCmdBlitImage: pRegions[" << i << "].srcOffsets specify a zero-volume area.";
1892 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1893 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1894 "%s", ss.str().c_str());
1895 }
1896 if ((pRegions[i].dstOffsets[0].x == pRegions[i].dstOffsets[1].x) ||
1897 (pRegions[i].dstOffsets[0].y == pRegions[i].dstOffsets[1].y) ||
1898 (pRegions[i].dstOffsets[0].z == pRegions[i].dstOffsets[1].z)) {
1899 std::stringstream ss;
1900 ss << "vkCmdBlitImage: pRegions[" << i << "].dstOffsets specify a zero-volume area.";
1901 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1902 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1903 "%s", ss.str().c_str());
1904 }
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001905 if (pRegions[i].srcSubresource.layerCount == 0) {
1906 char const str[] = "vkCmdBlitImage: number of layers in source subresource is zero";
1907 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 -07001908 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1909 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001910 }
1911 if (pRegions[i].dstSubresource.layerCount == 0) {
1912 char const str[] = "vkCmdBlitImage: number of layers in destination subresource is zero";
1913 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 -07001914 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1915 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001916 }
1917
1918 // Check that src/dst layercounts match
1919 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1920 skip |=
1921 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1922 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01304, "IMAGE",
1923 "vkCmdBlitImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s",
1924 i, validation_error_map[VALIDATION_ERROR_01304]);
1925 }
Mark Lobodzinskie7e85fd2017-02-07 13:44:57 -07001926
1927 if (pRegions[i].srcSubresource.aspectMask != pRegions[i].dstSubresource.aspectMask) {
1928 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1929 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01303, "IMAGE",
1930 "vkCmdBlitImage: aspectMask members for pRegion[%d] do not match. %s", i,
1931 validation_error_map[VALIDATION_ERROR_01303]);
1932 }
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001933 }
1934
1935 VkFormat src_format = src_image_state->createInfo.format;
1936 VkFormat dst_format = dst_image_state->createInfo.format;
1937
1938 // Validate consistency for unsigned formats
Dave Houlton1d2022c2017-03-29 11:43:58 -06001939 if (FormatIsUInt(src_format) != FormatIsUInt(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001940 std::stringstream ss;
1941 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has unsigned integer format, "
1942 << "the other one must also have unsigned integer format. "
1943 << "Source format is " << string_VkFormat(src_format) << " Destination format is " << string_VkFormat(dst_format);
1944 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1945 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02191, "IMAGE", "%s. %s",
1946 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02191]);
1947 }
1948
1949 // Validate consistency for signed formats
Dave Houlton1d2022c2017-03-29 11:43:58 -06001950 if (FormatIsSInt(src_format) != FormatIsSInt(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001951 std::stringstream ss;
1952 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has signed integer format, "
1953 << "the other one must also have signed integer format. "
1954 << "Source format is " << string_VkFormat(src_format) << " Destination format is " << string_VkFormat(dst_format);
1955 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1956 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02190, "IMAGE", "%s. %s",
1957 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02190]);
1958 }
1959
1960 // Validate aspect bits and formats for depth/stencil images
Dave Houlton1d2022c2017-03-29 11:43:58 -06001961 if (FormatIsDepthOrStencil(src_format) || FormatIsDepthOrStencil(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001962 if (src_format != dst_format) {
1963 std::stringstream ss;
1964 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has a format of depth, stencil or depth "
1965 << "stencil, the other one must have exactly the same format. "
1966 << "Source format is " << string_VkFormat(src_format) << " Destination format is "
1967 << string_VkFormat(dst_format);
1968 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1969 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02192, "IMAGE",
1970 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02192]);
1971 }
1972
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001973 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001974 VkImageAspectFlags srcAspect = pRegions[i].srcSubresource.aspectMask;
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001975
Dave Houlton1d2022c2017-03-29 11:43:58 -06001976 if (FormatIsDepthAndStencil(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001977 if ((srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1978 std::stringstream ss;
1979 ss << "vkCmdBlitImage: Combination depth/stencil image formats must have only one of "
1980 "VK_IMAGE_ASPECT_DEPTH_BIT "
1981 << "and VK_IMAGE_ASPECT_STENCIL_BIT set in srcImage and dstImage";
1982 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1983 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1984 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1985 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001986 } else if (FormatIsStencilOnly(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001987 if (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT) {
1988 std::stringstream ss;
1989 ss << "vkCmdBlitImage: Stencil-only image formats must have only the VK_IMAGE_ASPECT_STENCIL_BIT "
1990 << "set in both the srcImage and dstImage";
1991 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1992 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1993 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1994 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001995 } else if (FormatIsDepthOnly(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001996 if (srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) {
1997 std::stringstream ss;
1998 ss << "vkCmdBlitImage: Depth-only image formats must have only the VK_IMAGE_ASPECT_DEPTH "
1999 << "set in both the srcImage and dstImage";
2000 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2001 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
2002 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
2003 }
2004 }
2005 }
2006 }
2007
2008 // Validate filter
Dave Houlton1d2022c2017-03-29 11:43:58 -06002009 if (FormatIsDepthOrStencil(src_format) && (filter != VK_FILTER_NEAREST)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07002010 std::stringstream ss;
2011 ss << "vkCmdBlitImage: If the format of srcImage is a depth, stencil, or depth stencil "
2012 << "then filter must be VK_FILTER_NEAREST.";
2013 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2014 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02193, "IMAGE", "%s. %s",
2015 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02193]);
2016 }
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07002017 } else {
2018 assert(0);
2019 }
2020 return skip;
2021}
2022
Tobin Ehlis58c884f2017-02-08 12:15:27 -07002023void PreCallRecordCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
2024 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07002025 // Update bindings between images and cmd buffer
2026 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
2027 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
2028
2029 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdBlitImage()"); };
2030 cb_node->validate_functions.push_back(function);
2031 function = [=]() {
2032 SetImageMemoryValid(device_data, dst_image_state, true);
2033 return false;
2034 };
2035 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002036 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_BLITIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07002037}
2038
Tony Barbourdf013b92017-01-25 12:53:48 -07002039// This validates that the initial layout specified in the command buffer for
2040// the IMAGE is the same
2041// as the global IMAGE layout
Tony Barboure0c5cc92017-02-08 13:53:39 -07002042bool ValidateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB,
2043 std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap) {
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07002044 bool skip = false;
Tony Barbourdf013b92017-01-25 12:53:48 -07002045 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07002046 for (auto cb_image_data : pCB->imageLayoutMap) {
2047 VkImageLayout imageLayout;
Tony Barbourdf013b92017-01-25 12:53:48 -07002048
Jeremy Hayes55b6c292017-02-28 09:44:45 -07002049 if (FindLayout(imageLayoutMap, cb_image_data.first, imageLayout)) {
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07002050 if (cb_image_data.second.initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
2051 // TODO: Set memory invalid which is in mem_tracker currently
2052 } else if (imageLayout != cb_image_data.second.initialLayout) {
2053 if (cb_image_data.first.hasSubresource) {
Dave Houltoneba86e22017-03-02 14:56:23 -07002054 skip |= log_msg(
2055 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2056 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2057 "Cannot submit cmd buffer using image (0x%" PRIx64
2058 ") [sub-resource: aspectMask 0x%X array layer %u, mip level %u], "
2059 "with layout %s when first use is %s.",
2060 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), cb_image_data.first.subresource.aspectMask,
2061 cb_image_data.first.subresource.arrayLayer, cb_image_data.first.subresource.mipLevel,
2062 string_VkImageLayout(imageLayout), string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07002063 } else {
Dave Houltoneba86e22017-03-02 14:56:23 -07002064 skip |=
2065 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2066 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2067 "Cannot submit cmd buffer using image (0x%" PRIx64
2068 ") with layout %s when "
2069 "first use is %s.",
2070 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), string_VkImageLayout(imageLayout),
2071 string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07002072 }
2073 }
Tony Barbourdf013b92017-01-25 12:53:48 -07002074 SetLayout(imageLayoutMap, cb_image_data.first, cb_image_data.second.layout);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07002075 }
2076 }
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07002077 return skip;
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07002078}
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002079
Tony Barbourdf013b92017-01-25 12:53:48 -07002080void UpdateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB) {
2081 for (auto cb_image_data : pCB->imageLayoutMap) {
2082 VkImageLayout imageLayout;
2083 FindGlobalLayout(device_data, cb_image_data.first, imageLayout);
2084 SetGlobalLayout(device_data, cb_image_data.first, cb_image_data.second.layout);
2085 }
2086}
2087
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002088// Print readable FlagBits in FlagMask
2089static std::string string_VkAccessFlags(VkAccessFlags accessMask) {
2090 std::string result;
2091 std::string separator;
2092
2093 if (accessMask == 0) {
2094 result = "[None]";
2095 } else {
2096 result = "[";
2097 for (auto i = 0; i < 32; i++) {
2098 if (accessMask & (1 << i)) {
2099 result = result + separator + string_VkAccessFlagBits((VkAccessFlagBits)(1 << i));
2100 separator = " | ";
2101 }
2102 }
2103 result = result + "]";
2104 }
2105 return result;
2106}
2107
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002108// AccessFlags MUST have 'required_bit' set, and may have one or more of 'optional_bits' set. If required_bit is zero, accessMask
2109// must have at least one of 'optional_bits' set
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002110// TODO: Add tracking to ensure that at least one barrier has been set for these layout transitions
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002111static bool ValidateMaskBits(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer, const VkAccessFlags &accessMask,
2112 const VkImageLayout &layout, VkAccessFlags required_bit, VkAccessFlags optional_bits,
2113 const char *type) {
2114 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2115 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002116
2117 if ((accessMask & required_bit) || (!required_bit && (accessMask & optional_bits))) {
2118 if (accessMask & ~(required_bit | optional_bits)) {
2119 // TODO: Verify against Valid Use
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002120 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2121 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002122 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
2123 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002124 }
2125 } else {
2126 if (!required_bit) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002127 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2128 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002129 "%s AccessMask %d %s must contain at least one of access bits %d "
2130 "%s when layout is %s, unless the app has previously added a "
2131 "barrier for this transition.",
2132 type, accessMask, string_VkAccessFlags(accessMask).c_str(), optional_bits,
2133 string_VkAccessFlags(optional_bits).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002134 } else {
2135 std::string opt_bits;
2136 if (optional_bits != 0) {
2137 std::stringstream ss;
2138 ss << optional_bits;
2139 opt_bits = "and may have optional bits " + ss.str() + ' ' + string_VkAccessFlags(optional_bits);
2140 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002141 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2142 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002143 "%s AccessMask %d %s must have required access bit %d %s %s when "
2144 "layout is %s, unless the app has previously added a barrier for "
2145 "this transition.",
2146 type, accessMask, string_VkAccessFlags(accessMask).c_str(), required_bit,
2147 string_VkAccessFlags(required_bit).c_str(), opt_bits.c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002148 }
2149 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002150 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002151}
2152
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002153bool ValidateMaskBitsFromLayouts(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer,
2154 const VkAccessFlags &accessMask, const VkImageLayout &layout, const char *type) {
2155 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002156
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002157 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002158 switch (layout) {
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002159 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: {
2160 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
2161 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2162 break;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002163 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002164 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: {
2165 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
2166 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2167 break;
2168 }
2169 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: {
2170 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_WRITE_BIT, 0, type);
2171 break;
2172 }
2173 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL: {
2174 skip |= ValidateMaskBits(
2175 device_data, cmdBuffer, accessMask, layout, 0,
2176 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
2177 type);
2178 break;
2179 }
2180 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: {
2181 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, 0,
2182 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
2183 break;
2184 }
2185 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: {
2186 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_READ_BIT, 0, type);
2187 break;
2188 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002189 case VK_IMAGE_LAYOUT_UNDEFINED: {
2190 if (accessMask != 0) {
2191 // TODO: Verify against Valid Use section spec
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002192 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2193 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002194 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
2195 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
2196 }
2197 break;
2198 }
Chris Forbesbfd831d2017-04-28 17:29:10 -07002199 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
Dave Houlton1150cf52017-04-27 14:38:11 -06002200 // Notes: QueuePresentKHR performs automatic visibility operations,
2201 // so the app is /NOT/ required to include VK_ACCESS_MEMORY_READ_BIT
2202 // when transitioning to this layout.
2203 //
2204 // When transitioning /from/ this layout, the application needs to
2205 // avoid only a WAR hazard -- any writes need to be ordered after
2206 // the PE's reads. There is no need for a memory dependency for this
2207 // case.
2208 /* fallthrough */
Chris Forbesbfd831d2017-04-28 17:29:10 -07002209
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002210 case VK_IMAGE_LAYOUT_GENERAL:
2211 default: { break; }
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002212 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002213 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002214}
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002215
2216// ValidateLayoutVsAttachmentDescription is a general function where we can validate various state associated with the
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002217// VkAttachmentDescription structs that are used by the sub-passes of a renderpass. Initial check is to make sure that READ_ONLY
2218// layout attachments don't have CLEAR as their loadOp.
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002219bool ValidateLayoutVsAttachmentDescription(const debug_report_data *report_data, const VkImageLayout first_layout,
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002220 const uint32_t attachment, const VkAttachmentDescription &attachment_description) {
2221 bool skip = false;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002222 // Verify that initial loadOp on READ_ONLY attachments is not CLEAR
2223 if (attachment_description.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2224 if ((first_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) ||
2225 (first_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)) {
Mark Lobodzinskieb9e73f2017-04-13 10:06:48 -06002226 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2227 VALIDATION_ERROR_02351, "DS", "Cannot clear attachment %d with invalid first layout %s. %s", attachment,
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002228 string_VkImageLayout(first_layout), validation_error_map[VALIDATION_ERROR_02351]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002229 }
2230 }
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002231 return skip;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002232}
2233
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002234bool ValidateLayouts(core_validation::layer_data *device_data, VkDevice device, const VkRenderPassCreateInfo *pCreateInfo) {
2235 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002236 bool skip = false;
2237
2238 // Track when we're observing the first use of an attachment
2239 std::vector<bool> attach_first_use(pCreateInfo->attachmentCount, true);
2240 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
2241 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
Cort Stratton7547f772017-05-04 15:18:52 -07002242
2243 // Check input attachments first, so we can detect first-use-as-input for VU #00349
2244 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
2245 auto attach_index = subpass.pInputAttachments[j].attachment;
2246 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2247
2248 switch (subpass.pInputAttachments[j].layout) {
2249 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2250 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
2251 // These are ideal.
2252 break;
2253
2254 case VK_IMAGE_LAYOUT_GENERAL:
2255 // May not be optimal. TODO: reconsider this warning based on other constraints.
2256 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2257 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2258 "Layout for input attachment is GENERAL but should be READ_ONLY_OPTIMAL.");
2259 break;
2260
2261 default:
2262 // No other layouts are acceptable
2263 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2264 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2265 "Layout for input attachment is %s but can only be READ_ONLY_OPTIMAL or GENERAL.",
2266 string_VkImageLayout(subpass.pInputAttachments[j].layout));
2267 }
2268
2269 VkImageLayout layout = subpass.pInputAttachments[j].layout;
2270 bool found_layout_mismatch = subpass.pDepthStencilAttachment &&
2271 subpass.pDepthStencilAttachment->attachment == attach_index &&
2272 subpass.pDepthStencilAttachment->layout != layout;
2273 for (uint32_t c = 0; !found_layout_mismatch && c < subpass.colorAttachmentCount; ++c) {
2274 found_layout_mismatch =
2275 (subpass.pColorAttachments[c].attachment == attach_index && subpass.pColorAttachments[c].layout != layout);
2276 }
2277 if (found_layout_mismatch) {
2278 skip |= log_msg(
2279 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2280 VALIDATION_ERROR_00358, "DS",
2281 "CreateRenderPass: Subpass %u pInputAttachments[%u] (%u) has layout %u, but is also used as a depth/color "
2282 "attachment with a different layout. %s",
2283 i, j, attach_index, layout, validation_error_map[VALIDATION_ERROR_00358]);
2284 }
2285
2286 if (attach_first_use[attach_index]) {
2287 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pInputAttachments[j].layout, attach_index,
2288 pCreateInfo->pAttachments[attach_index]);
2289
2290 bool used_as_depth =
2291 (subpass.pDepthStencilAttachment != NULL && subpass.pDepthStencilAttachment->attachment == attach_index);
2292 bool used_as_color = false;
2293 for (uint32_t k = 0; !used_as_depth && !used_as_color && k < subpass.colorAttachmentCount; ++k) {
2294 used_as_color = (subpass.pColorAttachments[k].attachment == attach_index);
2295 }
2296 if (!used_as_depth && !used_as_color &&
2297 pCreateInfo->pAttachments[attach_index].loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2298 skip |= log_msg(
2299 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2300 VALIDATION_ERROR_00349, "DS",
2301 "CreateRenderPass: attachment %u is first used as an input attachment in subpass %u with loadOp=CLEAR. %s",
2302 attach_index, attach_index, validation_error_map[VALIDATION_ERROR_00349]);
2303 }
2304 }
2305 attach_first_use[attach_index] = false;
2306 }
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002307 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
2308 auto attach_index = subpass.pColorAttachments[j].attachment;
2309 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2310
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06002311 // TODO: Need a way to validate shared presentable images here, currently just allowing
2312 // VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR
2313 // as an acceptable layout, but need to make sure shared presentable images ONLY use that layout
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002314 switch (subpass.pColorAttachments[j].layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002315 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
2316 // This is ideal.
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06002317 case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
2318 // TODO: See note above, just assuming that attachment is shared presentable and allowing this for now.
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002319 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002320
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002321 case VK_IMAGE_LAYOUT_GENERAL:
2322 // May not be optimal; TODO: reconsider this warning based on other constraints?
2323 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2324 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2325 "Layout for color attachment is GENERAL but should be COLOR_ATTACHMENT_OPTIMAL.");
2326 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002327
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002328 default:
2329 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2330 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2331 "Layout for color attachment is %s but can only be COLOR_ATTACHMENT_OPTIMAL or GENERAL.",
2332 string_VkImageLayout(subpass.pColorAttachments[j].layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002333 }
2334
2335 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002336 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pColorAttachments[j].layout, attach_index,
2337 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002338 }
2339 attach_first_use[attach_index] = false;
2340 }
2341 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
2342 switch (subpass.pDepthStencilAttachment->layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002343 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
2344 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2345 // These are ideal.
2346 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002347
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002348 case VK_IMAGE_LAYOUT_GENERAL:
2349 // May not be optimal; TODO: reconsider this warning based on other constraints? GENERAL can be better than
2350 // doing a bunch of transitions.
2351 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2352 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2353 "GENERAL layout for depth attachment may not give optimal performance.");
2354 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002355
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002356 default:
2357 // No other layouts are acceptable
2358 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2359 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2360 "Layout for depth attachment is %s but can only be DEPTH_STENCIL_ATTACHMENT_OPTIMAL, "
2361 "DEPTH_STENCIL_READ_ONLY_OPTIMAL or GENERAL.",
2362 string_VkImageLayout(subpass.pDepthStencilAttachment->layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002363 }
2364
2365 auto attach_index = subpass.pDepthStencilAttachment->attachment;
2366 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002367 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pDepthStencilAttachment->layout, attach_index,
2368 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002369 }
2370 attach_first_use[attach_index] = false;
2371 }
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002372 }
2373 return skip;
2374}
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002375
2376// For any image objects that overlap mapped memory, verify that their layouts are PREINIT or GENERAL
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002377bool ValidateMapImageLayouts(core_validation::layer_data *device_data, VkDevice device, DEVICE_MEM_INFO const *mem_info,
2378 VkDeviceSize offset, VkDeviceSize end_offset) {
2379 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2380 bool skip = false;
2381 // Iterate over all bound image ranges and verify that for any that overlap the map ranges, the layouts are
2382 // VK_IMAGE_LAYOUT_PREINITIALIZED or VK_IMAGE_LAYOUT_GENERAL
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002383 // TODO : This can be optimized if we store ranges based on starting address and early exit when we pass our range
2384 for (auto image_handle : mem_info->bound_images) {
2385 auto img_it = mem_info->bound_ranges.find(image_handle);
2386 if (img_it != mem_info->bound_ranges.end()) {
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002387 if (rangesIntersect(device_data, &img_it->second, offset, end_offset)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002388 std::vector<VkImageLayout> layouts;
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002389 if (FindLayouts(device_data, VkImage(image_handle), layouts)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002390 for (auto layout : layouts) {
2391 if (layout != VK_IMAGE_LAYOUT_PREINITIALIZED && layout != VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002392 skip |= log_msg(
2393 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
2394 reinterpret_cast<const uint64_t &>(mem_info->mem), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2395 "Mapping an image with layout %s can result in undefined behavior if this memory is "
2396 "used by the device. Only GENERAL or PREINITIALIZED should be used.",
2397 string_VkImageLayout(layout));
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002398 }
2399 }
2400 }
2401 }
2402 }
2403 }
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002404 return skip;
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002405}
Mark Lobodzinski96210742017-02-09 10:33:46 -07002406
2407// Helper function to validate correct usage bits set for buffers or images. Verify that (actual & desired) flags != 0 or, if strict
2408// is true, verify that (actual & desired) flags == desired
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002409static 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 -06002410 VulkanObjectType obj_type, int32_t const msgCode, char const *func_name, char const *usage_str) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002411 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002412
2413 bool correct_usage = false;
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002414 bool skip = false;
Mark Lobodzinski33826372017-04-13 11:10:11 -06002415 const char *type_str = object_string[obj_type];
Mark Lobodzinski96210742017-02-09 10:33:46 -07002416 if (strict) {
2417 correct_usage = ((actual & desired) == desired);
2418 } else {
2419 correct_usage = ((actual & desired) != 0);
2420 }
2421 if (!correct_usage) {
2422 if (msgCode == -1) {
2423 // TODO: Fix callers with msgCode == -1 to use correct validation checks.
Mark Lobodzinskic51dbb72017-04-13 14:25:39 -06002424 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 -06002425 MEMTRACK_INVALID_USAGE_FLAG, "MEM",
2426 "Invalid usage flag for %s 0x%" PRIxLEAST64
2427 " used by %s. In this case, %s should have %s set during creation.",
2428 type_str, obj_handle, func_name, type_str, usage_str);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002429 } else {
2430 const char *valid_usage = (msgCode == -1) ? "" : validation_error_map[msgCode];
Mark Lobodzinski33826372017-04-13 11:10:11 -06002431 skip = log_msg(
Mark Lobodzinskic51dbb72017-04-13 14:25:39 -06002432 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 -06002433 "Invalid usage flag for %s 0x%" PRIxLEAST64 " used by %s. In this case, %s should have %s set during creation. %s",
2434 type_str, obj_handle, func_name, type_str, usage_str, valid_usage);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002435 }
2436 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002437 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002438}
2439
2440// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2441// where an error will be flagged if usage is not correct
Chris Forbes8fdba302017-04-24 18:34:28 -07002442bool ValidateImageUsageFlags(layer_data *device_data, IMAGE_STATE const *image_state, VkFlags desired, bool strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002443 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002444 return validate_usage_flags(device_data, image_state->createInfo.usage, desired, strict,
Mark Lobodzinski33826372017-04-13 11:10:11 -06002445 reinterpret_cast<const uint64_t &>(image_state->image), kVulkanObjectTypeImage, msgCode, func_name,
2446 usage_string);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002447}
2448
2449// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2450// where an error will be flagged if usage is not correct
Chris Forbes8fdba302017-04-24 18:34:28 -07002451bool ValidateBufferUsageFlags(layer_data *device_data, BUFFER_STATE const *buffer_state, VkFlags desired, bool strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002452 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002453 return validate_usage_flags(device_data, buffer_state->createInfo.usage, desired, strict,
Mark Lobodzinski33826372017-04-13 11:10:11 -06002454 reinterpret_cast<const uint64_t &>(buffer_state->buffer), kVulkanObjectTypeBuffer, msgCode,
2455 func_name, usage_string);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002456}
2457
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002458bool PreCallValidateCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo) {
Mark Lobodzinski96210742017-02-09 10:33:46 -07002459 bool skip = false;
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002460 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2461
Mark Lobodzinski96210742017-02-09 10:33:46 -07002462 // TODO: Add check for VALIDATION_ERROR_00658
Mark Lobodzinski96210742017-02-09 10:33:46 -07002463 // TODO: Add check for VALIDATION_ERROR_00667
2464 // TODO: Add check for VALIDATION_ERROR_00668
2465 // TODO: Add check for VALIDATION_ERROR_00669
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002466
2467 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
2468 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2469 VALIDATION_ERROR_00666, "DS",
2470 "vkCreateBuffer(): the sparseBinding device feature is disabled: Buffers cannot be created with the "
2471 "VK_BUFFER_CREATE_SPARSE_BINDING_BIT set. %s",
2472 validation_error_map[VALIDATION_ERROR_00666]);
2473 }
Mark Lobodzinskiaf355062017-03-13 09:35:01 -06002474
2475 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyBuffer)) {
2476 skip |=
2477 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2478 DRAWSTATE_INVALID_FEATURE, "DS",
2479 "vkCreateBuffer(): the sparseResidencyBuffer device feature is disabled: Buffers cannot be created with the "
2480 "VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT set.");
2481 }
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -06002482
2483 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
2484 skip |=
2485 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2486 DRAWSTATE_INVALID_FEATURE, "DS",
2487 "vkCreateBuffer(): the sparseResidencyAliased device feature is disabled: Buffers cannot be created with the "
2488 "VK_BUFFER_CREATE_SPARSE_ALIASED_BIT set.");
2489 }
Mark Lobodzinski96210742017-02-09 10:33:46 -07002490 return skip;
2491}
2492
2493void PostCallRecordCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo, VkBuffer *pBuffer) {
2494 // TODO : This doesn't create deep copy of pQueueFamilyIndices so need to fix that if/when we want that data to be valid
2495 GetBufferMap(device_data)
2496 ->insert(std::make_pair(*pBuffer, std::unique_ptr<BUFFER_STATE>(new BUFFER_STATE(*pBuffer, pCreateInfo))));
2497}
2498
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002499bool PreCallValidateCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo) {
2500 bool skip = false;
2501 BUFFER_STATE *buffer_state = GetBufferState(device_data, pCreateInfo->buffer);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002502 // If this isn't a sparse buffer, it needs to have memory backing it at CreateBufferView time
2503 if (buffer_state) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002504 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCreateBufferView()", VALIDATION_ERROR_02522);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002505 // In order to create a valid buffer view, the buffer must have been created with at least one of the following flags:
2506 // UNIFORM_TEXEL_BUFFER_BIT or STORAGE_TEXEL_BUFFER_BIT
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002507 skip |= ValidateBufferUsageFlags(
2508 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 -07002509 VALIDATION_ERROR_00694, "vkCreateBufferView()", "VK_BUFFER_USAGE_[STORAGE|UNIFORM]_TEXEL_BUFFER_BIT");
2510 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002511 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002512}
2513
2514void PostCallRecordCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo, VkBufferView *pView) {
2515 (*GetBufferViewMap(device_data))[*pView] = std::unique_ptr<BUFFER_VIEW_STATE>(new BUFFER_VIEW_STATE(*pView, pCreateInfo));
2516}
Mark Lobodzinski602de982017-02-09 11:01:33 -07002517
2518// For the given format verify that the aspect masks make sense
2519bool ValidateImageAspectMask(layer_data *device_data, VkImage image, VkFormat format, VkImageAspectFlags aspect_mask,
2520 const char *func_name) {
2521 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2522 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06002523 if (FormatIsColor(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002524 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
2525 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2526 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2527 "%s: Color image formats must have the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2528 validation_error_map[VALIDATION_ERROR_00741]);
2529 } else if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != aspect_mask) {
2530 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2531 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2532 "%s: Color image formats must have ONLY the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2533 validation_error_map[VALIDATION_ERROR_00741]);
2534 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002535 } else if (FormatIsDepthAndStencil(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002536 if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) == 0) {
2537 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2538 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2539 "%s: Depth/stencil image formats must have "
2540 "at least one of VK_IMAGE_ASPECT_DEPTH_BIT "
2541 "and VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2542 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2543 } else if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) != aspect_mask) {
2544 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2545 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2546 "%s: Combination depth/stencil image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT and "
2547 "VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2548 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2549 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002550 } else if (FormatIsDepthOnly(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002551 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) {
2552 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2553 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2554 "%s: Depth-only image formats must have the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2555 validation_error_map[VALIDATION_ERROR_00741]);
2556 } else if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != aspect_mask) {
2557 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2558 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2559 "%s: Depth-only image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2560 validation_error_map[VALIDATION_ERROR_00741]);
2561 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002562 } else if (FormatIsStencilOnly(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002563 if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT) {
2564 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2565 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2566 "%s: Stencil-only image formats must have the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2567 validation_error_map[VALIDATION_ERROR_00741]);
2568 } else if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != aspect_mask) {
2569 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2570 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2571 "%s: Stencil-only image formats can have only the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2572 validation_error_map[VALIDATION_ERROR_00741]);
2573 }
2574 }
2575 return skip;
2576}
2577
2578bool ValidateImageSubrangeLevelLayerCounts(layer_data *device_data, const VkImageSubresourceRange &subresourceRange,
2579 const char *func_name) {
2580 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2581 bool skip = false;
2582 if (subresourceRange.levelCount == 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002583 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 -07002584 VALIDATION_ERROR_00768, "IMAGE", "%s called with 0 in subresourceRange.levelCount. %s", func_name,
2585 validation_error_map[VALIDATION_ERROR_00768]);
2586 }
2587 if (subresourceRange.layerCount == 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002588 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 -07002589 VALIDATION_ERROR_00769, "IMAGE", "%s called with 0 in subresourceRange.layerCount. %s", func_name,
2590 validation_error_map[VALIDATION_ERROR_00769]);
2591 }
2592 return skip;
2593}
2594
2595bool PreCallValidateCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info) {
2596 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2597 bool skip = false;
2598 IMAGE_STATE *image_state = GetImageState(device_data, create_info->image);
2599 if (image_state) {
2600 skip |= ValidateImageUsageFlags(
2601 device_data, image_state,
2602 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
2603 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
2604 false, -1, "vkCreateImageView()",
2605 "VK_IMAGE_USAGE_[SAMPLED|STORAGE|COLOR_ATTACHMENT|DEPTH_STENCIL_ATTACHMENT|INPUT_ATTACHMENT]_BIT");
2606 // If this isn't a sparse image, it needs to have memory backing it at CreateImageView time
2607 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCreateImageView()", VALIDATION_ERROR_02524);
2608 // Checks imported from image layer
Jeremy Hayesf6bfa6b2017-04-04 15:05:52 -06002609 if ((create_info->subresourceRange.baseMipLevel + create_info->subresourceRange.levelCount) >
2610 image_state->createInfo.mipLevels) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002611 std::stringstream ss;
2612 ss << "vkCreateImageView called with baseMipLevel " << create_info->subresourceRange.baseMipLevel << " for image "
2613 << create_info->image << " that only has " << image_state->createInfo.mipLevels << " mip levels.";
2614 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002615 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 -07002616 VALIDATION_ERROR_00768, "IMAGE", "%s %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_00768]);
2617 }
Chris Forbes481df4f2017-05-02 14:18:07 -07002618 if (!GetDeviceExtensions(device_data)->khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002619 if (create_info->subresourceRange.baseArrayLayer >= image_state->createInfo.arrayLayers) {
2620 std::stringstream ss;
2621 ss << "vkCreateImageView called with baseArrayLayer " << create_info->subresourceRange.baseArrayLayer
2622 << " for image " << create_info->image << " that only has " << image_state->createInfo.arrayLayers
2623 << " array layers.";
2624 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2625 VALIDATION_ERROR_00769, "IMAGE", "%s %s", ss.str().c_str(),
2626 validation_error_map[VALIDATION_ERROR_00769]);
2627 }
Mark Lobodzinski602de982017-02-09 11:01:33 -07002628 }
2629 // TODO: Need new valid usage language for levelCount == 0 & layerCount == 0
2630 skip |= ValidateImageSubrangeLevelLayerCounts(device_data, create_info->subresourceRange, "vkCreateImageView()");
2631
2632 VkImageCreateFlags image_flags = image_state->createInfo.flags;
2633 VkFormat image_format = image_state->createInfo.format;
2634 VkFormat view_format = create_info->format;
2635 VkImageAspectFlags aspect_mask = create_info->subresourceRange.aspectMask;
2636
2637 // Validate VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT state
2638 if (image_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
2639 // 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 -06002640 if (FormatCompatibilityClass(image_format) != FormatCompatibilityClass(view_format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002641 std::stringstream ss;
2642 ss << "vkCreateImageView(): ImageView format " << string_VkFormat(view_format)
2643 << " is not in the same format compatibility class as image (" << (uint64_t)create_info->image << ") format "
2644 << string_VkFormat(image_format) << ". Images created with the VK_IMAGE_CREATE_MUTABLE_FORMAT BIT "
2645 << "can support ImageViews with differing formats but they must be in the same compatibility class.";
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002646 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 -07002647 VALIDATION_ERROR_02171, "IMAGE", "%s %s", ss.str().c_str(),
2648 validation_error_map[VALIDATION_ERROR_02171]);
2649 }
2650 } else {
2651 // Format MUST be IDENTICAL to the format the image was created with
2652 if (image_format != view_format) {
2653 std::stringstream ss;
2654 ss << "vkCreateImageView() format " << string_VkFormat(view_format) << " differs from image "
2655 << (uint64_t)create_info->image << " format " << string_VkFormat(image_format)
2656 << ". Formats MUST be IDENTICAL unless VK_IMAGE_CREATE_MUTABLE_FORMAT BIT was set on image creation.";
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002657 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 -07002658 VALIDATION_ERROR_02172, "IMAGE", "%s %s", ss.str().c_str(),
2659 validation_error_map[VALIDATION_ERROR_02172]);
2660 }
2661 }
2662
2663 // Validate correct image aspect bits for desired formats and format consistency
2664 skip |= ValidateImageAspectMask(device_data, image_state->image, image_format, aspect_mask, "vkCreateImageView()");
2665 }
2666 return skip;
2667}
2668
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -07002669void PostCallRecordCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info, VkImageView view) {
2670 auto image_view_map = GetImageViewMap(device_data);
2671 (*image_view_map)[view] = std::unique_ptr<IMAGE_VIEW_STATE>(new IMAGE_VIEW_STATE(view, create_info));
2672
2673 auto image_state = GetImageState(device_data, create_info->image);
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002674 auto &sub_res_range = (*image_view_map)[view].get()->create_info.subresourceRange;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -06002675 sub_res_range.levelCount = ResolveRemainingLevels(&sub_res_range, image_state->createInfo.mipLevels);
2676 sub_res_range.layerCount = ResolveRemainingLayers(&sub_res_range, image_state->createInfo.arrayLayers);
Mark Lobodzinski602de982017-02-09 11:01:33 -07002677}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002678
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002679bool PreCallValidateCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2680 BUFFER_STATE *dst_buffer_state) {
2681 bool skip = false;
2682 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02531);
2683 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02532);
2684 // Validate that SRC & DST buffers have correct usage flags set
2685 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01164,
2686 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
2687 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01165,
2688 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07002689 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyBuffer()",
2690 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01171);
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002691 skip |= ValidateCmd(device_data, cb_node, CMD_COPYBUFFER, "vkCmdCopyBuffer()");
2692 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBuffer()", VALIDATION_ERROR_01172);
2693 return skip;
2694}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002695
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002696void PreCallRecordCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2697 BUFFER_STATE *dst_buffer_state) {
2698 // Update bindings between buffers and cmd buffer
2699 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
2700 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
2701
2702 std::function<bool()> function = [=]() {
2703 return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBuffer()");
2704 };
2705 cb_node->validate_functions.push_back(function);
2706 function = [=]() {
2707 SetBufferMemoryValid(device_data, dst_buffer_state, true);
2708 return false;
2709 };
2710 cb_node->validate_functions.push_back(function);
2711 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFER);
2712}
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002713
2714static bool validateIdleBuffer(layer_data *device_data, VkBuffer buffer) {
2715 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2716 bool skip = false;
2717 auto buffer_state = GetBufferState(device_data, buffer);
2718 if (!buffer_state) {
2719 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2720 __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
2721 "Cannot free buffer 0x%" PRIxLEAST64 " that has not been allocated.", (uint64_t)(buffer));
2722 } else {
2723 if (buffer_state->in_use.load()) {
2724 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2725 __LINE__, VALIDATION_ERROR_00676, "DS",
2726 "Cannot free buffer 0x%" PRIxLEAST64 " that is in use by a command buffer. %s", (uint64_t)(buffer),
2727 validation_error_map[VALIDATION_ERROR_00676]);
2728 }
2729 }
2730 return skip;
2731}
2732
2733bool PreCallValidateDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE **image_view_state,
2734 VK_OBJECT *obj_struct) {
2735 *image_view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski33826372017-04-13 11:10:11 -06002736 *obj_struct = {reinterpret_cast<uint64_t &>(image_view), kVulkanObjectTypeImageView};
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002737 if (GetDisables(device_data)->destroy_image_view) return false;
2738 bool skip = false;
2739 if (*image_view_state) {
2740 skip |= ValidateObjectNotInUse(device_data, *image_view_state, *obj_struct, VALIDATION_ERROR_00776);
2741 }
2742 return skip;
2743}
2744
2745void PostCallRecordDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE *image_view_state,
2746 VK_OBJECT obj_struct) {
2747 // Any bound cmd buffers are now invalid
2748 invalidateCommandBuffers(device_data, image_view_state->cb_bindings, obj_struct);
2749 (*GetImageViewMap(device_data)).erase(image_view);
2750}
2751
2752bool PreCallValidateDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE **buffer_state, VK_OBJECT *obj_struct) {
2753 *buffer_state = GetBufferState(device_data, buffer);
Dave Houlton1150cf52017-04-27 14:38:11 -06002754 *obj_struct = {reinterpret_cast<uint64_t &>(buffer), kVulkanObjectTypeBuffer};
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002755 if (GetDisables(device_data)->destroy_buffer) return false;
2756 bool skip = false;
2757 if (*buffer_state) {
2758 skip |= validateIdleBuffer(device_data, buffer);
2759 }
2760 return skip;
2761}
2762
2763void PostCallRecordDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE *buffer_state, VK_OBJECT obj_struct) {
2764 invalidateCommandBuffers(device_data, buffer_state->cb_bindings, obj_struct);
2765 for (auto mem_binding : buffer_state->GetBoundMemory()) {
2766 auto mem_info = GetMemObjInfo(device_data, mem_binding);
2767 if (mem_info) {
2768 core_validation::RemoveBufferMemoryRange(reinterpret_cast<uint64_t &>(buffer), mem_info);
2769 }
2770 }
Mark Lobodzinski33826372017-04-13 11:10:11 -06002771 ClearMemoryObjectBindings(device_data, reinterpret_cast<uint64_t &>(buffer), kVulkanObjectTypeBuffer);
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002772 GetBufferMap(device_data)->erase(buffer_state->buffer);
2773}
2774
2775bool PreCallValidateDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE **buffer_view_state,
2776 VK_OBJECT *obj_struct) {
2777 *buffer_view_state = GetBufferViewState(device_data, buffer_view);
Dave Houlton1150cf52017-04-27 14:38:11 -06002778 *obj_struct = {reinterpret_cast<uint64_t &>(buffer_view), kVulkanObjectTypeBufferView};
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002779 if (GetDisables(device_data)->destroy_buffer_view) return false;
2780 bool skip = false;
2781 if (*buffer_view_state) {
2782 skip |= ValidateObjectNotInUse(device_data, *buffer_view_state, *obj_struct, VALIDATION_ERROR_00701);
2783 }
2784 return skip;
2785}
2786
2787void PostCallRecordDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE *buffer_view_state,
2788 VK_OBJECT obj_struct) {
2789 // Any bound cmd buffers are now invalid
2790 invalidateCommandBuffers(device_data, buffer_view_state->cb_bindings, obj_struct);
2791 GetBufferViewMap(device_data)->erase(buffer_view);
2792}
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002793
2794bool PreCallValidateCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2795 bool skip = false;
2796 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCmdFillBuffer()", VALIDATION_ERROR_02529);
Mike Schuchardt9c582402017-02-23 15:57:37 -07002797 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdFillBuffer()",
2798 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01141);
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002799 skip |= ValidateCmd(device_data, cb_node, CMD_FILLBUFFER, "vkCmdFillBuffer()");
2800 // Validate that DST buffer has correct usage flags set
2801 skip |= ValidateBufferUsageFlags(device_data, buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01137,
2802 "vkCmdFillBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
2803 skip |= insideRenderPass(device_data, cb_node, "vkCmdFillBuffer()", VALIDATION_ERROR_01142);
2804 return skip;
2805}
2806
2807void PreCallRecordCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2808 std::function<bool()> function = [=]() {
2809 SetBufferMemoryValid(device_data, buffer_state, true);
2810 return false;
2811 };
2812 cb_node->validate_functions.push_back(function);
2813 // Update bindings between buffer and cmd buffer
2814 AddCommandBufferBindingBuffer(device_data, cb_node, buffer_state);
2815 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_FILLBUFFER);
2816}
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002817
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002818bool ValidateBufferImageCopyData(const debug_report_data *report_data, uint32_t regionCount, const VkBufferImageCopy *pRegions,
2819 IMAGE_STATE *image_state, const char *function) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002820 bool skip = false;
2821
2822 for (uint32_t i = 0; i < regionCount; i++) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002823 if (image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) {
2824 if ((pRegions[i].imageOffset.y != 0) || (pRegions[i].imageExtent.height != 1)) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002825 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 -07002826 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01746, "IMAGE",
2827 "%s(): pRegion[%d] imageOffset.y is %d and imageExtent.height is %d. For 1D images these "
2828 "must be 0 and 1, respectively. %s",
2829 function, i, pRegions[i].imageOffset.y, pRegions[i].imageExtent.height,
2830 validation_error_map[VALIDATION_ERROR_01746]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002831 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002832 }
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002833
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002834 if ((image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) || (image_state->createInfo.imageType == VK_IMAGE_TYPE_2D)) {
2835 if ((pRegions[i].imageOffset.z != 0) || (pRegions[i].imageExtent.depth != 1)) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002836 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 -07002837 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01747, "IMAGE",
2838 "%s(): pRegion[%d] imageOffset.z is %d and imageExtent.depth is %d. For 1D and 2D images these "
2839 "must be 0 and 1, respectively. %s",
2840 function, i, pRegions[i].imageOffset.z, pRegions[i].imageExtent.depth,
2841 validation_error_map[VALIDATION_ERROR_01747]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002842 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002843 }
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002844
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002845 if (image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) {
2846 if ((0 != pRegions[i].imageSubresource.baseArrayLayer) || (1 != pRegions[i].imageSubresource.layerCount)) {
2847 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2848 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01281, "IMAGE",
2849 "%s(): pRegion[%d] imageSubresource.baseArrayLayer is %d and imageSubresource.layerCount is "
2850 "%d. For 3D images these must be 0 and 1, respectively. %s",
2851 function, i, pRegions[i].imageSubresource.baseArrayLayer, pRegions[i].imageSubresource.layerCount,
2852 validation_error_map[VALIDATION_ERROR_01281]);
2853 }
2854 }
2855
2856 // If the the calling command's VkImage parameter's format is not a depth/stencil format,
2857 // then bufferOffset must be a multiple of the calling command's VkImage parameter's texel size
Dave Houlton1d2022c2017-03-29 11:43:58 -06002858 auto texel_size = FormatSize(image_state->createInfo.format);
Dave Houlton1150cf52017-04-27 14:38:11 -06002859 if (!FormatIsDepthAndStencil(image_state->createInfo.format) && SafeModulo(pRegions[i].bufferOffset, texel_size) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002860 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2861 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01263, "IMAGE",
2862 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64
2863 " must be a multiple of this format's texel size (" PRINTF_SIZE_T_SPECIFIER "). %s",
2864 function, i, pRegions[i].bufferOffset, texel_size, validation_error_map[VALIDATION_ERROR_01263]);
2865 }
2866
2867 // BufferOffset must be a multiple of 4
Dave Houlton1d2022c2017-03-29 11:43:58 -06002868 if (SafeModulo(pRegions[i].bufferOffset, 4) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002869 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2870 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01264, "IMAGE",
2871 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64 " must be a multiple of 4. %s", function, i,
2872 pRegions[i].bufferOffset, validation_error_map[VALIDATION_ERROR_01264]);
2873 }
2874
2875 // BufferRowLength must be 0, or greater than or equal to the width member of imageExtent
2876 if ((pRegions[i].bufferRowLength != 0) && (pRegions[i].bufferRowLength < pRegions[i].imageExtent.width)) {
2877 skip |= log_msg(
2878 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2879 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01265, "IMAGE",
2880 "%s(): pRegion[%d] bufferRowLength (%d) must be zero or greater-than-or-equal-to imageExtent.width (%d). %s",
2881 function, i, pRegions[i].bufferRowLength, pRegions[i].imageExtent.width,
2882 validation_error_map[VALIDATION_ERROR_01265]);
2883 }
2884
2885 // BufferImageHeight must be 0, or greater than or equal to the height member of imageExtent
2886 if ((pRegions[i].bufferImageHeight != 0) && (pRegions[i].bufferImageHeight < pRegions[i].imageExtent.height)) {
2887 skip |= log_msg(
2888 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2889 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01266, "IMAGE",
2890 "%s(): pRegion[%d] bufferImageHeight (%d) must be zero or greater-than-or-equal-to imageExtent.height (%d). %s",
2891 function, i, pRegions[i].bufferImageHeight, pRegions[i].imageExtent.height,
2892 validation_error_map[VALIDATION_ERROR_01266]);
2893 }
2894
2895 // subresource aspectMask must have exactly 1 bit set
2896 const int num_bits = sizeof(VkFlags) * CHAR_BIT;
2897 std::bitset<num_bits> aspect_mask_bits(pRegions[i].imageSubresource.aspectMask);
2898 if (aspect_mask_bits.count() != 1) {
2899 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2900 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01280, "IMAGE",
2901 "%s: aspectMasks for imageSubresource in each region must have only a single bit set. %s", function,
2902 validation_error_map[VALIDATION_ERROR_01280]);
2903 }
2904
2905 // image subresource aspect bit must match format
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002906 if (!VerifyAspectsPresent(pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002907 skip |= log_msg(
2908 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2909 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01279, "IMAGE",
2910 "%s(): pRegion[%d] subresource aspectMask 0x%x specifies aspects that are not present in image format 0x%x. %s",
2911 function, i, pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format,
2912 validation_error_map[VALIDATION_ERROR_01279]);
2913 }
2914
2915 // Checks that apply only to compressed images
2916 // TODO: there is a comment in ValidateCopyBufferImageTransferGranularityRequirements() in core_validation.cpp that
2917 // reserves a place for these compressed image checks. This block of code could move there once the image
2918 // stuff is moved into core validation.
Dave Houlton1d2022c2017-03-29 11:43:58 -06002919 if (FormatIsCompressed(image_state->createInfo.format)) {
2920 auto block_size = FormatCompressedTexelBlockExtent(image_state->createInfo.format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002921
2922 // BufferRowLength must be a multiple of block width
Dave Houlton1d2022c2017-03-29 11:43:58 -06002923 if (SafeModulo(pRegions[i].bufferRowLength, block_size.width) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002924 skip |= log_msg(
2925 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002926 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01271, "IMAGE",
2927 "%s(): pRegion[%d] bufferRowLength (%d) must be a multiple of the compressed image's texel width (%d). %s.",
2928 function, i, pRegions[i].bufferRowLength, block_size.width, validation_error_map[VALIDATION_ERROR_01271]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002929 }
2930
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002931 // BufferRowHeight must be a multiple of block height
Dave Houlton1d2022c2017-03-29 11:43:58 -06002932 if (SafeModulo(pRegions[i].bufferImageHeight, block_size.height) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002933 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 -07002934 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01272, "IMAGE",
2935 "%s(): pRegion[%d] bufferImageHeight (%d) must be a multiple of the compressed image's texel "
2936 "height (%d). %s.",
2937 function, i, pRegions[i].bufferImageHeight, block_size.height,
2938 validation_error_map[VALIDATION_ERROR_01272]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002939 }
2940
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002941 // image offsets must be multiples of block dimensions
Dave Houlton1d2022c2017-03-29 11:43:58 -06002942 if ((SafeModulo(pRegions[i].imageOffset.x, block_size.width) != 0) ||
2943 (SafeModulo(pRegions[i].imageOffset.y, block_size.height) != 0) ||
2944 (SafeModulo(pRegions[i].imageOffset.z, block_size.depth) != 0)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002945 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2946 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01273, "IMAGE",
2947 "%s(): pRegion[%d] imageOffset(x,y) (%d, %d) must be multiples of the compressed image's texel "
2948 "width & height (%d, %d). %s.",
2949 function, i, pRegions[i].imageOffset.x, pRegions[i].imageOffset.y, block_size.width,
2950 block_size.height, validation_error_map[VALIDATION_ERROR_01273]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002951 }
2952
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002953 // bufferOffset must be a multiple of block size (linear bytes)
Dave Houlton1d2022c2017-03-29 11:43:58 -06002954 size_t block_size_in_bytes = FormatSize(image_state->createInfo.format);
2955 if (SafeModulo(pRegions[i].bufferOffset, block_size_in_bytes) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002956 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2957 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01274, "IMAGE",
2958 "%s(): pRegion[%d] bufferOffset (0x%" PRIxLEAST64
2959 ") must be a multiple of the compressed image's texel block "
2960 "size (" PRINTF_SIZE_T_SPECIFIER "). %s.",
2961 function, i, pRegions[i].bufferOffset, block_size_in_bytes,
2962 validation_error_map[VALIDATION_ERROR_01274]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002963 }
Dave Houlton67e9b532017-03-02 17:00:10 -07002964
2965 // imageExtent width must be a multiple of block width, or extent+offset width must equal subresource width
Dave Houlton75967fc2017-03-06 17:21:16 -07002966 VkExtent3D mip_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
Dave Houlton1d2022c2017-03-29 11:43:58 -06002967 if ((SafeModulo(pRegions[i].imageExtent.width, block_size.width) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002968 (pRegions[i].imageExtent.width + pRegions[i].imageOffset.x != mip_extent.width)) {
2969 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2970 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01275, "IMAGE",
2971 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block width "
2972 "(%d), or when added to offset.x (%d) must equal the image subresource width (%d). %s.",
2973 function, i, pRegions[i].imageExtent.width, block_size.width, pRegions[i].imageOffset.x,
2974 mip_extent.width, validation_error_map[VALIDATION_ERROR_01275]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002975 }
2976
2977 // imageExtent height must be a multiple of block height, or extent+offset height must equal subresource height
Dave Houlton1d2022c2017-03-29 11:43:58 -06002978 if ((SafeModulo(pRegions[i].imageExtent.height, block_size.height) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002979 (pRegions[i].imageExtent.height + pRegions[i].imageOffset.y != mip_extent.height)) {
2980 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2981 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01276, "IMAGE",
2982 "%s(): pRegion[%d] extent height (%d) must be a multiple of the compressed texture block height "
2983 "(%d), or when added to offset.y (%d) must equal the image subresource height (%d). %s.",
2984 function, i, pRegions[i].imageExtent.height, block_size.height, pRegions[i].imageOffset.y,
2985 mip_extent.height, validation_error_map[VALIDATION_ERROR_01276]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002986 }
2987
2988 // imageExtent depth must be a multiple of block depth, or extent+offset depth must equal subresource depth
Dave Houlton1d2022c2017-03-29 11:43:58 -06002989 if ((SafeModulo(pRegions[i].imageExtent.depth, block_size.depth) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002990 (pRegions[i].imageExtent.depth + pRegions[i].imageOffset.z != mip_extent.depth)) {
2991 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2992 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01277, "IMAGE",
2993 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block depth "
2994 "(%d), or when added to offset.z (%d) must equal the image subresource depth (%d). %s.",
2995 function, i, pRegions[i].imageExtent.depth, block_size.depth, pRegions[i].imageOffset.z,
2996 mip_extent.depth, validation_error_map[VALIDATION_ERROR_01277]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002997 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002998 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002999 }
3000
3001 return skip;
3002}
3003
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003004static bool ValidateImageBounds(const debug_report_data *report_data, const IMAGE_STATE *image_state, const uint32_t regionCount,
3005 const VkBufferImageCopy *pRegions, const char *func_name, UNIQUE_VALIDATION_ERROR_CODE msg_code) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003006 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003007 const VkImageCreateInfo *image_info = &(image_state->createInfo);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003008
3009 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003010 VkExtent3D extent = pRegions[i].imageExtent;
3011 VkOffset3D offset = pRegions[i].imageOffset;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003012
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003013 if (IsExtentSizeZero(&extent)) // Warn on zero area subresource
3014 {
3015 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3016 (uint64_t)0, __LINE__, IMAGE_ZERO_AREA_SUBREGION, "IMAGE",
3017 "%s: pRegion[%d] imageExtent of {%1d, %1d, %1d} has zero area", func_name, i, extent.width,
3018 extent.height, extent.depth);
3019 }
3020
3021 VkExtent3D image_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
3022
3023 // 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 -06003024 if (FormatIsCompressed(image_info->format)) {
3025 auto block_extent = FormatCompressedTexelBlockExtent(image_info->format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003026 if (image_extent.width % block_extent.width) {
3027 image_extent.width += (block_extent.width - (image_extent.width % block_extent.width));
3028 }
3029 if (image_extent.height % block_extent.height) {
3030 image_extent.height += (block_extent.height - (image_extent.height % block_extent.height));
3031 }
3032 if (image_extent.depth % block_extent.depth) {
3033 image_extent.depth += (block_extent.depth - (image_extent.depth % block_extent.depth));
3034 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003035 }
3036
Dave Houltonfc1a4052017-04-27 14:32:45 -06003037 if (0 != ExceedsBounds(&offset, &extent, &image_extent)) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003038 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 -07003039 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds image bounds. %s.", func_name, i,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003040 validation_error_map[msg_code]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07003041 }
3042 }
3043
3044 return skip;
3045}
3046
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003047static inline bool ValidtateBufferBounds(const debug_report_data *report_data, IMAGE_STATE *image_state, BUFFER_STATE *buff_state,
3048 uint32_t regionCount, const VkBufferImageCopy *pRegions, const char *func_name,
3049 UNIQUE_VALIDATION_ERROR_CODE msg_code) {
3050 bool skip = false;
3051
3052 VkDeviceSize buffer_size = buff_state->createInfo.size;
3053
3054 for (uint32_t i = 0; i < regionCount; i++) {
3055 VkExtent3D copy_extent = pRegions[i].imageExtent;
3056
3057 VkDeviceSize buffer_width = (0 == pRegions[i].bufferRowLength ? copy_extent.width : pRegions[i].bufferRowLength);
3058 VkDeviceSize buffer_height = (0 == pRegions[i].bufferImageHeight ? copy_extent.height : pRegions[i].bufferImageHeight);
Dave Houlton1d2022c2017-03-29 11:43:58 -06003059 VkDeviceSize unit_size = FormatSize(image_state->createInfo.format); // size (bytes) of texel or block
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003060
Dave Houltonf3229d52017-02-21 15:59:08 -07003061 // Handle special buffer packing rules for specific depth/stencil formats
3062 if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Dave Houlton1d2022c2017-03-29 11:43:58 -06003063 unit_size = FormatSize(VK_FORMAT_S8_UINT);
Dave Houltonf3229d52017-02-21 15:59:08 -07003064 } else if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
3065 switch (image_state->createInfo.format) {
3066 case VK_FORMAT_D16_UNORM_S8_UINT:
Dave Houlton1d2022c2017-03-29 11:43:58 -06003067 unit_size = FormatSize(VK_FORMAT_D16_UNORM);
Dave Houltonf3229d52017-02-21 15:59:08 -07003068 break;
3069 case VK_FORMAT_D32_SFLOAT_S8_UINT:
Dave Houlton1d2022c2017-03-29 11:43:58 -06003070 unit_size = FormatSize(VK_FORMAT_D32_SFLOAT);
Dave Houltonf3229d52017-02-21 15:59:08 -07003071 break;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003072 case VK_FORMAT_X8_D24_UNORM_PACK32: // Fall through
Dave Houltonf3229d52017-02-21 15:59:08 -07003073 case VK_FORMAT_D24_UNORM_S8_UINT:
3074 unit_size = 4;
3075 break;
3076 default:
3077 break;
3078 }
3079 }
3080
Dave Houlton1d2022c2017-03-29 11:43:58 -06003081 if (FormatIsCompressed(image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003082 // Switch to texel block units, rounding up for any partially-used blocks
Dave Houlton1d2022c2017-03-29 11:43:58 -06003083 auto block_dim = FormatCompressedTexelBlockExtent(image_state->createInfo.format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003084 buffer_width = (buffer_width + block_dim.width - 1) / block_dim.width;
3085 buffer_height = (buffer_height + block_dim.height - 1) / block_dim.height;
3086
3087 copy_extent.width = (copy_extent.width + block_dim.width - 1) / block_dim.width;
3088 copy_extent.height = (copy_extent.height + block_dim.height - 1) / block_dim.height;
3089 copy_extent.depth = (copy_extent.depth + block_dim.depth - 1) / block_dim.depth;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003090 }
3091
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003092 // Either depth or layerCount may be greater than 1 (not both). This is the number of 'slices' to copy
3093 uint32_t z_copies = std::max(copy_extent.depth, pRegions[i].imageSubresource.layerCount);
3094 if (IsExtentSizeZero(&copy_extent) || (0 == z_copies)) {
3095 // TODO: Issure warning here? Already warned in ValidateImageBounds()...
3096 } else {
3097 // Calculate buffer offset of final copied byte, + 1.
3098 VkDeviceSize max_buffer_offset = (z_copies - 1) * buffer_height * buffer_width; // offset to slice
3099 max_buffer_offset += ((copy_extent.height - 1) * buffer_width) + copy_extent.width; // add row,col
3100 max_buffer_offset *= unit_size; // convert to bytes
3101 max_buffer_offset += pRegions[i].bufferOffset; // add initial offset (bytes)
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003102
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003103 if (buffer_size < max_buffer_offset) {
3104 skip |=
3105 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)0,
3106 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds buffer size of %" PRIu64 " bytes. %s.", func_name,
3107 i, buffer_size, validation_error_map[msg_code]);
3108 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003109 }
3110 }
3111
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07003112 return skip;
3113}
3114
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003115bool PreCallValidateCmdCopyImageToBuffer(layer_data *device_data, VkImageLayout srcImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003116 IMAGE_STATE *src_image_state, BUFFER_STATE *dst_buffer_state, uint32_t regionCount,
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07003117 const VkBufferImageCopy *pRegions, const char *func_name) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003118 const debug_report_data *report_data = core_validation::GetReportData(device_data);
3119 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, src_image_state, "vkCmdCopyImageToBuffer");
3120
3121 // Validate command buffer state
3122 if (CB_RECORDING != cb_node->state) {
3123 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3124 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01258, "DS",
3125 "Cannot call vkCmdCopyImageToBuffer() on command buffer which is not in recording state. %s.",
3126 validation_error_map[VALIDATION_ERROR_01258]);
3127 } else {
3128 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYIMAGETOBUFFER);
3129 }
3130
3131 // Command pool must support graphics, compute, or transfer operations
3132 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
3133
3134 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
3135 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
3136 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3137 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01259, "DS",
3138 "Cannot call vkCmdCopyImageToBuffer() on a command buffer allocated from a pool without graphics, compute, "
3139 "or transfer capabilities. %s.",
3140 validation_error_map[VALIDATION_ERROR_01259]);
3141 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003142 skip |= ValidateImageBounds(report_data, src_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003143 VALIDATION_ERROR_01245);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003144 skip |= ValidtateBufferBounds(report_data, src_image_state, dst_buffer_state, regionCount, pRegions, "vkCmdCopyImageToBuffer()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003145 VALIDATION_ERROR_01246);
3146
3147 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyImageToBuffer(): srcImage",
3148 VALIDATION_ERROR_01249);
3149 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02537);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003150 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02538);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003151
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003152 // Validate that SRC image & DST buffer have correct usage flags set
3153 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01248,
3154 "vkCmdCopyImageToBuffer()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003155 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01252,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003156 "vkCmdCopyImageToBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003157 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01260);
Tobin Ehlisc8266452017-04-07 12:20:30 -06003158 bool hit_error = false;
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003159 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlisc8266452017-04-07 12:20:30 -06003160 skip |=
3161 VerifyImageLayout(device_data, cb_node, src_image_state, pRegions[i].imageSubresource, srcImageLayout,
3162 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01251, &hit_error);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003163 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, src_image_state, &pRegions[i], i,
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06003164 "vkCmdCopyImageToBuffer()");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003165 }
3166 return skip;
3167}
3168
3169void PreCallRecordCmdCopyImageToBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003170 BUFFER_STATE *dst_buffer_state, uint32_t region_count, const VkBufferImageCopy *regions,
3171 VkImageLayout src_image_layout) {
3172 // Make sure that all image slices are updated to correct layout
3173 for (uint32_t i = 0; i < region_count; ++i) {
3174 SetImageLayout(device_data, cb_node, src_image_state, regions[i].imageSubresource, src_image_layout);
3175 }
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003176 // Update bindings between buffer/image and cmd buffer
3177 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003178 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003179
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003180 std::function<bool()> function = [=]() {
3181 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImageToBuffer()");
3182 };
3183 cb_node->validate_functions.push_back(function);
3184 function = [=]() {
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003185 SetBufferMemoryValid(device_data, dst_buffer_state, true);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003186 return false;
3187 };
3188 cb_node->validate_functions.push_back(function);
3189
3190 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGETOBUFFER);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003191}
3192
3193bool PreCallValidateCmdCopyBufferToImage(layer_data *device_data, VkImageLayout dstImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003194 BUFFER_STATE *src_buffer_state, IMAGE_STATE *dst_image_state, uint32_t regionCount,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003195 const VkBufferImageCopy *pRegions, const char *func_name) {
3196 const debug_report_data *report_data = core_validation::GetReportData(device_data);
3197 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, dst_image_state, "vkCmdCopyBufferToImage");
3198
3199 // Validate command buffer state
3200 if (CB_RECORDING != cb_node->state) {
3201 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3202 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01240, "DS",
3203 "Cannot call vkCmdCopyBufferToImage() on command buffer which is not in recording state. %s.",
3204 validation_error_map[VALIDATION_ERROR_01240]);
3205 } else {
3206 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYBUFFERTOIMAGE);
3207 }
3208
3209 // Command pool must support graphics, compute, or transfer operations
3210 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
3211 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
3212 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
3213 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3214 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01241, "DS",
3215 "Cannot call vkCmdCopyBufferToImage() on a command buffer allocated from a pool without graphics, compute, "
3216 "or transfer capabilities. %s.",
3217 validation_error_map[VALIDATION_ERROR_01241]);
3218 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003219 skip |= ValidateImageBounds(report_data, dst_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003220 VALIDATION_ERROR_01228);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003221 skip |= ValidtateBufferBounds(report_data, dst_image_state, src_buffer_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003222 VALIDATION_ERROR_01227);
3223 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyBufferToImage(): dstImage",
3224 VALIDATION_ERROR_01232);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003225 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02535);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003226 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02536);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003227 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01230,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003228 "vkCmdCopyBufferToImage()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
3229 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01231,
3230 "vkCmdCopyBufferToImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003231 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01242);
Tobin Ehlisc8266452017-04-07 12:20:30 -06003232 bool hit_error = false;
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003233 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlisc8266452017-04-07 12:20:30 -06003234 skip |=
3235 VerifyImageLayout(device_data, cb_node, dst_image_state, pRegions[i].imageSubresource, dstImageLayout,
3236 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01234, &hit_error);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003237 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &pRegions[i], i,
3238 "vkCmdCopyBufferToImage()");
3239 }
3240 return skip;
3241}
3242
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003243void PreCallRecordCmdCopyBufferToImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003244 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkBufferImageCopy *regions,
3245 VkImageLayout dst_image_layout) {
3246 // Make sure that all image slices are updated to correct layout
3247 for (uint32_t i = 0; i < region_count; ++i) {
3248 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].imageSubresource, dst_image_layout);
3249 }
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003250 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003251 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003252 std::function<bool()> function = [=]() {
3253 SetImageMemoryValid(device_data, dst_image_state, true);
3254 return false;
3255 };
3256 cb_node->validate_functions.push_back(function);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003257 function = [=]() { return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBufferToImage()"); };
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003258 cb_node->validate_functions.push_back(function);
3259
3260 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFERTOIMAGE);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07003261}
Mike Weiblen672b58b2017-02-21 14:32:53 -07003262
3263bool PreCallValidateGetImageSubresourceLayout(layer_data *device_data, VkImage image, const VkImageSubresource *pSubresource) {
3264 const auto report_data = core_validation::GetReportData(device_data);
3265 bool skip = false;
3266 const VkImageAspectFlags sub_aspect = pSubresource->aspectMask;
3267
3268 // VU 00733: The aspectMask member of pSubresource must only have a single bit set
3269 const int num_bits = sizeof(sub_aspect) * CHAR_BIT;
3270 std::bitset<num_bits> aspect_mask_bits(sub_aspect);
3271 if (aspect_mask_bits.count() != 1) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003272 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3273 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00733, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003274 "vkGetImageSubresourceLayout(): VkImageSubresource.aspectMask must have exactly 1 bit set. %s",
3275 validation_error_map[VALIDATION_ERROR_00733]);
3276 }
3277
3278 IMAGE_STATE *image_entry = GetImageState(device_data, image);
3279 if (!image_entry) {
3280 return skip;
3281 }
3282
3283 // VU 00732: image must have been created with tiling equal to VK_IMAGE_TILING_LINEAR
3284 if (image_entry->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003285 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3286 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00732, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003287 "vkGetImageSubresourceLayout(): Image must have tiling of VK_IMAGE_TILING_LINEAR. %s",
3288 validation_error_map[VALIDATION_ERROR_00732]);
3289 }
3290
3291 // VU 00739: mipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
3292 if (pSubresource->mipLevel >= image_entry->createInfo.mipLevels) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003293 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3294 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00739, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003295 "vkGetImageSubresourceLayout(): pSubresource.mipLevel (%d) must be less than %d. %s",
3296 pSubresource->mipLevel, image_entry->createInfo.mipLevels, validation_error_map[VALIDATION_ERROR_00739]);
3297 }
3298
3299 // VU 00740: arrayLayer must be less than the arrayLayers specified in VkImageCreateInfo when the image was created
3300 if (pSubresource->arrayLayer >= image_entry->createInfo.arrayLayers) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003301 skip |=
3302 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3303 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00740, "IMAGE",
3304 "vkGetImageSubresourceLayout(): pSubresource.arrayLayer (%d) must be less than %d. %s",
3305 pSubresource->arrayLayer, image_entry->createInfo.arrayLayers, validation_error_map[VALIDATION_ERROR_00740]);
Mike Weiblen672b58b2017-02-21 14:32:53 -07003306 }
3307
3308 // VU 00741: subresource's aspect must be compatible with image's format.
3309 const VkFormat img_format = image_entry->createInfo.format;
Dave Houlton1d2022c2017-03-29 11:43:58 -06003310 if (FormatIsColor(img_format)) {
Mike Weiblen672b58b2017-02-21 14:32:53 -07003311 if (sub_aspect != VK_IMAGE_ASPECT_COLOR_BIT) {
3312 skip |= log_msg(
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003313 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3314 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003315 "vkGetImageSubresourceLayout(): For color formats, VkImageSubresource.aspectMask must be VK_IMAGE_ASPECT_COLOR. %s",
3316 validation_error_map[VALIDATION_ERROR_00741]);
3317 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06003318 } else if (FormatIsDepthOrStencil(img_format)) {
Mike Weiblen672b58b2017-02-21 14:32:53 -07003319 if ((sub_aspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (sub_aspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003320 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3321 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003322 "vkGetImageSubresourceLayout(): For depth/stencil formats, VkImageSubresource.aspectMask must be "
3323 "either VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT. %s",
3324 validation_error_map[VALIDATION_ERROR_00741]);
3325 }
3326 }
3327 return skip;
3328}