blob: 5358f7ce0c6ec7aef6f907f09e14ca58f24cb14c [file] [log] [blame]
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001/*
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002 *
Courtney Goeltzenleuchterfcbe16f2015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Michael Lentineb6986752015-10-06 14:56:18 -07004 * Copyright (C) 2015 Google, Inc.
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060023 *
Tobin Ehlis88452832015-12-03 09:40:56 -070024 * Author: Cody Northrop <cnorthrop@google.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060025 * Author: Michael Lentine <mlentine@google.com>
Tobin Ehlis88452832015-12-03 09:40:56 -070026 * Author: Tobin Ehlis <tobine@google.com>
27 * Author: Chia-I Wu <olv@google.com>
Tobin Ehlis7e2ad752015-12-01 09:48:58 -070028 * Author: Chris Forbes <chrisf@ijw.co.nz>
Mark Lobodzinski78da8cb2015-12-28 15:51:07 -070029 * Author: Mark Lobodzinski <mark@lunarg.com>
Tobin Ehlis10ae8c12015-03-17 16:24:32 -060030 */
31
Mark Lobodzinski78da8cb2015-12-28 15:51:07 -070032// Allow use of STL min and max functions in Windows
33#define NOMINMAX
34
Tobin Ehlis10ae8c12015-03-17 16:24:32 -060035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
Tobin Ehlis7e2ad752015-12-01 09:48:58 -070038#include <assert.h>
Tobin Ehlis10ae8c12015-03-17 16:24:32 -060039#include <unordered_map>
Michael Lentineb6986752015-10-06 14:56:18 -070040#include <unordered_set>
Tobin Ehlis7e2ad752015-12-01 09:48:58 -070041#include <map>
42#include <string>
43#include <iostream>
44#include <algorithm>
Mark Lobodzinski39298632015-11-18 08:38:27 -070045#include <list>
Tobin Ehlis7e2ad752015-12-01 09:48:58 -070046#include <spirv.hpp>
Tobin Ehlis88452832015-12-03 09:40:56 -070047#include <set>
Tobin Ehlis10ae8c12015-03-17 16:24:32 -060048
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060049#include "vk_loader_platform.h"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060050#include "vk_dispatch_table_helper.h"
51#include "vk_struct_string_helper_cpp.h"
Tony Barbour18f71552015-04-22 11:36:22 -060052#if defined(__GNUC__)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -060053#pragma GCC diagnostic ignored "-Wwrite-strings"
Tony Barbour18f71552015-04-22 11:36:22 -060054#endif
Tony Barbour18f71552015-04-22 11:36:22 -060055#if defined(__GNUC__)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -060056#pragma GCC diagnostic warning "-Wwrite-strings"
Tony Barbour18f71552015-04-22 11:36:22 -060057#endif
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060058#include "vk_struct_size_helper.h"
Tobin Ehlis10ae8c12015-03-17 16:24:32 -060059#include "draw_state.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060060#include "vk_layer_config.h"
Michael Lentine97eb7462015-11-20 09:48:52 -080061#include "vulkan/vk_debug_marker_layer.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060062#include "vk_layer_table.h"
63#include "vk_layer_debug_marker_table.h"
64#include "vk_layer_data.h"
65#include "vk_layer_logging.h"
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -060066#include "vk_layer_extension_utils.h"
Tobin Ehlisa1c28562015-10-23 16:00:08 -060067#include "vk_layer_utils.h"
Tobin Ehlis10ae8c12015-03-17 16:24:32 -060068
Mark Lobodzinski31e5f282015-11-30 16:48:53 -070069// This definition controls whether image layout transitions are enabled/disabled.
70// disable until corner cases are fixed
71#define DISABLE_IMAGE_LAYOUT_VALIDATION
72
Tobin Ehlis7e2ad752015-12-01 09:48:58 -070073using std::unordered_map;
74using std::unordered_set;
75
Tobin Ehlisac0ef842015-12-14 13:46:38 -070076// Track command pools and their command buffers
77struct CMD_POOL_INFO {
78 VkCommandPoolCreateFlags createFlags;
79 list<VkCommandBuffer> commandBuffers; // list container of cmd buffers allocated from this pool
80};
81
Tobin Ehlis0b632332015-10-07 09:38:40 -060082struct devExts {
Courtney Goeltzenleuchter6ed5dc22015-11-03 15:41:43 -070083 VkBool32 debug_marker_enabled;
Michael Lentineabc5e922015-10-12 11:30:14 -050084 VkBool32 wsi_enabled;
85 unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE*> swapchainMap;
Tobin Ehlis0b632332015-10-07 09:38:40 -060086};
87
Tobin Ehlis7e2ad752015-12-01 09:48:58 -070088// fwd decls
89struct shader_module;
90struct render_pass;
91
Cody Northrop55443ef2015-09-28 15:09:32 -060092struct layer_data {
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -070093 debug_report_data* report_data;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070094 std::vector<VkDebugReportCallbackEXT> logging_callback;
Tobin Ehlis0b632332015-10-07 09:38:40 -060095 VkLayerDispatchTable* device_dispatch_table;
96 VkLayerInstanceDispatchTable* instance_dispatch_table;
97 devExts device_extensions;
Tobin Ehlisae82e7f2016-01-20 16:23:37 -070098 vector<VkQueue> queues; // all queues under given device
Tobin Ehlis74714d22016-01-25 15:24:34 -080099 // Global set of all cmdBuffers that are inFlight on this device
100 unordered_set<VkCommandBuffer> globalInFlightCmdBuffers;
Tobin Ehlisb212dfc2015-10-07 15:40:22 -0600101 // Layer specific data
Mark Lobodzinski39298632015-11-18 08:38:27 -0700102 unordered_map<VkSampler, unique_ptr<SAMPLER_NODE>> sampleMap;
103 unordered_map<VkImageView, unique_ptr<VkImageViewCreateInfo>> imageViewMap;
104 unordered_map<VkImage, unique_ptr<VkImageCreateInfo>> imageMap;
105 unordered_map<VkBufferView, unique_ptr<VkBufferViewCreateInfo>> bufferViewMap;
Michael Lentine700b0aa2015-10-30 17:57:32 -0700106 unordered_map<VkBuffer, BUFFER_NODE> bufferMap;
Mark Lobodzinski39298632015-11-18 08:38:27 -0700107 unordered_map<VkPipeline, PIPELINE_NODE*> pipelineMap;
Tobin Ehlisac0ef842015-12-14 13:46:38 -0700108 unordered_map<VkCommandPool, CMD_POOL_INFO> commandPoolMap;
Mark Lobodzinski39298632015-11-18 08:38:27 -0700109 unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE*> descriptorPoolMap;
110 unordered_map<VkDescriptorSet, SET_NODE*> setMap;
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -0700111 unordered_map<VkDescriptorSetLayout, LAYOUT_NODE*> descriptorSetLayoutMap;
Mark Lobodzinski39298632015-11-18 08:38:27 -0700112 unordered_map<VkPipelineLayout, PIPELINE_LAYOUT_NODE> pipelineLayoutMap;
113 unordered_map<VkDeviceMemory, VkImage> memImageMap;
Mark Lobodzinski8da0d1e2016-01-06 14:58:59 -0700114 unordered_map<VkFence, FENCE_NODE> fenceMap;
115 unordered_map<VkQueue, QUEUE_NODE> queueMap;
Michael Lentineb887b0a2015-12-29 14:12:11 -0600116 unordered_map<VkEvent, EVENT_NODE> eventMap;
117 unordered_map<QueryObject, bool> queryToStateMap;
Michael Lentine15a47882016-01-06 10:05:48 -0600118 unordered_map<VkSemaphore, uint32_t> semaphoreSignaledMap;
Mark Lobodzinski39298632015-11-18 08:38:27 -0700119 unordered_map<void*, GLOBAL_CB_NODE*> commandBufferMap;
120 unordered_map<VkFramebuffer, VkFramebufferCreateInfo*> frameBufferMap;
121 unordered_map<VkImage, IMAGE_NODE*> imageLayoutMap;
122 unordered_map<VkRenderPass, RENDER_PASS_NODE*> renderPassMap;
Michael Lentine15a47882016-01-06 10:05:48 -0600123 unordered_map<VkShaderModule, shader_module*> shaderModuleMap;
Michael Lentineabc5e922015-10-12 11:30:14 -0500124 // Current render pass
Michael Lentine15a47882016-01-06 10:05:48 -0600125 VkRenderPassBeginInfo renderPassBeginInfo;
126 uint32_t currentSubpass;
Mark Lobodzinski941aea92016-01-13 10:23:15 -0700127 unordered_map<VkDevice, VkPhysicalDeviceProperties> physDevPropertyMap;
Cody Northrop55443ef2015-09-28 15:09:32 -0600128
129 layer_data() :
130 report_data(nullptr),
Tobin Ehlis0b632332015-10-07 09:38:40 -0600131 device_dispatch_table(nullptr),
132 instance_dispatch_table(nullptr),
133 device_extensions()
Cody Northrop55443ef2015-09-28 15:09:32 -0600134 {};
135};
Michael Lentine15a47882016-01-06 10:05:48 -0600136
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700137// Code imported from ShaderChecker
138static void
Chris Forbes21977d92016-01-26 13:41:39 +1300139build_def_index(shader_module *);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700140
Chris Forbesc7e2e202016-01-18 08:56:40 +1300141// A forward iterator over spirv instructions. Provides easy access to len, opcode, and content words
142// without the caller needing to care too much about the physical SPIRV module layout.
143struct spirv_inst_iter {
144 std::vector<uint32_t>::const_iterator zero;
145 std::vector<uint32_t>::const_iterator it;
146
147 uint32_t len() { return *it >> 16; }
148 uint32_t opcode() { return *it & 0x0ffffu; }
149 uint32_t const & word(unsigned n) { return it[n]; }
Mark Youngd652d132016-01-25 13:37:06 -0700150 uint32_t offset() { return (uint32_t)(it - zero); }
Chris Forbesc7e2e202016-01-18 08:56:40 +1300151
152 spirv_inst_iter(std::vector<uint32_t>::const_iterator zero,
153 std::vector<uint32_t>::const_iterator it) : zero(zero), it(it) {}
154
155 bool operator== (spirv_inst_iter const & other) {
156 return it == other.it;
157 }
158
159 bool operator!= (spirv_inst_iter const & other) {
160 return it != other.it;
161 }
162
163 spirv_inst_iter operator++ (int) { /* x++ */
164 spirv_inst_iter ii = *this;
165 it += len();
166 return ii;
167 }
168
169 spirv_inst_iter operator++ () { /* ++x; */
170 it += len();
171 return *this;
172 }
173
174 /* The iterator and the value are the same thing. */
175 spirv_inst_iter & operator* () { return *this; }
176 spirv_inst_iter const & operator* () const { return *this; }
177};
178
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700179struct shader_module {
180 /* the spirv image itself */
181 vector<uint32_t> words;
182 /* a mapping of <id> to the first word of its def. this is useful because walking type
Chris Forbes21977d92016-01-26 13:41:39 +1300183 * trees, constant expressions, etc requires jumping all over the instruction stream.
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700184 */
Chris Forbes21977d92016-01-26 13:41:39 +1300185 unordered_map<unsigned, unsigned> def_index;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700186
187 shader_module(VkShaderModuleCreateInfo const *pCreateInfo) :
188 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
Chris Forbes21977d92016-01-26 13:41:39 +1300189 def_index() {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700190
Chris Forbes21977d92016-01-26 13:41:39 +1300191 build_def_index(this);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700192 }
Chris Forbesc7e2e202016-01-18 08:56:40 +1300193
194 /* expose begin() / end() to enable range-based for */
195 spirv_inst_iter begin() const { return spirv_inst_iter(words.begin(), words.begin() + 5); } /* first insn */
196 spirv_inst_iter end() const { return spirv_inst_iter(words.begin(), words.end()); } /* just past last insn */
197 /* given an offset into the module, produce an iterator there. */
198 spirv_inst_iter at(unsigned offset) const { return spirv_inst_iter(words.begin(), words.begin() + offset); }
Chris Forbes1257f912016-01-18 12:07:01 +1300199
Chris Forbes21977d92016-01-26 13:41:39 +1300200 /* gets an iterator to the definition of an id */
201 spirv_inst_iter get_def(unsigned id) const {
202 auto it = def_index.find(id);
203 if (it == def_index.end()) {
Chris Forbes1257f912016-01-18 12:07:01 +1300204 return end();
205 }
206 return at(it->second);
207 }
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700208};
209
Tobin Ehlisb212dfc2015-10-07 15:40:22 -0600210// TODO : Do we need to guard access to layer_data_map w/ lock?
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -0700211static unordered_map<void*, layer_data*> layer_data_map;
Courtney Goeltzenleuchter3d0dfad2015-06-13 21:23:09 -0600212
Tobin Ehlis10ae8c12015-03-17 16:24:32 -0600213static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
214// TODO : This can be much smarter, using separate locks for separate global data
215static int globalLockInitialized = 0;
216static loader_platform_thread_mutex globalLock;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -0600217#define MAX_TID 513
218static loader_platform_thread_id g_tidMapping[MAX_TID] = {0};
219static uint32_t g_maxTID = 0;
Tobin Ehlisa16e8922015-06-16 15:50:44 -0600220
221template layer_data *get_my_data_ptr<layer_data>(
222 void *data_key,
223 std::unordered_map<void *, layer_data *> &data_map);
224
Tobin Ehlis10ae8c12015-03-17 16:24:32 -0600225// Map actual TID to an index value and return that index
226// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs
227static uint32_t getTIDIndex() {
228 loader_platform_thread_id tid = loader_platform_get_thread_id();
229 for (uint32_t i = 0; i < g_maxTID; i++) {
230 if (tid == g_tidMapping[i])
231 return i;
232 }
233 // Don't yet have mapping, set it and return newly set index
234 uint32_t retVal = (uint32_t) g_maxTID;
235 g_tidMapping[g_maxTID++] = tid;
236 assert(g_maxTID < MAX_TID);
237 return retVal;
238}
Tobin Ehlis559c6382015-11-05 09:52:49 -0700239
Tobin Ehlis10ae8c12015-03-17 16:24:32 -0600240// Return a string representation of CMD_TYPE enum
241static string cmdTypeToString(CMD_TYPE cmd)
242{
243 switch (cmd)
244 {
245 case CMD_BINDPIPELINE:
246 return "CMD_BINDPIPELINE";
247 case CMD_BINDPIPELINEDELTA:
248 return "CMD_BINDPIPELINEDELTA";
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600249 case CMD_SETVIEWPORTSTATE:
250 return "CMD_SETVIEWPORTSTATE";
251 case CMD_SETLINEWIDTHSTATE:
252 return "CMD_SETLINEWIDTHSTATE";
253 case CMD_SETDEPTHBIASSTATE:
254 return "CMD_SETDEPTHBIASSTATE";
255 case CMD_SETBLENDSTATE:
256 return "CMD_SETBLENDSTATE";
257 case CMD_SETDEPTHBOUNDSSTATE:
258 return "CMD_SETDEPTHBOUNDSSTATE";
259 case CMD_SETSTENCILREADMASKSTATE:
260 return "CMD_SETSTENCILREADMASKSTATE";
261 case CMD_SETSTENCILWRITEMASKSTATE:
262 return "CMD_SETSTENCILWRITEMASKSTATE";
263 case CMD_SETSTENCILREFERENCESTATE:
264 return "CMD_SETSTENCILREFERENCESTATE";
Tobin Ehlis793ad302015-04-03 12:01:11 -0600265 case CMD_BINDDESCRIPTORSETS:
266 return "CMD_BINDDESCRIPTORSETS";
Tobin Ehlis10ae8c12015-03-17 16:24:32 -0600267 case CMD_BINDINDEXBUFFER:
268 return "CMD_BINDINDEXBUFFER";
269 case CMD_BINDVERTEXBUFFER:
270 return "CMD_BINDVERTEXBUFFER";
271 case CMD_DRAW:
272 return "CMD_DRAW";
273 case CMD_DRAWINDEXED:
274 return "CMD_DRAWINDEXED";
275 case CMD_DRAWINDIRECT:
276 return "CMD_DRAWINDIRECT";
277 case CMD_DRAWINDEXEDINDIRECT:
278 return "CMD_DRAWINDEXEDINDIRECT";
279 case CMD_DISPATCH:
280 return "CMD_DISPATCH";
281 case CMD_DISPATCHINDIRECT:
282 return "CMD_DISPATCHINDIRECT";
283 case CMD_COPYBUFFER:
284 return "CMD_COPYBUFFER";
285 case CMD_COPYIMAGE:
286 return "CMD_COPYIMAGE";
Tobin Ehlis793ad302015-04-03 12:01:11 -0600287 case CMD_BLITIMAGE:
288 return "CMD_BLITIMAGE";
Tobin Ehlis10ae8c12015-03-17 16:24:32 -0600289 case CMD_COPYBUFFERTOIMAGE:
290 return "CMD_COPYBUFFERTOIMAGE";
291 case CMD_COPYIMAGETOBUFFER:
292 return "CMD_COPYIMAGETOBUFFER";
293 case CMD_CLONEIMAGEDATA:
294 return "CMD_CLONEIMAGEDATA";
295 case CMD_UPDATEBUFFER:
296 return "CMD_UPDATEBUFFER";
297 case CMD_FILLBUFFER:
298 return "CMD_FILLBUFFER";
299 case CMD_CLEARCOLORIMAGE:
300 return "CMD_CLEARCOLORIMAGE";
Courtney Goeltzenleuchterc9323e02015-10-15 16:51:05 -0600301 case CMD_CLEARATTACHMENTS:
Tobin Ehlis53eddda2015-07-01 16:46:13 -0600302 return "CMD_CLEARCOLORATTACHMENT";
303 case CMD_CLEARDEPTHSTENCILIMAGE:
304 return "CMD_CLEARDEPTHSTENCILIMAGE";
Tobin Ehlis10ae8c12015-03-17 16:24:32 -0600305 case CMD_RESOLVEIMAGE:
306 return "CMD_RESOLVEIMAGE";
307 case CMD_SETEVENT:
308 return "CMD_SETEVENT";
309 case CMD_RESETEVENT:
310 return "CMD_RESETEVENT";
311 case CMD_WAITEVENTS:
312 return "CMD_WAITEVENTS";
313 case CMD_PIPELINEBARRIER:
314 return "CMD_PIPELINEBARRIER";
315 case CMD_BEGINQUERY:
316 return "CMD_BEGINQUERY";
317 case CMD_ENDQUERY:
318 return "CMD_ENDQUERY";
319 case CMD_RESETQUERYPOOL:
320 return "CMD_RESETQUERYPOOL";
Mark Lobodzinski5495d132015-09-30 16:19:16 -0600321 case CMD_COPYQUERYPOOLRESULTS:
322 return "CMD_COPYQUERYPOOLRESULTS";
Tobin Ehlis10ae8c12015-03-17 16:24:32 -0600323 case CMD_WRITETIMESTAMP:
324 return "CMD_WRITETIMESTAMP";
325 case CMD_INITATOMICCOUNTERS:
326 return "CMD_INITATOMICCOUNTERS";
327 case CMD_LOADATOMICCOUNTERS:
328 return "CMD_LOADATOMICCOUNTERS";
329 case CMD_SAVEATOMICCOUNTERS:
330 return "CMD_SAVEATOMICCOUNTERS";
331 case CMD_BEGINRENDERPASS:
332 return "CMD_BEGINRENDERPASS";
333 case CMD_ENDRENDERPASS:
334 return "CMD_ENDRENDERPASS";
335 case CMD_DBGMARKERBEGIN:
336 return "CMD_DBGMARKERBEGIN";
337 case CMD_DBGMARKEREND:
338 return "CMD_DBGMARKEREND";
339 default:
340 return "UNKNOWN";
341 }
342}
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700343
344// SPIRV utility functions
345static void
Chris Forbes21977d92016-01-26 13:41:39 +1300346build_def_index(shader_module *module)
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700347{
Chris Forbesc7e2e202016-01-18 08:56:40 +1300348 for (auto insn : *module) {
349 switch (insn.opcode()) {
Chris Forbes92b9ab02016-01-26 13:49:27 +1300350 /* Types */
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700351 case spv::OpTypeVoid:
352 case spv::OpTypeBool:
353 case spv::OpTypeInt:
354 case spv::OpTypeFloat:
355 case spv::OpTypeVector:
356 case spv::OpTypeMatrix:
357 case spv::OpTypeImage:
358 case spv::OpTypeSampler:
359 case spv::OpTypeSampledImage:
360 case spv::OpTypeArray:
361 case spv::OpTypeRuntimeArray:
362 case spv::OpTypeStruct:
363 case spv::OpTypeOpaque:
364 case spv::OpTypePointer:
365 case spv::OpTypeFunction:
366 case spv::OpTypeEvent:
367 case spv::OpTypeDeviceEvent:
368 case spv::OpTypeReserveId:
369 case spv::OpTypeQueue:
370 case spv::OpTypePipe:
Chris Forbes21977d92016-01-26 13:41:39 +1300371 module->def_index[insn.word(1)] = insn.offset();
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700372 break;
373
Chris Forbes92b9ab02016-01-26 13:49:27 +1300374 /* Fixed constants */
375 case spv::OpConstantTrue:
376 case spv::OpConstantFalse:
377 case spv::OpConstant:
378 case spv::OpConstantComposite:
379 case spv::OpConstantSampler:
380 case spv::OpConstantNull:
381 module->def_index[insn.word(2)] = insn.offset();
382 break;
383
384 /* Specialization constants */
385 case spv::OpSpecConstantTrue:
386 case spv::OpSpecConstantFalse:
387 case spv::OpSpecConstant:
388 case spv::OpSpecConstantComposite:
389 case spv::OpSpecConstantOp:
390 module->def_index[insn.word(2)] = insn.offset();
391 break;
392
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700393 default:
Chris Forbes92b9ab02016-01-26 13:49:27 +1300394 /* We don't care about any other defs for now. */
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700395 break;
396 }
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700397 }
398}
399
400bool
401shader_is_spirv(VkShaderModuleCreateInfo const *pCreateInfo)
402{
403 uint32_t *words = (uint32_t *)pCreateInfo->pCode;
404 size_t sizeInWords = pCreateInfo->codeSize / sizeof(uint32_t);
405
406 /* Just validate that the header makes sense. */
407 return sizeInWords >= 5 && words[0] == spv::MagicNumber && words[1] == spv::Version;
408}
409
410static char const *
411storage_class_name(unsigned sc)
412{
413 switch (sc) {
414 case spv::StorageClassInput: return "input";
415 case spv::StorageClassOutput: return "output";
416 case spv::StorageClassUniformConstant: return "const uniform";
417 case spv::StorageClassUniform: return "uniform";
418 case spv::StorageClassWorkgroup: return "workgroup local";
419 case spv::StorageClassCrossWorkgroup: return "workgroup global";
420 case spv::StorageClassPrivate: return "private global";
421 case spv::StorageClassFunction: return "function";
422 case spv::StorageClassGeneric: return "generic";
423 case spv::StorageClassAtomicCounter: return "atomic counter";
424 case spv::StorageClassImage: return "image";
425 default: return "unknown";
426 }
427}
428
Chris Forbes204207a2016-01-26 14:07:16 +1300429/* get the value of an integral constant */
430unsigned
431get_constant_value(shader_module const *src, unsigned id)
432{
433 auto value = src->get_def(id);
434 assert(value != src->end());
435
436 if (value.opcode() != spv::OpConstant) {
437 /* TODO: Either ensure that the specialization transform is already performed on a module we're
438 considering here, OR -- specialize on the fly now.
439 */
440 return 1;
441 }
442
443 return value.word(3);
444}
445
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700446/* returns ptr to null terminator */
447static char *
448describe_type(char *dst, shader_module const *src, unsigned type)
449{
Chris Forbes21977d92016-01-26 13:41:39 +1300450 auto insn = src->get_def(type);
Chris Forbes1257f912016-01-18 12:07:01 +1300451 assert(insn != src->end());
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700452
Chris Forbes1257f912016-01-18 12:07:01 +1300453 switch (insn.opcode()) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700454 case spv::OpTypeBool:
455 return dst + sprintf(dst, "bool");
456 case spv::OpTypeInt:
Chris Forbes1257f912016-01-18 12:07:01 +1300457 return dst + sprintf(dst, "%cint%d", insn.word(3) ? 's' : 'u', insn.word(2));
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700458 case spv::OpTypeFloat:
Chris Forbes1257f912016-01-18 12:07:01 +1300459 return dst + sprintf(dst, "float%d", insn.word(2));
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700460 case spv::OpTypeVector:
Chris Forbes1257f912016-01-18 12:07:01 +1300461 dst += sprintf(dst, "vec%d of ", insn.word(3));
462 return describe_type(dst, src, insn.word(2));
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700463 case spv::OpTypeMatrix:
Chris Forbes1257f912016-01-18 12:07:01 +1300464 dst += sprintf(dst, "mat%d of ", insn.word(3));
465 return describe_type(dst, src, insn.word(2));
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700466 case spv::OpTypeArray:
Chris Forbes95c98052016-01-26 14:08:53 +1300467 dst += sprintf(dst, "arr[%d] of ", get_constant_value(src, insn.word(3)));
Chris Forbes1257f912016-01-18 12:07:01 +1300468 return describe_type(dst, src, insn.word(2));
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700469 case spv::OpTypePointer:
Chris Forbes1257f912016-01-18 12:07:01 +1300470 dst += sprintf(dst, "ptr to %s ", storage_class_name(insn.word(2)));
471 return describe_type(dst, src, insn.word(3));
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700472 case spv::OpTypeStruct:
473 {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700474 dst += sprintf(dst, "struct of (");
Chris Forbes1257f912016-01-18 12:07:01 +1300475 for (unsigned i = 2; i < insn.len(); i++) {
476 dst = describe_type(dst, src, insn.word(i));
477 dst += sprintf(dst, i == insn.len()-1 ? ")" : ", ");
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700478 }
479 return dst;
480 }
481 case spv::OpTypeSampler:
482 return dst + sprintf(dst, "sampler");
483 default:
484 return dst + sprintf(dst, "oddtype");
485 }
486}
487
488static bool
489types_match(shader_module const *a, shader_module const *b, unsigned a_type, unsigned b_type, bool b_arrayed)
490{
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700491 /* walk two type trees together, and complain about differences */
Chris Forbes21977d92016-01-26 13:41:39 +1300492 auto a_insn = a->get_def(a_type);
493 auto b_insn = b->get_def(b_type);
Chris Forbes1257f912016-01-18 12:07:01 +1300494 assert(a_insn != a->end());
495 assert(b_insn != b->end());
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700496
Chris Forbes1257f912016-01-18 12:07:01 +1300497 if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700498 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
Chris Forbes1257f912016-01-18 12:07:01 +1300499 return types_match(a, b, a_type, b_insn.word(2), false);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700500 }
501
Chris Forbes1257f912016-01-18 12:07:01 +1300502 if (a_insn.opcode() != b_insn.opcode()) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700503 return false;
504 }
505
Chris Forbes1257f912016-01-18 12:07:01 +1300506 switch (a_insn.opcode()) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700507 /* if b_arrayed and we hit a leaf type, then we can't match -- there's nowhere for the extra OpTypeArray to be! */
508 case spv::OpTypeBool:
509 return true && !b_arrayed;
510 case spv::OpTypeInt:
511 /* match on width, signedness */
Chris Forbes1257f912016-01-18 12:07:01 +1300512 return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3) && !b_arrayed;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700513 case spv::OpTypeFloat:
514 /* match on width */
Chris Forbes1257f912016-01-18 12:07:01 +1300515 return a_insn.word(2) == b_insn.word(2) && !b_arrayed;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700516 case spv::OpTypeVector:
517 case spv::OpTypeMatrix:
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700518 /* match on element type, count. these all have the same layout. we don't get here if
519 * b_arrayed -- that is handled above. */
Chris Forbes95c98052016-01-26 14:08:53 +1300520 return !b_arrayed &&
521 types_match(a, b, a_insn.word(2), b_insn.word(2), b_arrayed) &&
522 a_insn.word(3) == b_insn.word(3);
523 case spv::OpTypeArray:
524 /* match on element type, count. these all have the same layout. we don't get here if
525 * b_arrayed. This differs from vector & matrix types in that the array size is the id of a constant instruction,
526 * not a literal within OpTypeArray */
527 return !b_arrayed &&
528 types_match(a, b, a_insn.word(2), b_insn.word(2), b_arrayed) &&
529 get_constant_value(a, a_insn.word(3)) == get_constant_value(b, b_insn.word(3));
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700530 case spv::OpTypeStruct:
531 /* match on all element types */
532 {
533 if (b_arrayed) {
534 /* for the purposes of matching different levels of arrayness, structs are leaves. */
535 return false;
536 }
537
Chris Forbes1257f912016-01-18 12:07:01 +1300538 if (a_insn.len() != b_insn.len()) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700539 return false; /* structs cannot match if member counts differ */
540 }
541
Chris Forbes1257f912016-01-18 12:07:01 +1300542 for (unsigned i = 2; i < a_insn.len(); i++) {
543 if (!types_match(a, b, a_insn.word(i), b_insn.word(i), b_arrayed)) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700544 return false;
545 }
546 }
547
548 return true;
549 }
550 case spv::OpTypePointer:
551 /* match on pointee type. storage class is expected to differ */
Chris Forbes1257f912016-01-18 12:07:01 +1300552 return types_match(a, b, a_insn.word(3), b_insn.word(3), b_arrayed);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700553
554 default:
555 /* remaining types are CLisms, or may not appear in the interfaces we
556 * are interested in. Just claim no match.
557 */
558 return false;
559
560 }
561}
562
563static int
564value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
565{
566 auto it = map.find(id);
567 if (it == map.end())
568 return def;
569 else
570 return it->second;
571}
572
573
574static unsigned
575get_locations_consumed_by_type(shader_module const *src, unsigned type, bool strip_array_level)
576{
Chris Forbes21977d92016-01-26 13:41:39 +1300577 auto insn = src->get_def(type);
Chris Forbes1257f912016-01-18 12:07:01 +1300578 assert(insn != src->end());
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700579
Chris Forbesc7e2e202016-01-18 08:56:40 +1300580 switch (insn.opcode()) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700581 case spv::OpTypePointer:
582 /* see through the ptr -- this is only ever at the toplevel for graphics shaders;
583 * we're never actually passing pointers around. */
Chris Forbesc7e2e202016-01-18 08:56:40 +1300584 return get_locations_consumed_by_type(src, insn.word(3), strip_array_level);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700585 case spv::OpTypeArray:
586 if (strip_array_level) {
Chris Forbesc7e2e202016-01-18 08:56:40 +1300587 return get_locations_consumed_by_type(src, insn.word(2), false);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700588 }
589 else {
Chris Forbes95c98052016-01-26 14:08:53 +1300590 return get_constant_value(src, insn.word(3)) * get_locations_consumed_by_type(src, insn.word(2), false);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700591 }
592 case spv::OpTypeMatrix:
593 /* num locations is the dimension * element size */
Chris Forbesc7e2e202016-01-18 08:56:40 +1300594 return insn.word(3) * get_locations_consumed_by_type(src, insn.word(2), false);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700595 default:
596 /* everything else is just 1. */
597 return 1;
598
599 /* TODO: extend to handle 64bit scalar types, whose vectors may need
600 * multiple locations. */
601 }
602}
603
604
605struct interface_var {
606 uint32_t id;
607 uint32_t type_id;
608 uint32_t offset;
609 /* TODO: collect the name, too? Isn't required to be present. */
610};
611
Chris Forbesa3e85f62016-01-15 14:53:11 +1300612
613static void
614collect_interface_block_members(layer_data *my_data, VkDevice dev,
615 shader_module const *src,
616 std::map<uint32_t, interface_var> &out,
617 std::map<uint32_t, interface_var> &builtins_out,
618 std::unordered_map<unsigned, unsigned> const &blocks,
619 bool is_array_of_verts,
620 uint32_t id,
621 uint32_t type_id)
622{
623 /* Walk down the type_id presented, trying to determine whether it's actually an interface block. */
Chris Forbes21977d92016-01-26 13:41:39 +1300624 auto type = src->get_def(type_id);
Chris Forbes1257f912016-01-18 12:07:01 +1300625
Chris Forbesa3e85f62016-01-15 14:53:11 +1300626 while (true) {
627
Chris Forbes1257f912016-01-18 12:07:01 +1300628 if (type.opcode() == spv::OpTypePointer) {
Chris Forbes21977d92016-01-26 13:41:39 +1300629 type = src->get_def(type.word(3));
Chris Forbesa3e85f62016-01-15 14:53:11 +1300630 }
Chris Forbes1257f912016-01-18 12:07:01 +1300631 else if (type.opcode() == spv::OpTypeArray && is_array_of_verts) {
Chris Forbes21977d92016-01-26 13:41:39 +1300632 type = src->get_def(type.word(2));
Chris Forbesa3e85f62016-01-15 14:53:11 +1300633 is_array_of_verts = false;
634 }
Chris Forbes1257f912016-01-18 12:07:01 +1300635 else if (type.opcode() == spv::OpTypeStruct) {
636 if (blocks.find(type.word(1)) == blocks.end()) {
Chris Forbesa3e85f62016-01-15 14:53:11 +1300637 /* This isn't an interface block. */
638 return;
639 }
640 else {
641 /* We have found the correct type. Walk its members. */
642 break;
643 }
644 }
645 else {
646 /* not an interface block */
647 return;
648 }
649 }
650
Chris Forbes1257f912016-01-18 12:07:01 +1300651 /* Walk all the OpMemberDecorate for type's result id. */
Chris Forbesc7e2e202016-01-18 08:56:40 +1300652 for (auto insn : *src) {
Chris Forbes1257f912016-01-18 12:07:01 +1300653 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
Chris Forbesc7e2e202016-01-18 08:56:40 +1300654 unsigned member_index = insn.word(2);
Chris Forbes1257f912016-01-18 12:07:01 +1300655 unsigned member_type_id = type.word(2 + member_index);
Chris Forbesa3e85f62016-01-15 14:53:11 +1300656
Chris Forbesc7e2e202016-01-18 08:56:40 +1300657 if (insn.word(3) == spv::DecorationLocation) {
658 unsigned location = insn.word(4);
Chris Forbesa3e85f62016-01-15 14:53:11 +1300659 unsigned num_locations = get_locations_consumed_by_type(src, member_type_id, false);
660 for (unsigned int offset = 0; offset < num_locations; offset++) {
661 interface_var v;
662 v.id = id;
663 /* TODO: member index in interface_var too? */
664 v.type_id = member_type_id;
665 v.offset = offset;
666 out[location + offset] = v;
667 }
668 }
Chris Forbesc7e2e202016-01-18 08:56:40 +1300669 else if (insn.word(3) == spv::DecorationBuiltIn) {
670 unsigned builtin = insn.word(4);
Chris Forbesa3e85f62016-01-15 14:53:11 +1300671 interface_var v;
672 v.id = id;
673 v.type_id = member_type_id;
674 v.offset = 0;
675 builtins_out[builtin] = v;
676 }
677 }
Chris Forbesa3e85f62016-01-15 14:53:11 +1300678 }
679}
680
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700681static void
682collect_interface_by_location(layer_data *my_data, VkDevice dev,
683 shader_module const *src, spv::StorageClass sinterface,
684 std::map<uint32_t, interface_var> &out,
685 std::map<uint32_t, interface_var> &builtins_out,
686 bool is_array_of_verts)
687{
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700688 std::unordered_map<unsigned, unsigned> var_locations;
689 std::unordered_map<unsigned, unsigned> var_builtins;
Chris Forbesa3e85f62016-01-15 14:53:11 +1300690 std::unordered_map<unsigned, unsigned> blocks;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700691
Chris Forbesc7e2e202016-01-18 08:56:40 +1300692 for (auto insn : *src) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700693
694 /* We consider two interface models: SSO rendezvous-by-location, and
695 * builtins. Complain about anything that fits neither model.
696 */
Chris Forbesc7e2e202016-01-18 08:56:40 +1300697 if (insn.opcode() == spv::OpDecorate) {
698 if (insn.word(2) == spv::DecorationLocation) {
699 var_locations[insn.word(1)] = insn.word(3);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700700 }
701
Chris Forbesc7e2e202016-01-18 08:56:40 +1300702 if (insn.word(2) == spv::DecorationBuiltIn) {
703 var_builtins[insn.word(1)] = insn.word(3);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700704 }
Chris Forbesa3e85f62016-01-15 14:53:11 +1300705
Chris Forbesc7e2e202016-01-18 08:56:40 +1300706 if (insn.word(2) == spv::DecorationBlock) {
707 blocks[insn.word(1)] = 1;
Chris Forbesa3e85f62016-01-15 14:53:11 +1300708 }
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700709 }
710
711 /* TODO: handle grouped decorations */
712 /* TODO: handle index=1 dual source outputs from FS -- two vars will
713 * have the same location, and we DONT want to clobber. */
714
Chris Forbesc7e2e202016-01-18 08:56:40 +1300715 else if (insn.opcode() == spv::OpVariable && insn.word(3) == sinterface) {
716 unsigned id = insn.word(2);
717 unsigned type = insn.word(1);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700718
Chris Forbesc7e2e202016-01-18 08:56:40 +1300719 int location = value_or_default(var_locations, id, -1);
720 int builtin = value_or_default(var_builtins, id, -1);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700721
Chris Forbesf5020cf2016-01-13 09:29:31 +1300722 /* All variables and interface block members in the Input or Output storage classes
723 * must be decorated with either a builtin or an explicit location.
724 *
725 * TODO: integrate the interface block support here. For now, don't complain --
726 * a valid SPIRV module will only hit this path for the interface block case, as the
727 * individual members of the type are decorated, rather than variable declarations.
728 */
729
730 if (location != -1) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700731 /* A user-defined interface variable, with a location. Where a variable
732 * occupied multiple locations, emit one result for each. */
733 unsigned num_locations = get_locations_consumed_by_type(src, type,
734 is_array_of_verts);
Courtney Goeltzenleuchter51239472015-12-16 16:06:06 -0700735 for (unsigned int offset = 0; offset < num_locations; offset++) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700736 interface_var v;
737 v.id = id;
738 v.type_id = type;
739 v.offset = offset;
740 out[location + offset] = v;
741 }
742 }
Chris Forbesf5020cf2016-01-13 09:29:31 +1300743 else if (builtin != -1) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700744 /* A builtin interface variable */
745 /* Note that since builtin interface variables do not consume numbered
746 * locations, there is no larger-than-vec4 consideration as above
747 */
748 interface_var v;
749 v.id = id;
750 v.type_id = type;
751 v.offset = 0;
752 builtins_out[builtin] = v;
753 }
Chris Forbesa3e85f62016-01-15 14:53:11 +1300754 else {
755 /* An interface block instance */
756 collect_interface_block_members(my_data, dev, src, out, builtins_out,
757 blocks, is_array_of_verts, id, type);
758 }
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700759 }
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700760 }
761}
762
763static void
764collect_interface_by_descriptor_slot(layer_data *my_data, VkDevice dev,
765 shader_module const *src, spv::StorageClass sinterface,
766 std::map<std::pair<unsigned, unsigned>, interface_var> &out)
767{
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700768
769 std::unordered_map<unsigned, unsigned> var_sets;
770 std::unordered_map<unsigned, unsigned> var_bindings;
771
Chris Forbesc7e2e202016-01-18 08:56:40 +1300772 for (auto insn : *src) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700773 /* All variables in the Uniform or UniformConstant storage classes are required to be decorated with both
774 * DecorationDescriptorSet and DecorationBinding.
775 */
Chris Forbesc7e2e202016-01-18 08:56:40 +1300776 if (insn.opcode() == spv::OpDecorate) {
777 if (insn.word(2) == spv::DecorationDescriptorSet) {
778 var_sets[insn.word(1)] = insn.word(3);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700779 }
780
Chris Forbesc7e2e202016-01-18 08:56:40 +1300781 if (insn.word(2) == spv::DecorationBinding) {
782 var_bindings[insn.word(1)] = insn.word(3);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700783 }
784 }
785
Chris Forbesc7e2e202016-01-18 08:56:40 +1300786 else if (insn.opcode() == spv::OpVariable &&
787 (insn.word(3) == spv::StorageClassUniform ||
788 insn.word(3) == spv::StorageClassUniformConstant)) {
789 unsigned set = value_or_default(var_sets, insn.word(2), 0);
790 unsigned binding = value_or_default(var_bindings, insn.word(2), 0);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700791
792 auto existing_it = out.find(std::make_pair(set, binding));
793 if (existing_it != out.end()) {
794 /* conflict within spv image */
Mark Lobodzinskib01451b2016-01-04 13:18:10 -0700795 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__,
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700796 SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
797 "var %d (type %d) in %s interface in descriptor slot (%u,%u) conflicts with existing definition",
Chris Forbesc7e2e202016-01-18 08:56:40 +1300798 insn.word(2), insn.word(1), storage_class_name(sinterface),
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700799 existing_it->first.first, existing_it->first.second);
800 }
801
802 interface_var v;
Chris Forbesc7e2e202016-01-18 08:56:40 +1300803 v.id = insn.word(2);
804 v.type_id = insn.word(1);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700805 out[std::make_pair(set, binding)] = v;
806 }
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700807 }
808}
809
810static bool
811validate_interface_between_stages(layer_data *my_data, VkDevice dev,
812 shader_module const *producer, char const *producer_name,
813 shader_module const *consumer, char const *consumer_name,
814 bool consumer_arrayed_input)
815{
816 std::map<uint32_t, interface_var> outputs;
817 std::map<uint32_t, interface_var> inputs;
818
819 std::map<uint32_t, interface_var> builtin_outputs;
820 std::map<uint32_t, interface_var> builtin_inputs;
821
822 bool pass = true;
823
824 collect_interface_by_location(my_data, dev, producer, spv::StorageClassOutput, outputs, builtin_outputs, false);
825 collect_interface_by_location(my_data, dev, consumer, spv::StorageClassInput, inputs, builtin_inputs,
826 consumer_arrayed_input);
827
828 auto a_it = outputs.begin();
829 auto b_it = inputs.begin();
830
831 /* maps sorted by key (location); walk them together to find mismatches */
832 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
833 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
834 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
835 auto a_first = a_at_end ? 0 : a_it->first;
836 auto b_first = b_at_end ? 0 : b_it->first;
837
838 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -0700839 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_PERF_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700840 "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name)) {
841 pass = false;
842 }
843 a_it++;
844 }
845 else if (a_at_end || a_first > b_first) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -0700846 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700847 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name)) {
848 pass = false;
849 }
850 b_it++;
851 }
852 else {
853 if (types_match(producer, consumer, a_it->second.type_id, b_it->second.type_id, consumer_arrayed_input)) {
854 /* OK! */
855 }
856 else {
857 char producer_type[1024];
858 char consumer_type[1024];
859 describe_type(producer_type, producer, a_it->second.type_id);
860 describe_type(consumer_type, consumer, b_it->second.type_id);
861
Mark Lobodzinskib01451b2016-01-04 13:18:10 -0700862 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700863 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type)) {
864 pass = false;
865 }
866 }
867 a_it++;
868 b_it++;
869 }
870 }
871
872 return pass;
873}
874
875enum FORMAT_TYPE {
876 FORMAT_TYPE_UNDEFINED,
877 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
878 FORMAT_TYPE_SINT,
879 FORMAT_TYPE_UINT,
880};
881
882static unsigned
883get_format_type(VkFormat fmt) {
884 switch (fmt) {
885 case VK_FORMAT_UNDEFINED:
886 return FORMAT_TYPE_UNDEFINED;
887 case VK_FORMAT_R8_SINT:
888 case VK_FORMAT_R8G8_SINT:
889 case VK_FORMAT_R8G8B8_SINT:
890 case VK_FORMAT_R8G8B8A8_SINT:
891 case VK_FORMAT_R16_SINT:
892 case VK_FORMAT_R16G16_SINT:
893 case VK_FORMAT_R16G16B16_SINT:
894 case VK_FORMAT_R16G16B16A16_SINT:
895 case VK_FORMAT_R32_SINT:
896 case VK_FORMAT_R32G32_SINT:
897 case VK_FORMAT_R32G32B32_SINT:
898 case VK_FORMAT_R32G32B32A32_SINT:
899 case VK_FORMAT_B8G8R8_SINT:
900 case VK_FORMAT_B8G8R8A8_SINT:
901 case VK_FORMAT_A2B10G10R10_SINT_PACK32:
902 case VK_FORMAT_A2R10G10B10_SINT_PACK32:
903 return FORMAT_TYPE_SINT;
904 case VK_FORMAT_R8_UINT:
905 case VK_FORMAT_R8G8_UINT:
906 case VK_FORMAT_R8G8B8_UINT:
907 case VK_FORMAT_R8G8B8A8_UINT:
908 case VK_FORMAT_R16_UINT:
909 case VK_FORMAT_R16G16_UINT:
910 case VK_FORMAT_R16G16B16_UINT:
911 case VK_FORMAT_R16G16B16A16_UINT:
912 case VK_FORMAT_R32_UINT:
913 case VK_FORMAT_R32G32_UINT:
914 case VK_FORMAT_R32G32B32_UINT:
915 case VK_FORMAT_R32G32B32A32_UINT:
916 case VK_FORMAT_B8G8R8_UINT:
917 case VK_FORMAT_B8G8R8A8_UINT:
918 case VK_FORMAT_A2B10G10R10_UINT_PACK32:
919 case VK_FORMAT_A2R10G10B10_UINT_PACK32:
920 return FORMAT_TYPE_UINT;
921 default:
922 return FORMAT_TYPE_FLOAT;
923 }
924}
925
926/* characterizes a SPIR-V type appearing in an interface to a FF stage,
927 * for comparison to a VkFormat's characterization above. */
928static unsigned
929get_fundamental_type(shader_module const *src, unsigned type)
930{
Chris Forbes21977d92016-01-26 13:41:39 +1300931 auto insn = src->get_def(type);
Chris Forbes1257f912016-01-18 12:07:01 +1300932 assert(insn != src->end());
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700933
Chris Forbes1257f912016-01-18 12:07:01 +1300934 switch (insn.opcode()) {
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700935 case spv::OpTypeInt:
Chris Forbes1257f912016-01-18 12:07:01 +1300936 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700937 case spv::OpTypeFloat:
938 return FORMAT_TYPE_FLOAT;
939 case spv::OpTypeVector:
Chris Forbes1257f912016-01-18 12:07:01 +1300940 return get_fundamental_type(src, insn.word(2));
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700941 case spv::OpTypeMatrix:
Chris Forbes1257f912016-01-18 12:07:01 +1300942 return get_fundamental_type(src, insn.word(2));
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700943 case spv::OpTypeArray:
Chris Forbes1257f912016-01-18 12:07:01 +1300944 return get_fundamental_type(src, insn.word(2));
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700945 case spv::OpTypePointer:
Chris Forbes1257f912016-01-18 12:07:01 +1300946 return get_fundamental_type(src, insn.word(3));
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700947 default:
948 return FORMAT_TYPE_UNDEFINED;
949 }
950}
951
952static bool
953validate_vi_consistency(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
954{
955 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
956 * each binding should be specified only once.
957 */
958 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
959 bool pass = true;
960
961 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
962 auto desc = &vi->pVertexBindingDescriptions[i];
963 auto & binding = bindings[desc->binding];
964 if (binding) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -0700965 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__, SHADER_CHECKER_INCONSISTENT_VI, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -0700966 "Duplicate vertex input binding descriptions for binding %d", desc->binding)) {
967 pass = false;
968 }
969 }
970 else {
971 binding = desc;
972 }
973 }
974
975 return pass;
976}
977
978static bool
979validate_vi_against_vs_inputs(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
980{
981 std::map<uint32_t, interface_var> inputs;
982 /* we collect builtin inputs, but they will never appear in the VI state --
983 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
984 */
985 std::map<uint32_t, interface_var> builtin_inputs;
986 bool pass = true;
987
988 collect_interface_by_location(my_data, dev, vs, spv::StorageClassInput, inputs, builtin_inputs, false);
989
990 /* Build index by location */
991 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
992 if (vi) {
993 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++)
994 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
995 }
996
997 auto it_a = attribs.begin();
998 auto it_b = inputs.begin();
999
1000 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
1001 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
1002 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
1003 auto a_first = a_at_end ? 0 : it_a->first;
1004 auto b_first = b_at_end ? 0 : it_b->first;
Chris Forbes7d83cd52016-01-15 11:32:03 +13001005 if (!a_at_end && (b_at_end || a_first < b_first)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001006 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_PERF_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001007 "Vertex attribute at location %d not consumed by VS", a_first)) {
1008 pass = false;
1009 }
1010 it_a++;
1011 }
Chris Forbes7d83cd52016-01-15 11:32:03 +13001012 else if (!b_at_end && (a_at_end || b_first < a_first)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001013 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001014 "VS consumes input at location %d but not provided", b_first)) {
1015 pass = false;
1016 }
1017 it_b++;
1018 }
1019 else {
1020 unsigned attrib_type = get_format_type(it_a->second->format);
1021 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
1022
1023 /* type checking */
1024 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
1025 char vs_type[1024];
1026 describe_type(vs_type, vs, it_b->second.type_id);
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001027 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001028 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
1029 string_VkFormat(it_a->second->format), a_first, vs_type)) {
1030 pass = false;
1031 }
1032 }
1033
1034 /* OK! */
1035 it_a++;
1036 it_b++;
1037 }
1038 }
1039
1040 return pass;
1041}
1042
1043static bool
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001044validate_fs_outputs_against_render_pass(layer_data *my_data, VkDevice dev, shader_module const *fs, RENDER_PASS_NODE const *rp, uint32_t subpass)
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001045{
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001046 const std::vector<VkFormat> &color_formats = rp->subpassColorFormats[subpass];
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001047 std::map<uint32_t, interface_var> outputs;
1048 std::map<uint32_t, interface_var> builtin_outputs;
1049 bool pass = true;
1050
1051 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
1052
1053 collect_interface_by_location(my_data, dev, fs, spv::StorageClassOutput, outputs, builtin_outputs, false);
1054
1055 auto it = outputs.begin();
1056 uint32_t attachment = 0;
1057
1058 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
1059 * are currently dense, but the parallel with matching between shader stages is nice.
1060 */
1061
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001062 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
1063 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001064 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001065 "FS writes to output location %d with no matching attachment", it->first)) {
1066 pass = false;
1067 }
1068 it++;
1069 }
1070 else if (it == outputs.end() || it->first > attachment) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001071 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001072 "Attachment %d not written by FS", attachment)) {
1073 pass = false;
1074 }
1075 attachment++;
1076 }
1077 else {
1078 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
1079 unsigned att_type = get_format_type(color_formats[attachment]);
1080
1081 /* type checking */
1082 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
1083 char fs_type[1024];
1084 describe_type(fs_type, fs, it->second.type_id);
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001085 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001086 "Attachment %d of type `%s` does not match FS output type of `%s`",
1087 attachment, string_VkFormat(color_formats[attachment]), fs_type)) {
1088 pass = false;
1089 }
1090 }
1091
1092 /* OK! */
1093 it++;
1094 attachment++;
1095 }
1096 }
1097
1098 return pass;
1099}
1100
1101
1102struct shader_stage_attributes {
1103 char const * const name;
1104 bool arrayed_input;
1105};
1106
1107
1108static shader_stage_attributes
1109shader_stage_attribs[] = {
1110 { "vertex shader", false },
1111 { "tessellation control shader", true },
1112 { "tessellation evaluation shader", false },
1113 { "geometry shader", true },
1114 { "fragment shader", false },
1115};
1116
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001117// For given pipelineLayout verify that the setLayout at slot.first
1118// has the requested binding at slot.second
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001119static bool
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001120has_descriptor_binding(layer_data* my_data,
1121 vector<VkDescriptorSetLayout>* pipelineLayout,
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001122 std::pair<unsigned, unsigned> slot)
1123{
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001124 if (!pipelineLayout)
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001125 return false;
1126
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001127 if (slot.first >= pipelineLayout->size())
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001128 return false;
1129
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001130 auto set = my_data->descriptorSetLayoutMap[(*pipelineLayout)[slot.first]]->bindings;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001131
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001132 return (set.find(slot.second) != set.end());
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001133}
1134
1135static uint32_t get_shader_stage_id(VkShaderStageFlagBits stage)
1136{
1137 uint32_t bit_pos = u_ffs(stage);
1138 return bit_pos-1;
1139}
1140
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001141// Block of code at start here for managing/tracking Pipeline state that this layer cares about
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001142
1143static uint64_t g_drawCount[NUM_DRAW_TYPES] = {0, 0, 0, 0};
1144
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001145// TODO : Should be tracking lastBound per commandBuffer and when draws occur, report based on that cmd buffer lastBound
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001146// Then need to synchronize the accesses based on cmd buffer so that if I'm reading state on one cmd buffer, updates
1147// to that same cmd buffer by separate thread are not changing state from underneath us
1148// Track the last cmd buffer touched by this thread
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001149
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06001150// prototype
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001151static GLOBAL_CB_NODE* getCBNode(layer_data*, const VkCommandBuffer);
Tobin Ehlis559c6382015-11-05 09:52:49 -07001152
Courtney Goeltzenleuchtercd2a0992015-07-09 11:44:38 -06001153static VkBool32 hasDrawCmd(GLOBAL_CB_NODE* pCB)
Tobin Ehlis53eddda2015-07-01 16:46:13 -06001154{
1155 for (uint32_t i=0; i<NUM_DRAW_TYPES; i++) {
1156 if (pCB->drawCount[i])
1157 return VK_TRUE;
1158 }
1159 return VK_FALSE;
1160}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001161
Tobin Ehlise382c5a2015-06-10 12:57:07 -06001162// Check object status for selected flag state
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06001163static VkBool32 validate_status(layer_data* my_data, GLOBAL_CB_NODE* pNode, CBStatusFlags enable_mask, CBStatusFlags status_mask, CBStatusFlags status_flag, VkFlags msg_flags, DRAW_STATE_ERROR error_code, const char* fail_msg)
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001164{
Tobin Ehlis429b91d2015-06-22 17:20:50 -06001165 // If non-zero enable mask is present, check it against status but if enable_mask
1166 // is 0 then no enable required so we should always just check status
1167 if ((!enable_mask) || (enable_mask & pNode->status)) {
1168 if ((pNode->status & status_mask) != status_flag) {
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06001169 // TODO : How to pass dispatchable objects as srcObject? Here src obj should be cmd buffer
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001170 return log_msg(my_data->report_data, msg_flags, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, error_code, "DS",
Mark Youngee3f3a22016-01-25 12:18:32 -07001171 "CB object %#" PRIxLEAST64 ": %s", (uint64_t)(pNode->commandBuffer), fail_msg);
Tobin Ehlise382c5a2015-06-10 12:57:07 -06001172 }
Tobin Ehlise382c5a2015-06-10 12:57:07 -06001173 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001174 return VK_FALSE;
Tobin Ehlise382c5a2015-06-10 12:57:07 -06001175}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001176
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001177// Retrieve pipeline node ptr for given pipeline object
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06001178static PIPELINE_NODE* getPipeline(layer_data* my_data, const VkPipeline pipeline)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001179{
1180 loader_platform_thread_lock_mutex(&globalLock);
Chia-I Wue2fc5522015-10-26 20:04:44 +08001181 if (my_data->pipelineMap.find(pipeline) == my_data->pipelineMap.end()) {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001182 loader_platform_thread_unlock_mutex(&globalLock);
1183 return NULL;
1184 }
1185 loader_platform_thread_unlock_mutex(&globalLock);
Chia-I Wue2fc5522015-10-26 20:04:44 +08001186 return my_data->pipelineMap[pipeline];
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001187}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001188
Tobin Ehlisd332f282015-10-02 11:00:56 -06001189// Return VK_TRUE if for a given PSO, the given state enum is dynamic, else return VK_FALSE
1190static VkBool32 isDynamic(const PIPELINE_NODE* pPipeline, const VkDynamicState state)
1191{
1192 if (pPipeline && pPipeline->graphicsPipelineCI.pDynamicState) {
1193 for (uint32_t i=0; i<pPipeline->graphicsPipelineCI.pDynamicState->dynamicStateCount; i++) {
1194 if (state == pPipeline->graphicsPipelineCI.pDynamicState->pDynamicStates[i])
1195 return VK_TRUE;
1196 }
1197 }
1198 return VK_FALSE;
1199}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001200
Tobin Ehlis429b91d2015-06-22 17:20:50 -06001201// Validate state stored as flags at time of draw call
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06001202static VkBool32 validate_draw_state_flags(layer_data* my_data, GLOBAL_CB_NODE* pCB, VkBool32 indexedDraw) {
Courtney Goeltzenleuchtercd2a0992015-07-09 11:44:38 -06001203 VkBool32 result;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001204 result = validate_status(my_data, pCB, CBSTATUS_NONE, CBSTATUS_VIEWPORT_SET, CBSTATUS_VIEWPORT_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_VIEWPORT_NOT_BOUND, "Dynamic viewport state not set for this command buffer");
1205 result |= validate_status(my_data, pCB, CBSTATUS_NONE, CBSTATUS_SCISSOR_SET, CBSTATUS_SCISSOR_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_SCISSOR_NOT_BOUND, "Dynamic scissor state not set for this command buffer");
1206 result |= validate_status(my_data, pCB, CBSTATUS_NONE, CBSTATUS_LINE_WIDTH_SET, CBSTATUS_LINE_WIDTH_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_LINE_WIDTH_NOT_BOUND, "Dynamic line width state not set for this command buffer");
1207 result |= validate_status(my_data, pCB, CBSTATUS_NONE, CBSTATUS_DEPTH_BIAS_SET, CBSTATUS_DEPTH_BIAS_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_DEPTH_BIAS_NOT_BOUND, "Dynamic depth bias state not set for this command buffer");
1208 result |= validate_status(my_data, pCB, CBSTATUS_COLOR_BLEND_WRITE_ENABLE, CBSTATUS_BLEND_SET, CBSTATUS_BLEND_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_BLEND_NOT_BOUND, "Dynamic blend object state not set for this command buffer");
1209 result |= validate_status(my_data, pCB, CBSTATUS_DEPTH_WRITE_ENABLE, CBSTATUS_DEPTH_BOUNDS_SET, CBSTATUS_DEPTH_BOUNDS_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_DEPTH_BOUNDS_NOT_BOUND, "Dynamic depth bounds state not set for this command buffer");
1210 result |= validate_status(my_data, pCB, CBSTATUS_STENCIL_TEST_ENABLE, CBSTATUS_STENCIL_READ_MASK_SET, CBSTATUS_STENCIL_READ_MASK_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_STENCIL_NOT_BOUND, "Dynamic stencil read mask state not set for this command buffer");
1211 result |= validate_status(my_data, pCB, CBSTATUS_STENCIL_TEST_ENABLE, CBSTATUS_STENCIL_WRITE_MASK_SET, CBSTATUS_STENCIL_WRITE_MASK_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_STENCIL_NOT_BOUND, "Dynamic stencil write mask state not set for this command buffer");
1212 result |= validate_status(my_data, pCB, CBSTATUS_STENCIL_TEST_ENABLE, CBSTATUS_STENCIL_REFERENCE_SET, CBSTATUS_STENCIL_REFERENCE_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_STENCIL_NOT_BOUND, "Dynamic stencil reference state not set for this command buffer");
Tobin Ehlis429b91d2015-06-22 17:20:50 -06001213 if (indexedDraw)
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001214 result |= validate_status(my_data, pCB, CBSTATUS_NONE, CBSTATUS_INDEX_BUFFER_BOUND, CBSTATUS_INDEX_BUFFER_BOUND, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_INDEX_BUFFER_NOT_BOUND, "Index buffer object not bound to this command buffer when Indexed Draw attempted");
Tobin Ehlis429b91d2015-06-22 17:20:50 -06001215 return result;
1216}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001217
Tobin Ehlis651d9b02015-12-16 05:01:22 -07001218// Verify attachment reference compatibility according to spec
1219// If one array is larger, treat missing elements of shorter array as VK_ATTACHMENT_UNUSED & other array much match this
1220// If both AttachmentReference arrays have requested index, check their corresponding AttachementDescriptions
1221// to make sure that format and samples counts match.
1222// If not, they are not compatible.
1223static bool attachment_references_compatible(const uint32_t index, const VkAttachmentReference* pPrimary, const uint32_t primaryCount, const VkAttachmentDescription* pPrimaryAttachments,
1224 const VkAttachmentReference* pSecondary, const uint32_t secondaryCount, const VkAttachmentDescription* pSecondaryAttachments)
1225{
1226 if (index >= primaryCount) { // Check secondary as if primary is VK_ATTACHMENT_UNUSED
1227 if (VK_ATTACHMENT_UNUSED != pSecondary[index].attachment)
1228 return false;
1229 } else if (index >= secondaryCount) { // Check primary as if secondary is VK_ATTACHMENT_UNUSED
1230 if (VK_ATTACHMENT_UNUSED != pPrimary[index].attachment)
1231 return false;
1232 } else { // format and sample count must match
1233 if ((pPrimaryAttachments[pPrimary[index].attachment].format == pSecondaryAttachments[pSecondary[index].attachment].format) &&
1234 (pPrimaryAttachments[pPrimary[index].attachment].samples == pSecondaryAttachments[pSecondary[index].attachment].samples))
1235 return true;
1236 }
1237 // Format and sample counts didn't match
1238 return false;
1239}
1240
1241// For give primary and secondary RenderPass objects, verify that they're compatible
1242static bool verify_renderpass_compatibility(layer_data* my_data, const VkRenderPass primaryRP, const VkRenderPass secondaryRP, string& errorMsg)
1243{
1244 stringstream errorStr;
1245 if (my_data->renderPassMap.find(primaryRP) == my_data->renderPassMap.end()) {
1246 errorStr << "invalid VkRenderPass (" << primaryRP << ")";
1247 errorMsg = errorStr.str();
1248 return false;
1249 } else if (my_data->renderPassMap.find(secondaryRP) == my_data->renderPassMap.end()) {
1250 errorStr << "invalid VkRenderPass (" << secondaryRP << ")";
1251 errorMsg = errorStr.str();
1252 return false;
1253 }
1254 // Trivial pass case is exact same RP
1255 if (primaryRP == secondaryRP)
1256 return true;
1257 const VkRenderPassCreateInfo* primaryRPCI = my_data->renderPassMap[primaryRP]->pCreateInfo;
1258 const VkRenderPassCreateInfo* secondaryRPCI = my_data->renderPassMap[secondaryRP]->pCreateInfo;
1259 if (primaryRPCI->subpassCount != secondaryRPCI->subpassCount) {
1260 errorStr << "RenderPass for primary cmdBuffer has " << primaryRPCI->subpassCount << " subpasses but renderPass for secondary cmdBuffer has " << secondaryRPCI->subpassCount << " subpasses.";
1261 errorMsg = errorStr.str();
1262 return false;
1263 }
1264 uint32_t spIndex = 0;
1265 for (spIndex = 0; spIndex < primaryRPCI->subpassCount; ++spIndex) {
1266 // For each subpass, verify that corresponding color, input, resolve & depth/stencil attachment references are compatible
1267 uint32_t primaryColorCount = primaryRPCI->pSubpasses[spIndex].colorAttachmentCount;
1268 uint32_t secondaryColorCount = secondaryRPCI->pSubpasses[spIndex].colorAttachmentCount;
1269 uint32_t colorMax = std::max(primaryColorCount, secondaryColorCount);
1270 for (uint32_t cIdx = 0; cIdx < colorMax; ++cIdx) {
1271 if (!attachment_references_compatible(cIdx, primaryRPCI->pSubpasses[spIndex].pColorAttachments, primaryColorCount, primaryRPCI->pAttachments,
1272 secondaryRPCI->pSubpasses[spIndex].pColorAttachments, secondaryColorCount, secondaryRPCI->pAttachments)) {
1273 errorStr << "color attachments at index " << cIdx << " of subpass index " << spIndex << " are not compatible.";
1274 errorMsg = errorStr.str();
1275 return false;
1276 } else if (!attachment_references_compatible(cIdx, primaryRPCI->pSubpasses[spIndex].pResolveAttachments, primaryColorCount, primaryRPCI->pAttachments,
1277 secondaryRPCI->pSubpasses[spIndex].pResolveAttachments, secondaryColorCount, secondaryRPCI->pAttachments)) {
1278 errorStr << "resolve attachments at index " << cIdx << " of subpass index " << spIndex << " are not compatible.";
1279 errorMsg = errorStr.str();
1280 return false;
1281 } else if (!attachment_references_compatible(cIdx, primaryRPCI->pSubpasses[spIndex].pDepthStencilAttachment, primaryColorCount, primaryRPCI->pAttachments,
1282 secondaryRPCI->pSubpasses[spIndex].pDepthStencilAttachment, secondaryColorCount, secondaryRPCI->pAttachments)) {
1283 errorStr << "depth/stencil attachments at index " << cIdx << " of subpass index " << spIndex << " are not compatible.";
1284 errorMsg = errorStr.str();
1285 return false;
1286 }
1287 }
1288 uint32_t primaryInputCount = primaryRPCI->pSubpasses[spIndex].inputAttachmentCount;
1289 uint32_t secondaryInputCount = secondaryRPCI->pSubpasses[spIndex].inputAttachmentCount;
1290 uint32_t inputMax = std::max(primaryInputCount, secondaryInputCount);
1291 for (uint32_t i = 0; i < inputMax; ++i) {
1292 if (!attachment_references_compatible(i, primaryRPCI->pSubpasses[spIndex].pInputAttachments, primaryColorCount, primaryRPCI->pAttachments,
1293 secondaryRPCI->pSubpasses[spIndex].pInputAttachments, secondaryColorCount, secondaryRPCI->pAttachments)) {
1294 errorStr << "input attachments at index " << i << " of subpass index " << spIndex << " are not compatible.";
1295 errorMsg = errorStr.str();
1296 return false;
1297 }
1298 }
1299 }
1300 return true;
1301}
1302
Tobin Ehlis559c6382015-11-05 09:52:49 -07001303// For give SET_NODE, verify that its Set is compatible w/ the setLayout corresponding to pipelineLayout[layoutIndex]
1304static bool verify_set_layout_compatibility(layer_data* my_data, const SET_NODE* pSet, const VkPipelineLayout layout, const uint32_t layoutIndex, string& errorMsg)
1305{
Tobin Ehlis8fab6562015-12-01 09:57:09 -07001306 stringstream errorStr;
Tobin Ehlis559c6382015-11-05 09:52:49 -07001307 if (my_data->pipelineLayoutMap.find(layout) == my_data->pipelineLayoutMap.end()) {
Tobin Ehlis8fab6562015-12-01 09:57:09 -07001308 errorStr << "invalid VkPipelineLayout (" << layout << ")";
1309 errorMsg = errorStr.str();
Tobin Ehlis559c6382015-11-05 09:52:49 -07001310 return false;
1311 }
1312 PIPELINE_LAYOUT_NODE pl = my_data->pipelineLayoutMap[layout];
1313 if (layoutIndex >= pl.descriptorSetLayouts.size()) {
Tobin Ehlis8fab6562015-12-01 09:57:09 -07001314 errorStr << "VkPipelineLayout (" << layout << ") only contains " << pl.descriptorSetLayouts.size() << " setLayouts corresponding to sets 0-" << pl.descriptorSetLayouts.size()-1 << ", but you're attempting to bind set to index " << layoutIndex;
1315 errorMsg = errorStr.str();
Tobin Ehlis559c6382015-11-05 09:52:49 -07001316 return false;
1317 }
1318 // Get the specific setLayout from PipelineLayout that overlaps this set
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001319 LAYOUT_NODE* pLayoutNode = my_data->descriptorSetLayoutMap[pl.descriptorSetLayouts[layoutIndex]];
Tobin Ehlis559c6382015-11-05 09:52:49 -07001320 if (pLayoutNode->layout == pSet->pLayout->layout) { // trivial pass case
1321 return true;
1322 }
Courtney Goeltzenleuchter51239472015-12-16 16:06:06 -07001323 size_t descriptorCount = pLayoutNode->descriptorTypes.size();
Tobin Ehlis559c6382015-11-05 09:52:49 -07001324 if (descriptorCount != pSet->pLayout->descriptorTypes.size()) {
Tobin Ehlis8fab6562015-12-01 09:57:09 -07001325 errorStr << "setLayout " << layoutIndex << " from pipelineLayout " << layout << " has " << descriptorCount << " descriptors, but corresponding set being bound has " << pSet->pLayout->descriptorTypes.size() << " descriptors.";
1326 errorMsg = errorStr.str();
Tobin Ehlis559c6382015-11-05 09:52:49 -07001327 return false; // trivial fail case
1328 }
1329 // Now need to check set against corresponding pipelineLayout to verify compatibility
Courtney Goeltzenleuchter51239472015-12-16 16:06:06 -07001330 for (size_t i=0; i<descriptorCount; ++i) {
Tobin Ehlis559c6382015-11-05 09:52:49 -07001331 // Need to verify that layouts are identically defined
1332 // TODO : Is below sufficient? Making sure that types & stageFlags match per descriptor
1333 // do we also need to check immutable samplers?
1334 if (pLayoutNode->descriptorTypes[i] != pSet->pLayout->descriptorTypes[i]) {
Tobin Ehlis8fab6562015-12-01 09:57:09 -07001335 errorStr << "descriptor " << i << " for descriptorSet being bound is type '" << string_VkDescriptorType(pSet->pLayout->descriptorTypes[i]) << "' but corresponding descriptor from pipelineLayout is type '" << string_VkDescriptorType(pLayoutNode->descriptorTypes[i]) << "'";
1336 errorMsg = errorStr.str();
Tobin Ehlis559c6382015-11-05 09:52:49 -07001337 return false;
1338 }
1339 if (pLayoutNode->stageFlags[i] != pSet->pLayout->stageFlags[i]) {
Tobin Ehlis8fab6562015-12-01 09:57:09 -07001340 errorStr << "stageFlags " << i << " for descriptorSet being bound is " << pSet->pLayout->stageFlags[i] << "' but corresponding descriptor from pipelineLayout has stageFlags " << pLayoutNode->stageFlags[i];
1341 errorMsg = errorStr.str();
Tobin Ehlis559c6382015-11-05 09:52:49 -07001342 return false;
1343 }
1344 }
1345 return true;
1346}
1347
Tobin Ehlis88452832015-12-03 09:40:56 -07001348// Validate that the shaders used by the given pipeline
1349// As a side effect this function also records the sets that are actually used by the pipeline
Courtney Goeltzenleuchtere6dd8082015-12-16 16:07:01 -07001350static VkBool32
Tobin Ehlis88452832015-12-03 09:40:56 -07001351validate_pipeline_shaders(layer_data *my_data, VkDevice dev, PIPELINE_NODE* pPipeline)
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001352{
Tobin Ehlis88452832015-12-03 09:40:56 -07001353 VkGraphicsPipelineCreateInfo const *pCreateInfo = &pPipeline->graphicsPipelineCI;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001354 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
1355 * before trying to do anything more: */
1356 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1357 int geometry_stage = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
1358 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
1359
1360 shader_module **shaders = new shader_module*[fragment_stage + 1]; /* exclude CS */
1361 memset(shaders, 0, sizeof(shader_module *) * (fragment_stage +1));
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001362 RENDER_PASS_NODE const *rp = 0;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001363 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Mark Youngb20a6a82016-01-07 15:41:43 -07001364 VkBool32 pass = VK_TRUE;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001365
1366 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
1367 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
1368 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
1369
1370 if ((pStage->stage & (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT | VK_SHADER_STAGE_FRAGMENT_BIT
1371 | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)) == 0) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001372 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__, SHADER_CHECKER_UNKNOWN_STAGE, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001373 "Unknown shader stage %d", pStage->stage)) {
Mark Youngb20a6a82016-01-07 15:41:43 -07001374 pass = VK_FALSE;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001375 }
1376 }
1377 else {
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001378 shader_module *module = my_data->shaderModuleMap[pStage->module];
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001379 shaders[get_shader_stage_id(pStage->stage)] = module;
1380
1381 /* validate descriptor set layout against what the spirv module actually uses */
1382 std::map<std::pair<unsigned, unsigned>, interface_var> descriptor_uses;
1383 collect_interface_by_descriptor_slot(my_data, dev, module, spv::StorageClassUniform,
1384 descriptor_uses);
1385
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001386 auto layouts = pCreateInfo->layout != VK_NULL_HANDLE ?
1387 &(my_data->pipelineLayoutMap[pCreateInfo->layout].descriptorSetLayouts) : nullptr;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001388
1389 for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) {
Tobin Ehlis88452832015-12-03 09:40:56 -07001390 // As a side-effect of this function, capture which sets are used by the pipeline
1391 pPipeline->active_sets.insert(it->first.first);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001392
1393 /* find the matching binding */
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001394 auto found = has_descriptor_binding(my_data, layouts, it->first);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001395
1396 if (!found) {
1397 char type_name[1024];
1398 describe_type(type_name, module, it->second.type_id);
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001399 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/0, __LINE__,
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001400 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1401 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
1402 it->first.first, it->first.second, type_name)) {
Mark Youngb20a6a82016-01-07 15:41:43 -07001403 pass = VK_FALSE;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001404 }
1405 }
1406 }
1407 }
1408 }
1409 }
1410
1411 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001412 rp = my_data->renderPassMap[pCreateInfo->renderPass];
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001413
1414 vi = pCreateInfo->pVertexInputState;
1415
1416 if (vi) {
1417 pass = validate_vi_consistency(my_data, dev, vi) && pass;
1418 }
1419
1420 if (shaders[vertex_stage]) {
1421 pass = validate_vi_against_vs_inputs(my_data, dev, vi, shaders[vertex_stage]) && pass;
1422 }
1423
1424 /* TODO: enforce rules about present combinations of shaders */
1425 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1426 int consumer = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
1427
1428 while (!shaders[producer] && producer != fragment_stage) {
1429 producer++;
1430 consumer++;
1431 }
1432
1433 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
1434 assert(shaders[producer]);
1435 if (shaders[consumer]) {
1436 pass = validate_interface_between_stages(my_data, dev,
1437 shaders[producer], shader_stage_attribs[producer].name,
1438 shaders[consumer], shader_stage_attribs[consumer].name,
1439 shader_stage_attribs[consumer].arrayed_input) && pass;
1440
1441 producer = consumer;
1442 }
1443 }
1444
1445 if (shaders[fragment_stage] && rp) {
1446 pass = validate_fs_outputs_against_render_pass(my_data, dev, shaders[fragment_stage], rp, pCreateInfo->subpass) && pass;
1447 }
1448
Chris Forbes47f4f6f2015-12-17 17:10:19 +13001449 delete [] shaders;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07001450
1451 return pass;
1452}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001453
Tobin Ehlisf6585052015-12-17 11:48:42 -07001454// Return Set node ptr for specified set or else NULL
1455static SET_NODE* getSetNode(layer_data* my_data, const VkDescriptorSet set)
1456{
1457 loader_platform_thread_lock_mutex(&globalLock);
1458 if (my_data->setMap.find(set) == my_data->setMap.end()) {
1459 loader_platform_thread_unlock_mutex(&globalLock);
1460 return NULL;
1461 }
1462 loader_platform_thread_unlock_mutex(&globalLock);
1463 return my_data->setMap[set];
1464}
1465
1466// For the given set, verify that for each dynamic descriptor in that set that its
1467// dynamic offset combined with the offet and range from its descriptor update
1468// do not overflow the size of its buffer being updated
1469static VkBool32 validate_dynamic_offsets(layer_data* my_data, const SET_NODE* pSet)
1470{
1471 VkBool32 result = VK_FALSE;
1472 if (pSet->dynamicOffsets.empty())
1473 return result;
1474
1475 VkWriteDescriptorSet* pWDS = NULL;
1476 uint32_t dynOffsetIndex = 0;
1477 VkDeviceSize bufferSize = 0;
1478 for (uint32_t i=0; i < pSet->descriptorCount; ++i) {
1479 switch (pSet->ppDescriptors[i]->sType) {
1480 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
1481 pWDS = (VkWriteDescriptorSet*)pSet->ppDescriptors[i];
1482 if ((pWDS->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
1483 (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
1484 for (uint32_t j=0; j<pWDS->descriptorCount; ++j) {
Michael Lentine700b0aa2015-10-30 17:57:32 -07001485 bufferSize = my_data->bufferMap[pWDS->pBufferInfo[j].buffer].create_info->size;
Tobin Ehlisf6585052015-12-17 11:48:42 -07001486 if ((pSet->dynamicOffsets[dynOffsetIndex] + pWDS->pBufferInfo[j].offset + pWDS->pBufferInfo[j].range) > bufferSize) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001487 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pSet->set, __LINE__, DRAWSTATE_DYNAMIC_OFFSET_OVERFLOW, "DS",
Tobin Ehlisf6585052015-12-17 11:48:42 -07001488 "VkDescriptorSet (%#" PRIxLEAST64 ") bound as set #%u has dynamic offset %u. Combined with offet %#" PRIxLEAST64 " and range %#" PRIxLEAST64 " from its update, this oversteps its buffer (%#" PRIxLEAST64 ") which has a size of %#" PRIxLEAST64 ".",
1489 (uint64_t)pSet->set, i, pSet->dynamicOffsets[dynOffsetIndex], pWDS->pBufferInfo[j].offset, pWDS->pBufferInfo[j].range, (uint64_t)pWDS->pBufferInfo[j].buffer, bufferSize);
1490 }
1491 dynOffsetIndex++;
1492 i += j; // Advance i to end of this set of descriptors (++i at end of for loop will move 1 index past last of these descriptors)
1493 }
1494 }
1495 break;
1496 default: // Currently only shadowing Write update nodes so shouldn't get here
1497 assert(0);
1498 continue;
1499 }
1500 }
1501 return result;
1502}
1503
Tobin Ehlis429b91d2015-06-22 17:20:50 -06001504// Validate overall state at the time of a draw call
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06001505static VkBool32 validate_draw_state(layer_data* my_data, GLOBAL_CB_NODE* pCB, VkBool32 indexedDraw) {
Tobin Ehlis429b91d2015-06-22 17:20:50 -06001506 // First check flag states
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06001507 VkBool32 result = validate_draw_state_flags(my_data, pCB, indexedDraw);
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06001508 PIPELINE_NODE* pPipe = getPipeline(my_data, pCB->lastBoundPipeline);
Tobin Ehlis429b91d2015-06-22 17:20:50 -06001509 // Now complete other state checks
Tobin Ehlise90e66d2015-09-09 13:31:01 -06001510 // TODO : Currently only performing next check if *something* was bound (non-zero last bound)
1511 // There is probably a better way to gate when this check happens, and to know if something *should* have been bound
1512 // We should have that check separately and then gate this check based on that check
Mark Lobodzinski74635932015-12-18 15:35:38 -07001513 if (pPipe) {
1514 if (pCB->lastBoundPipelineLayout) {
1515 string errorString;
1516 for (auto setIndex : pPipe->active_sets) {
1517 // If valid set is not bound throw an error
1518 if ((pCB->boundDescriptorSets.size() <= setIndex) || (!pCB->boundDescriptorSets[setIndex])) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001519 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_DESCRIPTOR_SET_NOT_BOUND, "DS",
Mark Lobodzinski74635932015-12-18 15:35:38 -07001520 "VkPipeline %#" PRIxLEAST64 " uses set #%u but that set is not bound.", (uint64_t)pPipe->pipeline, setIndex);
1521 } else if (!verify_set_layout_compatibility(my_data, my_data->setMap[pCB->boundDescriptorSets[setIndex]], pPipe->graphicsPipelineCI.layout, setIndex, errorString)) {
1522 // Set is bound but not compatible w/ overlapping pipelineLayout from PSO
1523 VkDescriptorSet setHandle = my_data->setMap[pCB->boundDescriptorSets[setIndex]]->set;
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001524 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)setHandle, __LINE__, DRAWSTATE_PIPELINE_LAYOUTS_INCOMPATIBLE, "DS",
Mark Lobodzinski74635932015-12-18 15:35:38 -07001525 "VkDescriptorSet (%#" PRIxLEAST64 ") bound as set #%u is not compatible with overlapping VkPipelineLayout %#" PRIxLEAST64 " due to: %s",
1526 (uint64_t)setHandle, setIndex, (uint64_t)pPipe->graphicsPipelineCI.layout, errorString.c_str());
Tobin Ehlis43c39c02016-01-11 13:18:40 -07001527 } else { // Valid set is bound and layout compatible, validate that it's updated and verify any dynamic offsets
1528 // Pull the set node
Tobin Ehlisf6585052015-12-17 11:48:42 -07001529 SET_NODE* pSet = getSetNode(my_data, pCB->boundDescriptorSets[setIndex]);
Tobin Ehlis43c39c02016-01-11 13:18:40 -07001530 // Make sure set has been updated
1531 if (!pSet->pUpdateStructs) {
1532 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pSet->set, __LINE__, DRAWSTATE_DESCRIPTOR_SET_NOT_UPDATED, "DS",
1533 "DS %#" PRIxLEAST64 " bound but it was never updated. It is now being used to draw so this will result in undefined behavior.", (uint64_t) pSet->set);
1534 }
1535 // For each dynamic descriptor, make sure dynamic offset doesn't overstep buffer
Tobin Ehlisf6585052015-12-17 11:48:42 -07001536 result |= validate_dynamic_offsets(my_data, pSet);
Mark Lobodzinski74635932015-12-18 15:35:38 -07001537 }
Tobin Ehlis88452832015-12-03 09:40:56 -07001538 }
1539 }
Mark Lobodzinskidfcd9b62015-12-14 15:14:10 -07001540
Mark Lobodzinski74635932015-12-18 15:35:38 -07001541 // Verify Vtx binding
1542 if (pPipe->vtxBindingCount > 0) {
1543 VkPipelineVertexInputStateCreateInfo *vtxInCI = &pPipe->vertexInputCI;
1544 for (uint32_t i = 0; i < vtxInCI->vertexBindingDescriptionCount; i++) {
Michael Lentine700b0aa2015-10-30 17:57:32 -07001545 if ((pCB->currentDrawData.buffers.size() < (i+1)) || (pCB->currentDrawData.buffers[i] == VK_NULL_HANDLE)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001546 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_VTX_INDEX_OUT_OF_BOUNDS, "DS",
Mark Lobodzinski74635932015-12-18 15:35:38 -07001547 "The Pipeline State Object (%#" PRIxLEAST64 ") expects that this Command Buffer's vertex binding Index %d should be set via vkCmdBindVertexBuffers.",
1548 (uint64_t)pCB->lastBoundPipeline, i);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001549
Mark Lobodzinski74635932015-12-18 15:35:38 -07001550 }
1551 }
1552 } else {
Michael Lentine700b0aa2015-10-30 17:57:32 -07001553 if (!pCB->currentDrawData.buffers.empty()) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001554 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_PERF_WARN_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_VTX_INDEX_OUT_OF_BOUNDS,
Mark Lobodzinski74635932015-12-18 15:35:38 -07001555 "DS", "Vertex buffers are bound to command buffer (%#" PRIxLEAST64 ") but no vertex buffers are attached to this Pipeline State Object (%#" PRIxLEAST64 ").",
1556 (uint64_t)pCB->commandBuffer, (uint64_t)pCB->lastBoundPipeline);
Tobin Ehlisf7bf4502015-09-09 15:12:35 -06001557 }
1558 }
Mark Lobodzinskidfcd9b62015-12-14 15:14:10 -07001559
Mark Lobodzinski74635932015-12-18 15:35:38 -07001560 // If Viewport or scissors are dynamic, verify that dynamic count matches PSO count
1561 VkBool32 dynViewport = isDynamic(pPipe, VK_DYNAMIC_STATE_VIEWPORT);
1562 VkBool32 dynScissor = isDynamic(pPipe, VK_DYNAMIC_STATE_SCISSOR);
1563 if (dynViewport) {
1564 if (pCB->viewports.size() != pPipe->graphicsPipelineCI.pViewportState->viewportCount) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001565 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
Mark Lobodzinski74635932015-12-18 15:35:38 -07001566 "Dynamic viewportCount from vkCmdSetViewport() is " PRINTF_SIZE_T_SPECIFIER ", but PSO viewportCount is %u. These counts must match.", pCB->viewports.size(), pPipe->graphicsPipelineCI.pViewportState->viewportCount);
1567 }
Tobin Ehlisd332f282015-10-02 11:00:56 -06001568 }
Mark Lobodzinski74635932015-12-18 15:35:38 -07001569 if (dynScissor) {
1570 if (pCB->scissors.size() != pPipe->graphicsPipelineCI.pViewportState->scissorCount) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001571 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
Mark Lobodzinski74635932015-12-18 15:35:38 -07001572 "Dynamic scissorCount from vkCmdSetScissor() is " PRINTF_SIZE_T_SPECIFIER ", but PSO scissorCount is %u. These counts must match.", pCB->scissors.size(), pPipe->graphicsPipelineCI.pViewportState->scissorCount);
1573 }
Tobin Ehlisd332f282015-10-02 11:00:56 -06001574 }
1575 }
Tobin Ehlis429b91d2015-06-22 17:20:50 -06001576 return result;
1577}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001578
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001579// Verify that create state for a pipeline is valid
Tobin Ehlis88452832015-12-03 09:40:56 -07001580static VkBool32 verifyPipelineCreateState(layer_data* my_data, const VkDevice device, PIPELINE_NODE* pPipeline)
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001581{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001582 VkBool32 skipCall = VK_FALSE;
Mark Lobodzinski2fd2d032015-12-16 14:25:22 -07001583
Tobin Ehlis88452832015-12-03 09:40:56 -07001584 if (!validate_pipeline_shaders(my_data, device, pPipeline)) {
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001585 skipCall = VK_TRUE;
1586 }
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001587 // VS is required
1588 if (!(pPipeline->active_shaders & VK_SHADER_STAGE_VERTEX_BIT)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001589 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001590 "Invalid Pipeline CreateInfo State: Vtx Shader required");
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001591 }
1592 // Either both or neither TC/TE shaders should be defined
Courtney Goeltzenleuchter96835892015-10-15 17:35:38 -06001593 if (((pPipeline->active_shaders & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT) == 0) !=
1594 ((pPipeline->active_shaders & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) == 0) ) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001595 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001596 "Invalid Pipeline CreateInfo State: TE and TC shaders must be included or excluded as a pair");
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001597 }
1598 // Compute shaders should be specified independent of Gfx shaders
1599 if ((pPipeline->active_shaders & VK_SHADER_STAGE_COMPUTE_BIT) &&
Courtney Goeltzenleuchter96835892015-10-15 17:35:38 -06001600 (pPipeline->active_shaders & (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT |
1601 VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT | VK_SHADER_STAGE_GEOMETRY_BIT |
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001602 VK_SHADER_STAGE_FRAGMENT_BIT))) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001603 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001604 "Invalid Pipeline CreateInfo State: Do not specify Compute Shader for Gfx Pipeline");
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001605 }
Chia-I Wu515eb8f2015-10-31 00:31:16 +08001606 // VK_PRIMITIVE_TOPOLOGY_PATCH_LIST primitive topology is only valid for tessellation pipelines.
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001607 // Mismatching primitive topology and tessellation fails graphics pipeline creation.
Courtney Goeltzenleuchter96835892015-10-15 17:35:38 -06001608 if (pPipeline->active_shaders & (VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) &&
Chia-I Wu515eb8f2015-10-31 00:31:16 +08001609 (pPipeline->iaStateCI.topology != VK_PRIMITIVE_TOPOLOGY_PATCH_LIST)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001610 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
Chia-I Wu515eb8f2015-10-31 00:31:16 +08001611 "Invalid Pipeline CreateInfo State: VK_PRIMITIVE_TOPOLOGY_PATCH_LIST must be set as IA topology for tessellation pipelines");
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001612 }
Chia-I Wu515eb8f2015-10-31 00:31:16 +08001613 if (pPipeline->iaStateCI.topology == VK_PRIMITIVE_TOPOLOGY_PATCH_LIST) {
Courtney Goeltzenleuchter96835892015-10-15 17:35:38 -06001614 if (~pPipeline->active_shaders & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001615 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
Chia-I Wu515eb8f2015-10-31 00:31:16 +08001616 "Invalid Pipeline CreateInfo State: VK_PRIMITIVE_TOPOLOGY_PATCH_LIST primitive topology is only valid for tessellation pipelines");
Tobin Ehlis912df022015-09-17 08:46:18 -06001617 }
1618 if (!pPipeline->tessStateCI.patchControlPoints || (pPipeline->tessStateCI.patchControlPoints > 32)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001619 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
Chia-I Wu515eb8f2015-10-31 00:31:16 +08001620 "Invalid Pipeline CreateInfo State: VK_PRIMITIVE_TOPOLOGY_PATCH_LIST primitive topology used with patchControlPoints value %u."
Tobin Ehlis912df022015-09-17 08:46:18 -06001621 " patchControlPoints should be >0 and <=32.", pPipeline->tessStateCI.patchControlPoints);
1622 }
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001623 }
Tobin Ehlisd332f282015-10-02 11:00:56 -06001624 // Viewport state must be included and viewport and scissor counts should always match
Tobin Ehlise68360f2015-10-01 11:15:13 -06001625 // NOTE : Even if these are flagged as dynamic, counts need to be set correctly for shader compiler
Tobin Ehlisd332f282015-10-02 11:00:56 -06001626 if (!pPipeline->graphicsPipelineCI.pViewportState) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001627 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
Tobin Ehlisd332f282015-10-02 11:00:56 -06001628 "Gfx Pipeline pViewportState is null. Even if viewport and scissors are dynamic PSO must include viewportCount and scissorCount in pViewportState.");
1629 } else if (pPipeline->graphicsPipelineCI.pViewportState->scissorCount != pPipeline->graphicsPipelineCI.pViewportState->viewportCount) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001630 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
Tobin Ehlise68360f2015-10-01 11:15:13 -06001631 "Gfx Pipeline viewport count (%u) must match scissor count (%u).", pPipeline->vpStateCI.viewportCount, pPipeline->vpStateCI.scissorCount);
Tobin Ehlisd332f282015-10-02 11:00:56 -06001632 } else {
1633 // If viewport or scissor are not dynamic, then verify that data is appropriate for count
1634 VkBool32 dynViewport = isDynamic(pPipeline, VK_DYNAMIC_STATE_VIEWPORT);
1635 VkBool32 dynScissor = isDynamic(pPipeline, VK_DYNAMIC_STATE_SCISSOR);
1636 if (!dynViewport) {
1637 if (pPipeline->graphicsPipelineCI.pViewportState->viewportCount && !pPipeline->graphicsPipelineCI.pViewportState->pViewports) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001638 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
Courtney Goeltzenleuchterb19042e2015-11-25 11:38:54 -07001639 "Gfx Pipeline viewportCount is %u, but pViewports is NULL. For non-zero viewportCount, you must either include pViewports data, or include viewport in pDynamicState and set it with vkCmdSetViewport().", pPipeline->graphicsPipelineCI.pViewportState->viewportCount);
Tobin Ehlise68360f2015-10-01 11:15:13 -06001640 }
1641 }
Tobin Ehlisd332f282015-10-02 11:00:56 -06001642 if (!dynScissor) {
1643 if (pPipeline->graphicsPipelineCI.pViewportState->scissorCount && !pPipeline->graphicsPipelineCI.pViewportState->pScissors) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001644 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
Courtney Goeltzenleuchterb19042e2015-11-25 11:38:54 -07001645 "Gfx Pipeline scissorCount is %u, but pScissors is NULL. For non-zero scissorCount, you must either include pScissors data, or include scissor in pDynamicState and set it with vkCmdSetScissor().", pPipeline->graphicsPipelineCI.pViewportState->scissorCount);
Tobin Ehlisd332f282015-10-02 11:00:56 -06001646 }
Tobin Ehlise68360f2015-10-01 11:15:13 -06001647 }
1648 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001649 return skipCall;
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001650}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001651
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001652// Init the pipeline mapping info based on pipeline create info LL tree
1653// Threading note : Calls to this function should wrapped in mutex
Tobin Ehlis88452832015-12-03 09:40:56 -07001654// TODO : this should really just be in the constructor for PIPELINE_NODE
Mark Lobodzinski475a2182015-11-10 15:25:01 -07001655static PIPELINE_NODE* initGraphicsPipeline(layer_data* dev_data, const VkGraphicsPipelineCreateInfo* pCreateInfo, PIPELINE_NODE* pBasePipeline)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001656{
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001657 PIPELINE_NODE* pPipeline = new PIPELINE_NODE;
Mark Lobodzinskic44baa52015-12-11 11:56:07 -07001658
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001659 if (pBasePipeline) {
Mark Lobodzinskic44baa52015-12-11 11:56:07 -07001660 *pPipeline = *pBasePipeline;
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001661 }
Mark Lobodzinskic44baa52015-12-11 11:56:07 -07001662
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001663 // First init create info
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001664 memcpy(&pPipeline->graphicsPipelineCI, pCreateInfo, sizeof(VkGraphicsPipelineCreateInfo));
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001665
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001666 size_t bufferSize = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001667 const VkPipelineVertexInputStateCreateInfo* pVICI = NULL;
Tobin Ehlis5a65cca2015-07-13 13:14:24 -06001668 const VkPipelineColorBlendStateCreateInfo* pCBCI = NULL;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001669
1670 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
1671 const VkPipelineShaderStageCreateInfo *pPSSCI = &pCreateInfo->pStages[i];
1672
Chia-I Wu28e06912015-10-31 00:31:16 +08001673 switch (pPSSCI->stage) {
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001674 case VK_SHADER_STAGE_VERTEX_BIT:
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001675 memcpy(&pPipeline->vsCI, pPSSCI, sizeof(VkPipelineShaderStageCreateInfo));
1676 pPipeline->active_shaders |= VK_SHADER_STAGE_VERTEX_BIT;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001677 break;
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001678 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001679 memcpy(&pPipeline->tcsCI, pPSSCI, sizeof(VkPipelineShaderStageCreateInfo));
Courtney Goeltzenleuchter96835892015-10-15 17:35:38 -06001680 pPipeline->active_shaders |= VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001681 break;
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001682 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001683 memcpy(&pPipeline->tesCI, pPSSCI, sizeof(VkPipelineShaderStageCreateInfo));
Courtney Goeltzenleuchter96835892015-10-15 17:35:38 -06001684 pPipeline->active_shaders |= VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001685 break;
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001686 case VK_SHADER_STAGE_GEOMETRY_BIT:
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001687 memcpy(&pPipeline->gsCI, pPSSCI, sizeof(VkPipelineShaderStageCreateInfo));
1688 pPipeline->active_shaders |= VK_SHADER_STAGE_GEOMETRY_BIT;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001689 break;
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001690 case VK_SHADER_STAGE_FRAGMENT_BIT:
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001691 memcpy(&pPipeline->fsCI, pPSSCI, sizeof(VkPipelineShaderStageCreateInfo));
1692 pPipeline->active_shaders |= VK_SHADER_STAGE_FRAGMENT_BIT;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001693 break;
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001694 case VK_SHADER_STAGE_COMPUTE_BIT:
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001695 // TODO : Flag error, CS is specified through VkComputePipelineCreateInfo
1696 pPipeline->active_shaders |= VK_SHADER_STAGE_COMPUTE_BIT;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001697 break;
1698 default:
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001699 // TODO : Flag error
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001700 break;
1701 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001702 }
Tobin Ehlis963a4042015-09-29 08:18:34 -06001703 // Copy over GraphicsPipelineCreateInfo structure embedded pointers
1704 if (pCreateInfo->stageCount != 0) {
1705 pPipeline->graphicsPipelineCI.pStages = new VkPipelineShaderStageCreateInfo[pCreateInfo->stageCount];
1706 bufferSize = pCreateInfo->stageCount * sizeof(VkPipelineShaderStageCreateInfo);
1707 memcpy((void*)pPipeline->graphicsPipelineCI.pStages, pCreateInfo->pStages, bufferSize);
1708 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001709 if (pCreateInfo->pVertexInputState != NULL) {
1710 memcpy((void*)&pPipeline->vertexInputCI, pCreateInfo->pVertexInputState , sizeof(VkPipelineVertexInputStateCreateInfo));
1711 // Copy embedded ptrs
1712 pVICI = pCreateInfo->pVertexInputState;
Chia-I Wud50a7d72015-10-26 20:48:51 +08001713 pPipeline->vtxBindingCount = pVICI->vertexBindingDescriptionCount;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001714 if (pPipeline->vtxBindingCount) {
1715 pPipeline->pVertexBindingDescriptions = new VkVertexInputBindingDescription[pPipeline->vtxBindingCount];
1716 bufferSize = pPipeline->vtxBindingCount * sizeof(VkVertexInputBindingDescription);
1717 memcpy((void*)pPipeline->pVertexBindingDescriptions, pVICI->pVertexBindingDescriptions, bufferSize);
1718 }
Chia-I Wud50a7d72015-10-26 20:48:51 +08001719 pPipeline->vtxAttributeCount = pVICI->vertexAttributeDescriptionCount;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001720 if (pPipeline->vtxAttributeCount) {
1721 pPipeline->pVertexAttributeDescriptions = new VkVertexInputAttributeDescription[pPipeline->vtxAttributeCount];
1722 bufferSize = pPipeline->vtxAttributeCount * sizeof(VkVertexInputAttributeDescription);
1723 memcpy((void*)pPipeline->pVertexAttributeDescriptions, pVICI->pVertexAttributeDescriptions, bufferSize);
1724 }
1725 pPipeline->graphicsPipelineCI.pVertexInputState = &pPipeline->vertexInputCI;
1726 }
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001727 if (pCreateInfo->pInputAssemblyState != NULL) {
1728 memcpy((void*)&pPipeline->iaStateCI, pCreateInfo->pInputAssemblyState, sizeof(VkPipelineInputAssemblyStateCreateInfo));
1729 pPipeline->graphicsPipelineCI.pInputAssemblyState = &pPipeline->iaStateCI;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001730 }
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001731 if (pCreateInfo->pTessellationState != NULL) {
1732 memcpy((void*)&pPipeline->tessStateCI, pCreateInfo->pTessellationState, sizeof(VkPipelineTessellationStateCreateInfo));
1733 pPipeline->graphicsPipelineCI.pTessellationState = &pPipeline->tessStateCI;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001734 }
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001735 if (pCreateInfo->pViewportState != NULL) {
1736 memcpy((void*)&pPipeline->vpStateCI, pCreateInfo->pViewportState, sizeof(VkPipelineViewportStateCreateInfo));
1737 pPipeline->graphicsPipelineCI.pViewportState = &pPipeline->vpStateCI;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001738 }
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001739 if (pCreateInfo->pRasterizationState != NULL) {
1740 memcpy((void*)&pPipeline->rsStateCI, pCreateInfo->pRasterizationState, sizeof(VkPipelineRasterizationStateCreateInfo));
1741 pPipeline->graphicsPipelineCI.pRasterizationState = &pPipeline->rsStateCI;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001742 }
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001743 if (pCreateInfo->pMultisampleState != NULL) {
1744 memcpy((void*)&pPipeline->msStateCI, pCreateInfo->pMultisampleState, sizeof(VkPipelineMultisampleStateCreateInfo));
1745 pPipeline->graphicsPipelineCI.pMultisampleState = &pPipeline->msStateCI;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001746 }
Tobin Ehlis963a4042015-09-29 08:18:34 -06001747 if (pCreateInfo->pDepthStencilState != NULL) {
1748 memcpy((void*)&pPipeline->dsStateCI, pCreateInfo->pDepthStencilState, sizeof(VkPipelineDepthStencilStateCreateInfo));
1749 pPipeline->graphicsPipelineCI.pDepthStencilState = &pPipeline->dsStateCI;
1750 }
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001751 if (pCreateInfo->pColorBlendState != NULL) {
1752 memcpy((void*)&pPipeline->cbStateCI, pCreateInfo->pColorBlendState, sizeof(VkPipelineColorBlendStateCreateInfo));
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001753 // Copy embedded ptrs
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001754 pCBCI = pCreateInfo->pColorBlendState;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001755 pPipeline->attachmentCount = pCBCI->attachmentCount;
1756 if (pPipeline->attachmentCount) {
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001757 pPipeline->pAttachments = new VkPipelineColorBlendAttachmentState[pPipeline->attachmentCount];
1758 bufferSize = pPipeline->attachmentCount * sizeof(VkPipelineColorBlendAttachmentState);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001759 memcpy((void*)pPipeline->pAttachments, pCBCI->pAttachments, bufferSize);
1760 }
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001761 pPipeline->graphicsPipelineCI.pColorBlendState = &pPipeline->cbStateCI;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001762 }
Tobin Ehlis963a4042015-09-29 08:18:34 -06001763 if (pCreateInfo->pDynamicState != NULL) {
1764 memcpy((void*)&pPipeline->dynStateCI, pCreateInfo->pDynamicState, sizeof(VkPipelineDynamicStateCreateInfo));
1765 if (pPipeline->dynStateCI.dynamicStateCount) {
1766 pPipeline->dynStateCI.pDynamicStates = new VkDynamicState[pPipeline->dynStateCI.dynamicStateCount];
1767 bufferSize = pPipeline->dynStateCI.dynamicStateCount * sizeof(VkDynamicState);
1768 memcpy((void*)pPipeline->dynStateCI.pDynamicStates, pCreateInfo->pDynamicState->pDynamicStates, bufferSize);
1769 }
1770 pPipeline->graphicsPipelineCI.pDynamicState = &pPipeline->dynStateCI;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001771 }
Tobin Ehlis88452832015-12-03 09:40:56 -07001772 pPipeline->active_sets.clear();
Tobin Ehlis75283bf2015-06-18 15:59:33 -06001773 return pPipeline;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001774}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001775
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001776// Free the Pipeline nodes
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06001777static void deletePipelines(layer_data* my_data)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001778{
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06001779 if (my_data->pipelineMap.size() <= 0)
David Pinedod8f83d82015-04-27 16:36:17 -06001780 return;
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06001781 for (auto ii=my_data->pipelineMap.begin(); ii!=my_data->pipelineMap.end(); ++ii) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001782 if ((*ii).second->graphicsPipelineCI.stageCount != 0) {
1783 delete[] (*ii).second->graphicsPipelineCI.pStages;
1784 }
Tobin Ehlisf313c8b2015-04-01 11:59:08 -06001785 if ((*ii).second->pVertexBindingDescriptions) {
1786 delete[] (*ii).second->pVertexBindingDescriptions;
1787 }
1788 if ((*ii).second->pVertexAttributeDescriptions) {
1789 delete[] (*ii).second->pVertexAttributeDescriptions;
1790 }
1791 if ((*ii).second->pAttachments) {
1792 delete[] (*ii).second->pAttachments;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001793 }
Tobin Ehlis963a4042015-09-29 08:18:34 -06001794 if ((*ii).second->dynStateCI.dynamicStateCount != 0) {
1795 delete[] (*ii).second->dynStateCI.pDynamicStates;
1796 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001797 delete (*ii).second;
1798 }
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06001799 my_data->pipelineMap.clear();
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001800}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001801
Tobin Ehliseba312c2015-04-01 08:40:34 -06001802// For given pipeline, return number of MSAA samples, or one if MSAA disabled
Chia-I Wu5c17c962015-10-31 00:31:16 +08001803static VkSampleCountFlagBits getNumSamples(layer_data* my_data, const VkPipeline pipeline)
Tobin Ehliseba312c2015-04-01 08:40:34 -06001804{
Chia-I Wue2fc5522015-10-26 20:04:44 +08001805 PIPELINE_NODE* pPipe = my_data->pipelineMap[pipeline];
Tobin Ehlis577188e2015-07-13 14:51:15 -06001806 if (VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO == pPipe->msStateCI.sType) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001807 return pPipe->msStateCI.rasterizationSamples;
Tobin Ehlis577188e2015-07-13 14:51:15 -06001808 }
Chia-I Wu5c17c962015-10-31 00:31:16 +08001809 return VK_SAMPLE_COUNT_1_BIT;
Tobin Ehliseba312c2015-04-01 08:40:34 -06001810}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001811
Tobin Ehliseba312c2015-04-01 08:40:34 -06001812// Validate state related to the PSO
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06001813static VkBool32 validatePipelineState(layer_data* my_data, const GLOBAL_CB_NODE* pCB, const VkPipelineBindPoint pipelineBindPoint, const VkPipeline pipeline)
Tobin Ehliseba312c2015-04-01 08:40:34 -06001814{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001815 if (VK_PIPELINE_BIND_POINT_GRAPHICS == pipelineBindPoint) {
Tobin Ehliseba312c2015-04-01 08:40:34 -06001816 // Verify that any MSAA request in PSO matches sample# in bound FB
Chia-I Wu5c17c962015-10-31 00:31:16 +08001817 VkSampleCountFlagBits psoNumSamples = getNumSamples(my_data, pipeline);
Tobin Ehliseba312c2015-04-01 08:40:34 -06001818 if (pCB->activeRenderPass) {
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001819 const VkRenderPassCreateInfo* pRPCI = my_data->renderPassMap[pCB->activeRenderPass]->pCreateInfo;
Chia-I Wu08accc62015-07-07 11:50:03 +08001820 const VkSubpassDescription* pSD = &pRPCI->pSubpasses[pCB->activeSubpass];
Chia-I Wu5c17c962015-10-31 00:31:16 +08001821 VkSampleCountFlagBits subpassNumSamples = (VkSampleCountFlagBits) 0;
Chia-I Wu08accc62015-07-07 11:50:03 +08001822 uint32_t i;
1823
Chia-I Wud50a7d72015-10-26 20:48:51 +08001824 for (i = 0; i < pSD->colorAttachmentCount; i++) {
Chia-I Wu5c17c962015-10-31 00:31:16 +08001825 VkSampleCountFlagBits samples;
Chia-I Wu08accc62015-07-07 11:50:03 +08001826
Cody Northropa505dda2015-08-04 11:16:41 -06001827 if (pSD->pColorAttachments[i].attachment == VK_ATTACHMENT_UNUSED)
Chia-I Wu08accc62015-07-07 11:50:03 +08001828 continue;
1829
Cody Northropa505dda2015-08-04 11:16:41 -06001830 samples = pRPCI->pAttachments[pSD->pColorAttachments[i].attachment].samples;
Chia-I Wu5c17c962015-10-31 00:31:16 +08001831 if (subpassNumSamples == (VkSampleCountFlagBits) 0) {
Chia-I Wu08accc62015-07-07 11:50:03 +08001832 subpassNumSamples = samples;
1833 } else if (subpassNumSamples != samples) {
Chia-I Wu5c17c962015-10-31 00:31:16 +08001834 subpassNumSamples = (VkSampleCountFlagBits) -1;
Chia-I Wu08accc62015-07-07 11:50:03 +08001835 break;
1836 }
1837 }
Chia-I Wu1efb7e52015-10-26 17:32:47 +08001838 if (pSD->pDepthStencilAttachment && pSD->pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
Chia-I Wu5c17c962015-10-31 00:31:16 +08001839 const VkSampleCountFlagBits samples = pRPCI->pAttachments[pSD->pDepthStencilAttachment->attachment].samples;
1840 if (subpassNumSamples == (VkSampleCountFlagBits) 0)
Chia-I Wu08accc62015-07-07 11:50:03 +08001841 subpassNumSamples = samples;
1842 else if (subpassNumSamples != samples)
Chia-I Wu5c17c962015-10-31 00:31:16 +08001843 subpassNumSamples = (VkSampleCountFlagBits) -1;
Chia-I Wu08accc62015-07-07 11:50:03 +08001844 }
1845
1846 if (psoNumSamples != subpassNumSamples) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001847 return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, (uint64_t) pipeline, __LINE__, DRAWSTATE_NUM_SAMPLES_MISMATCH, "DS",
Mark Lobodzinski5495d132015-09-30 16:19:16 -06001848 "Num samples mismatch! Binding PSO (%#" PRIxLEAST64 ") with %u samples while current RenderPass (%#" PRIxLEAST64 ") w/ %u samples!",
Chia-I Wue2fc5522015-10-26 20:04:44 +08001849 (uint64_t) pipeline, psoNumSamples, (uint64_t) pCB->activeRenderPass, subpassNumSamples);
Tobin Ehliseba312c2015-04-01 08:40:34 -06001850 }
1851 } else {
1852 // TODO : I believe it's an error if we reach this point and don't have an activeRenderPass
1853 // Verify and flag error as appropriate
1854 }
1855 // TODO : Add more checks here
1856 } else {
1857 // TODO : Validate non-gfx pipeline updates
1858 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001859 return VK_FALSE;
Tobin Ehliseba312c2015-04-01 08:40:34 -06001860}
1861
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001862// Block of code at start here specifically for managing/tracking DSs
1863
Tobin Ehlis793ad302015-04-03 12:01:11 -06001864// Return Pool node ptr for specified pool or else NULL
Mark Lobodzinski39298632015-11-18 08:38:27 -07001865static DESCRIPTOR_POOL_NODE* getPoolNode(layer_data* my_data, const VkDescriptorPool pool)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001866{
1867 loader_platform_thread_lock_mutex(&globalLock);
Mark Lobodzinski39298632015-11-18 08:38:27 -07001868 if (my_data->descriptorPoolMap.find(pool) == my_data->descriptorPoolMap.end()) {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001869 loader_platform_thread_unlock_mutex(&globalLock);
1870 return NULL;
1871 }
1872 loader_platform_thread_unlock_mutex(&globalLock);
Mark Lobodzinski39298632015-11-18 08:38:27 -07001873 return my_data->descriptorPoolMap[pool];
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001874}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001875
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06001876static LAYOUT_NODE* getLayoutNode(layer_data* my_data, const VkDescriptorSetLayout layout) {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001877 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001878 if (my_data->descriptorSetLayoutMap.find(layout) == my_data->descriptorSetLayoutMap.end()) {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001879 loader_platform_thread_unlock_mutex(&globalLock);
1880 return NULL;
1881 }
1882 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07001883 return my_data->descriptorSetLayoutMap[layout];
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001884}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001885
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001886// Return VK_FALSE if update struct is of valid type, otherwise flag error and return code from callback
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06001887static VkBool32 validUpdateStruct(layer_data* my_data, const VkDevice device, const GENERIC_HEADER* pUpdateStruct)
Tobin Ehlis0174fed2015-05-28 12:10:17 -06001888{
Tobin Ehlis0174fed2015-05-28 12:10:17 -06001889 switch (pUpdateStruct->sType)
1890 {
Chia-I Wu9d00ed72015-05-25 16:27:55 +08001891 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
1892 case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001893 return VK_FALSE;
Tobin Ehlis0174fed2015-05-28 12:10:17 -06001894 default:
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001895 return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_UPDATE_STRUCT, "DS",
Tobin Ehlisa16e8922015-06-16 15:50:44 -06001896 "Unexpected UPDATE struct of type %s (value %u) in vkUpdateDescriptors() struct tree", string_VkStructureType(pUpdateStruct->sType), pUpdateStruct->sType);
Tobin Ehlis0174fed2015-05-28 12:10:17 -06001897 }
1898}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001899
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001900// Set count for given update struct in the last parameter
Courtney Goeltzenleuchter085f8dd2015-12-16 16:08:44 -07001901// Return value of skipCall, which is only VK_TRUE if error occurs and callback signals execution to cease
Tobin Ehlisbaca3ab2015-10-27 12:54:50 -06001902static uint32_t getUpdateCount(layer_data* my_data, const VkDevice device, const GENERIC_HEADER* pUpdateStruct)
Tobin Ehlis793ad302015-04-03 12:01:11 -06001903{
1904 switch (pUpdateStruct->sType)
1905 {
Chia-I Wu9d00ed72015-05-25 16:27:55 +08001906 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
Chia-I Wud50a7d72015-10-26 20:48:51 +08001907 return ((VkWriteDescriptorSet*)pUpdateStruct)->descriptorCount;
Chia-I Wu9d00ed72015-05-25 16:27:55 +08001908 case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
Tobin Ehlis793ad302015-04-03 12:01:11 -06001909 // TODO : Need to understand this case better and make sure code is correct
Chia-I Wud50a7d72015-10-26 20:48:51 +08001910 return ((VkCopyDescriptorSet*)pUpdateStruct)->descriptorCount;
Tobin Ehlis793ad302015-04-03 12:01:11 -06001911 }
Courtney Goeltzenleuchter7f47bca2015-12-16 16:08:08 -07001912
1913 return 0;
Tobin Ehlis793ad302015-04-03 12:01:11 -06001914}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001915
Tobin Ehlis793ad302015-04-03 12:01:11 -06001916// For given Layout Node and binding, return index where that binding begins
1917static uint32_t getBindingStartIndex(const LAYOUT_NODE* pLayout, const uint32_t binding)
1918{
1919 uint32_t offsetIndex = 0;
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001920 for (uint32_t i = 0; i < pLayout->createInfo.bindingCount; i++) {
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07001921 if (pLayout->createInfo.pBindings[i].binding == binding)
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001922 break;
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07001923 offsetIndex += pLayout->createInfo.pBindings[i].descriptorCount;
Tobin Ehlis793ad302015-04-03 12:01:11 -06001924 }
1925 return offsetIndex;
1926}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001927
Tobin Ehlis793ad302015-04-03 12:01:11 -06001928// For given layout node and binding, return last index that is updated
1929static uint32_t getBindingEndIndex(const LAYOUT_NODE* pLayout, const uint32_t binding)
1930{
1931 uint32_t offsetIndex = 0;
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001932 for (uint32_t i = 0; i < pLayout->createInfo.bindingCount; i++) {
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07001933 offsetIndex += pLayout->createInfo.pBindings[i].descriptorCount;
1934 if (pLayout->createInfo.pBindings[i].binding == binding)
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001935 break;
Tobin Ehlis793ad302015-04-03 12:01:11 -06001936 }
1937 return offsetIndex-1;
1938}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001939
Tobin Ehlisbaca3ab2015-10-27 12:54:50 -06001940// For given layout and update, return the first overall index of the layout that is updated
Tobin Ehlis8a62e632015-10-27 15:43:38 -06001941static uint32_t getUpdateStartIndex(layer_data* my_data, const VkDevice device, const LAYOUT_NODE* pLayout, const uint32_t binding, const uint32_t arrayIndex, const GENERIC_HEADER* pUpdateStruct)
Tobin Ehlis793ad302015-04-03 12:01:11 -06001942{
Tobin Ehlisbaca3ab2015-10-27 12:54:50 -06001943 return getBindingStartIndex(pLayout, binding)+arrayIndex;
Tobin Ehlis793ad302015-04-03 12:01:11 -06001944}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001945
Tobin Ehlis8a62e632015-10-27 15:43:38 -06001946// For given layout and update, return the last overall index of the layout that is updated
1947static uint32_t getUpdateEndIndex(layer_data* my_data, const VkDevice device, const LAYOUT_NODE* pLayout, const uint32_t binding, const uint32_t arrayIndex, const GENERIC_HEADER* pUpdateStruct)
Tobin Ehlis793ad302015-04-03 12:01:11 -06001948{
Tobin Ehlis8a62e632015-10-27 15:43:38 -06001949 uint32_t count = getUpdateCount(my_data, device, pUpdateStruct);
Tobin Ehlisbaca3ab2015-10-27 12:54:50 -06001950 return getBindingStartIndex(pLayout, binding)+arrayIndex+count-1;
Tobin Ehlis793ad302015-04-03 12:01:11 -06001951}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001952
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001953// Verify that the descriptor type in the update struct matches what's expected by the layout
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06001954static VkBool32 validateUpdateConsistency(layer_data* my_data, const VkDevice device, const LAYOUT_NODE* pLayout, const GENERIC_HEADER* pUpdateStruct, uint32_t startIndex, uint32_t endIndex)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001955{
1956 // First get actual type of update
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001957 VkBool32 skipCall = VK_FALSE;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001958 VkDescriptorType actualType;
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06001959 uint32_t i = 0;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001960 switch (pUpdateStruct->sType)
1961 {
Chia-I Wu9d00ed72015-05-25 16:27:55 +08001962 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
1963 actualType = ((VkWriteDescriptorSet*)pUpdateStruct)->descriptorType;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001964 break;
Chia-I Wu9d00ed72015-05-25 16:27:55 +08001965 case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
1966 /* no need to validate */
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001967 return VK_FALSE;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001968 break;
1969 default:
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001970 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_UPDATE_STRUCT, "DS",
Tobin Ehlisa16e8922015-06-16 15:50:44 -06001971 "Unexpected UPDATE struct of type %s (value %u) in vkUpdateDescriptors() struct tree", string_VkStructureType(pUpdateStruct->sType), pUpdateStruct->sType);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001972 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001973 if (VK_FALSE == skipCall) {
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06001974 // Set first stageFlags as reference and verify that all other updates match it
1975 VkShaderStageFlags refStageFlags = pLayout->stageFlags[startIndex];
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001976 for (i = startIndex; i <= endIndex; i++) {
Tobin Ehlis644ff042015-10-20 10:11:55 -06001977 if (pLayout->descriptorTypes[i] != actualType) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001978 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_DESCRIPTOR_TYPE_MISMATCH, "DS",
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06001979 "Write descriptor update has descriptor type %s that does not match overlapping binding descriptor type of %s!",
1980 string_VkDescriptorType(actualType), string_VkDescriptorType(pLayout->descriptorTypes[i]));
1981 }
1982 if (pLayout->stageFlags[i] != refStageFlags) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07001983 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_DESCRIPTOR_STAGEFLAGS_MISMATCH, "DS",
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06001984 "Write descriptor update has stageFlags %x that do not match overlapping binding descriptor stageFlags of %x!",
1985 refStageFlags, pLayout->stageFlags[i]);
Tobin Ehlis483cc352015-09-30 08:30:20 -06001986 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001987 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001988 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001989 return skipCall;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001990}
Tobin Ehlis559c6382015-11-05 09:52:49 -07001991
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001992// Determine the update type, allocate a new struct of that type, shadow the given pUpdate
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001993// struct into the pNewNode param. Return VK_TRUE if error condition encountered and callback signals early exit.
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001994// NOTE : Calls to this function should be wrapped in mutex
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06001995static VkBool32 shadowUpdateNode(layer_data* my_data, const VkDevice device, GENERIC_HEADER* pUpdate, GENERIC_HEADER** pNewNode)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06001996{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06001997 VkBool32 skipCall = VK_FALSE;
Chia-I Wu9d00ed72015-05-25 16:27:55 +08001998 VkWriteDescriptorSet* pWDS = NULL;
1999 VkCopyDescriptorSet* pCDS = NULL;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002000 size_t array_size = 0;
2001 size_t base_array_size = 0;
2002 size_t total_array_size = 0;
2003 size_t baseBuffAddr = 0;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002004 switch (pUpdate->sType)
2005 {
Chia-I Wu9d00ed72015-05-25 16:27:55 +08002006 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
2007 pWDS = new VkWriteDescriptorSet;
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002008 *pNewNode = (GENERIC_HEADER*)pWDS;
Chia-I Wu9d00ed72015-05-25 16:27:55 +08002009 memcpy(pWDS, pUpdate, sizeof(VkWriteDescriptorSet));
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -06002010
2011 switch (pWDS->descriptorType) {
2012 case VK_DESCRIPTOR_TYPE_SAMPLER:
2013 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
2014 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2015 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
2016 {
Chia-I Wud50a7d72015-10-26 20:48:51 +08002017 VkDescriptorImageInfo *info = new VkDescriptorImageInfo[pWDS->descriptorCount];
2018 memcpy(info, pWDS->pImageInfo, pWDS->descriptorCount * sizeof(VkDescriptorImageInfo));
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -06002019 pWDS->pImageInfo = info;
Tobin Ehlis8fab6562015-12-01 09:57:09 -07002020 }
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -06002021 break;
2022 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2023 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2024 {
Chia-I Wud50a7d72015-10-26 20:48:51 +08002025 VkBufferView *info = new VkBufferView[pWDS->descriptorCount];
2026 memcpy(info, pWDS->pTexelBufferView, pWDS->descriptorCount * sizeof(VkBufferView));
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -06002027 pWDS->pTexelBufferView = info;
2028 }
2029 break;
2030 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2031 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2032 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2033 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
2034 {
Chia-I Wud50a7d72015-10-26 20:48:51 +08002035 VkDescriptorBufferInfo *info = new VkDescriptorBufferInfo[pWDS->descriptorCount];
2036 memcpy(info, pWDS->pBufferInfo, pWDS->descriptorCount * sizeof(VkDescriptorBufferInfo));
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -06002037 pWDS->pBufferInfo = info;
2038 }
2039 break;
2040 default:
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07002041 return VK_ERROR_VALIDATION_FAILED_EXT;
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -06002042 break;
2043 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002044 break;
Chia-I Wu9d00ed72015-05-25 16:27:55 +08002045 case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
2046 pCDS = new VkCopyDescriptorSet;
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002047 *pNewNode = (GENERIC_HEADER*)pCDS;
Chia-I Wu9d00ed72015-05-25 16:27:55 +08002048 memcpy(pCDS, pUpdate, sizeof(VkCopyDescriptorSet));
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002049 break;
2050 default:
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002051 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_UPDATE_STRUCT, "DS",
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002052 "Unexpected UPDATE struct of type %s (value %u) in vkUpdateDescriptors() struct tree", string_VkStructureType(pUpdate->sType), pUpdate->sType))
2053 return VK_TRUE;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002054 }
2055 // Make sure that pNext for the end of shadow copy is NULL
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002056 (*pNewNode)->pNext = NULL;
2057 return skipCall;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002058}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002059
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002060// Verify that given sampler is valid
2061static VkBool32 validateSampler(const layer_data* my_data, const VkSampler* pSampler, const VkBool32 immutable)
2062{
2063 VkBool32 skipCall = VK_FALSE;
Chia-I Wue2fc5522015-10-26 20:04:44 +08002064 auto sampIt = my_data->sampleMap.find(*pSampler);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002065 if (sampIt == my_data->sampleMap.end()) {
2066 if (!immutable) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002067 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT, (uint64_t) *pSampler, __LINE__, DRAWSTATE_SAMPLER_DESCRIPTOR_ERROR, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002068 "vkUpdateDescriptorSets: Attempt to update descriptor with invalid sampler %#" PRIxLEAST64, (uint64_t) *pSampler);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002069 } else { // immutable
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002070 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT, (uint64_t) *pSampler, __LINE__, DRAWSTATE_SAMPLER_DESCRIPTOR_ERROR, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002071 "vkUpdateDescriptorSets: Attempt to update descriptor whose binding has an invalid immutable sampler %#" PRIxLEAST64, (uint64_t) *pSampler);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002072 }
2073 } else {
2074 // TODO : Any further checks we want to do on the sampler?
2075 }
2076 return skipCall;
2077}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002078
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002079// Verify that given imageView is valid
2080static VkBool32 validateImageView(const layer_data* my_data, const VkImageView* pImageView, const VkImageLayout imageLayout)
2081{
2082 VkBool32 skipCall = VK_FALSE;
Chia-I Wue2fc5522015-10-26 20:04:44 +08002083 auto ivIt = my_data->imageViewMap.find(*pImageView);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002084 if (ivIt == my_data->imageViewMap.end()) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002085 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT, (uint64_t) *pImageView, __LINE__, DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002086 "vkUpdateDescriptorSets: Attempt to update descriptor with invalid imageView %#" PRIxLEAST64, (uint64_t) *pImageView);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002087 } else {
2088 // Validate that imageLayout is compatible with aspectMask and image format
2089 VkImageAspectFlags aspectMask = ivIt->second->subresourceRange.aspectMask;
Chia-I Wue2fc5522015-10-26 20:04:44 +08002090 VkImage image = ivIt->second->image;
Michael Lentine7b236262015-10-23 12:41:44 -07002091 // TODO : Check here in case we have a bad image
Tobin Ehlis75cd1c52016-01-21 10:21:04 -07002092 auto imgIt = my_data->imageLayoutMap.find(image);
2093 if (imgIt == my_data->imageLayoutMap.end()) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002094 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t) image, __LINE__, DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS",
Michael Lentine7b236262015-10-23 12:41:44 -07002095 "vkUpdateDescriptorSets: Attempt to update descriptor with invalid image %#" PRIxLEAST64 " in imageView %#" PRIxLEAST64, (uint64_t) image, (uint64_t) *pImageView);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002096 } else {
2097 VkFormat format = (*imgIt).second->format;
Courtney Goeltzenleuchter6ed5dc22015-11-03 15:41:43 -07002098 VkBool32 ds = vk_format_is_depth_or_stencil(format);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002099 switch (imageLayout) {
2100 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
2101 // Only Color bit must be set
2102 if ((aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002103 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT, (uint64_t) *pImageView, __LINE__,
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002104 DRAWSTATE_INVALID_IMAGE_ASPECT, "DS", "vkUpdateDescriptorSets: Updating descriptor with layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL and imageView %#" PRIxLEAST64 ""
Chia-I Wue2fc5522015-10-26 20:04:44 +08002105 " that does not have VK_IMAGE_ASPECT_COLOR_BIT set.", (uint64_t) *pImageView);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002106 }
2107 // format must NOT be DS
2108 if (ds) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002109 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT, (uint64_t) *pImageView, __LINE__,
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002110 DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS", "vkUpdateDescriptorSets: Updating descriptor with layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL and imageView %#" PRIxLEAST64 ""
Chia-I Wue2fc5522015-10-26 20:04:44 +08002111 " but the image format is %s which is not a color format.", (uint64_t) *pImageView, string_VkFormat(format));
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002112 }
2113 break;
2114 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
2115 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2116 // Depth or stencil bit must be set, but both must NOT be set
2117 if (aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
2118 if (aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
2119 // both must NOT be set
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002120 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT, (uint64_t) *pImageView, __LINE__,
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002121 DRAWSTATE_INVALID_IMAGE_ASPECT, "DS", "vkUpdateDescriptorSets: Updating descriptor with imageView %#" PRIxLEAST64 ""
Chia-I Wue2fc5522015-10-26 20:04:44 +08002122 " that has both STENCIL and DEPTH aspects set", (uint64_t) *pImageView);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002123 }
2124 } else if (!(aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
2125 // Neither were set
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002126 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT, (uint64_t) *pImageView, __LINE__,
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002127 DRAWSTATE_INVALID_IMAGE_ASPECT, "DS", "vkUpdateDescriptorSets: Updating descriptor with layout %s and imageView %#" PRIxLEAST64 ""
Chia-I Wue2fc5522015-10-26 20:04:44 +08002128 " that does not have STENCIL or DEPTH aspect set.", string_VkImageLayout(imageLayout), (uint64_t) *pImageView);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002129 }
2130 // format must be DS
2131 if (!ds) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002132 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT, (uint64_t) *pImageView, __LINE__,
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002133 DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS", "vkUpdateDescriptorSets: Updating descriptor with layout %s and imageView %#" PRIxLEAST64 ""
Chia-I Wue2fc5522015-10-26 20:04:44 +08002134 " but the image format is %s which is not a depth/stencil format.", string_VkImageLayout(imageLayout), (uint64_t) *pImageView, string_VkFormat(format));
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002135 }
2136 break;
2137 default:
2138 // anything to check for other layouts?
2139 break;
2140 }
2141 }
2142 }
2143 return skipCall;
2144}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002145
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002146// Verify that given bufferView is valid
2147static VkBool32 validateBufferView(const layer_data* my_data, const VkBufferView* pBufferView)
2148{
2149 VkBool32 skipCall = VK_FALSE;
Chia-I Wue2fc5522015-10-26 20:04:44 +08002150 auto sampIt = my_data->bufferViewMap.find(*pBufferView);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002151 if (sampIt == my_data->bufferViewMap.end()) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002152 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT, (uint64_t) *pBufferView, __LINE__, DRAWSTATE_BUFFERVIEW_DESCRIPTOR_ERROR, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002153 "vkUpdateDescriptorSets: Attempt to update descriptor with invalid bufferView %#" PRIxLEAST64, (uint64_t) *pBufferView);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002154 } else {
2155 // TODO : Any further checks we want to do on the bufferView?
2156 }
2157 return skipCall;
2158}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002159
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002160// Verify that given bufferInfo is valid
2161static VkBool32 validateBufferInfo(const layer_data* my_data, const VkDescriptorBufferInfo* pBufferInfo)
2162{
2163 VkBool32 skipCall = VK_FALSE;
Chia-I Wue2fc5522015-10-26 20:04:44 +08002164 auto sampIt = my_data->bufferMap.find(pBufferInfo->buffer);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002165 if (sampIt == my_data->bufferMap.end()) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002166 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t) pBufferInfo->buffer, __LINE__, DRAWSTATE_BUFFERINFO_DESCRIPTOR_ERROR, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002167 "vkUpdateDescriptorSets: Attempt to update descriptor where bufferInfo has invalid buffer %#" PRIxLEAST64, (uint64_t) pBufferInfo->buffer);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002168 } else {
2169 // TODO : Any further checks we want to do on the bufferView?
2170 }
2171 return skipCall;
2172}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002173
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002174static VkBool32 validateUpdateContents(const layer_data* my_data, const VkWriteDescriptorSet *pWDS, const VkDescriptorSetLayoutBinding* pLayoutBinding)
2175{
2176 VkBool32 skipCall = VK_FALSE;
2177 // First verify that for the given Descriptor type, the correct DescriptorInfo data is supplied
2178 VkBufferView* pBufferView = NULL;
2179 const VkSampler* pSampler = NULL;
2180 VkImageView* pImageView = NULL;
2181 VkImageLayout* pImageLayout = NULL;
2182 VkDescriptorBufferInfo* pBufferInfo = NULL;
2183 VkBool32 immutable = VK_FALSE;
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002184 uint32_t i = 0;
2185 // For given update type, verify that update contents are correct
2186 switch (pWDS->descriptorType) {
2187 case VK_DESCRIPTOR_TYPE_SAMPLER:
Chia-I Wud50a7d72015-10-26 20:48:51 +08002188 for (i=0; i<pWDS->descriptorCount; ++i) {
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002189 skipCall |= validateSampler(my_data, &(pWDS->pImageInfo[i].sampler), immutable);
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002190 }
2191 break;
2192 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
Chia-I Wud50a7d72015-10-26 20:48:51 +08002193 for (i=0; i<pWDS->descriptorCount; ++i) {
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002194 if (NULL == pLayoutBinding->pImmutableSamplers) {
2195 pSampler = &(pWDS->pImageInfo[i].sampler);
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002196 if (immutable) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002197 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT, (uint64_t) *pSampler, __LINE__, DRAWSTATE_INCONSISTENT_IMMUTABLE_SAMPLER_UPDATE, "DS",
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002198 "vkUpdateDescriptorSets: Update #%u is not an immutable sampler %#" PRIxLEAST64 ", but previous update(s) from this "
2199 "VkWriteDescriptorSet struct used an immutable sampler. All updates from a single struct must either "
Chia-I Wue2fc5522015-10-26 20:04:44 +08002200 "use immutable or non-immutable samplers.", i, (uint64_t) *pSampler);
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002201 }
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002202 } else {
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002203 if (i>0 && !immutable) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002204 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT, (uint64_t) *pSampler, __LINE__, DRAWSTATE_INCONSISTENT_IMMUTABLE_SAMPLER_UPDATE, "DS",
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002205 "vkUpdateDescriptorSets: Update #%u is an immutable sampler, but previous update(s) from this "
2206 "VkWriteDescriptorSet struct used a non-immutable sampler. All updates from a single struct must either "
2207 "use immutable or non-immutable samplers.", i);
2208 }
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002209 immutable = VK_TRUE;
2210 pSampler = &(pLayoutBinding->pImmutableSamplers[i]);
2211 }
2212 skipCall |= validateSampler(my_data, pSampler, immutable);
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002213 }
2214 // Intentionally fall through here to also validate image stuff
2215 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2216 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
2217 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
Chia-I Wud50a7d72015-10-26 20:48:51 +08002218 for (i=0; i<pWDS->descriptorCount; ++i) {
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002219 skipCall |= validateImageView(my_data, &(pWDS->pImageInfo[i].imageView), pWDS->pImageInfo[i].imageLayout);
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002220 }
2221 break;
2222 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2223 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
Chia-I Wud50a7d72015-10-26 20:48:51 +08002224 for (i=0; i<pWDS->descriptorCount; ++i) {
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002225 skipCall |= validateBufferView(my_data, &(pWDS->pTexelBufferView[i]));
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002226 }
2227 break;
2228 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2229 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2230 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2231 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
Chia-I Wud50a7d72015-10-26 20:48:51 +08002232 for (i=0; i<pWDS->descriptorCount; ++i) {
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002233 skipCall |= validateBufferInfo(my_data, &(pWDS->pBufferInfo[i]));
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002234 }
2235 break;
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002236 }
2237 return skipCall;
2238}
Tobin Ehlis9c4f38d2016-01-14 12:47:19 -07002239// Validate that given set is valid and that it's not being used by an in-flight CmdBuffer
2240// func_str is the name of the calling function
2241// Return VK_FALSE if no errors occur
2242// Return VK_TRUE if validation error occurs and callback returns VK_TRUE (to skip upcoming API call down the chain)
2243VkBool32 validateIdleDescriptorSet(const layer_data* my_data, VkDescriptorSet set, std::string func_str) {
2244 VkBool32 skip_call = VK_FALSE;
2245 auto set_node = my_data->setMap.find(set);
2246 if (set_node == my_data->setMap.end()) {
Mark Youngee3f3a22016-01-25 12:18:32 -07002247 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)(set), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
2248 "Cannot call %s() on descriptor set %" PRIxLEAST64 " that has not been allocated.", func_str.c_str(), (uint64_t)(set));
Tobin Ehlis9c4f38d2016-01-14 12:47:19 -07002249 } else {
2250 if (set_node->second->in_use.load()) {
Mark Youngee3f3a22016-01-25 12:18:32 -07002251 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)(set), __LINE__, DRAWSTATE_OBJECT_INUSE, "DS",
2252 "Cannot call %s() on descriptor set %" PRIxLEAST64 " that is in use by a command buffer.", func_str.c_str(), (uint64_t)(set));
Tobin Ehlis9c4f38d2016-01-14 12:47:19 -07002253 }
2254 }
2255 return skip_call;
2256}
Tobin Ehlise6e574b2016-01-24 23:25:31 -07002257static void invalidateBoundCmdBuffers(layer_data* dev_data, const SET_NODE* pSet)
2258{
2259 // Flag any CBs this set is bound to as INVALID
2260 for (auto cb : pSet->boundCmdBuffers) {
2261 auto cb_node = dev_data->commandBufferMap.find(cb);
2262 if (cb_node != dev_data->commandBufferMap.end()) {
2263 cb_node->second->state = CB_INVALID;
2264 }
2265 }
2266}
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002267// update DS mappings based on write and copy update arrays
Chia-I Wu40cf0ae2015-10-26 17:20:32 +08002268static VkBool32 dsUpdate(layer_data* my_data, VkDevice device, uint32_t descriptorWriteCount, const VkWriteDescriptorSet* pWDS, uint32_t descriptorCopyCount, const VkCopyDescriptorSet* pCDS)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002269{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002270 VkBool32 skipCall = VK_FALSE;
Chia-I Wu9d00ed72015-05-25 16:27:55 +08002271
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002272 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002273 LAYOUT_NODE* pLayout = NULL;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06002274 VkDescriptorSetLayoutCreateInfo* pLayoutCI = NULL;
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002275 // Validate Write updates
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002276 uint32_t i = 0;
Chia-I Wu40cf0ae2015-10-26 17:20:32 +08002277 for (i=0; i < descriptorWriteCount; i++) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002278 VkDescriptorSet ds = pWDS[i].dstSet;
Chia-I Wue2fc5522015-10-26 20:04:44 +08002279 SET_NODE* pSet = my_data->setMap[ds];
Tobin Ehlis9c4f38d2016-01-14 12:47:19 -07002280 // Set being updated cannot be in-flight
2281 if ((skipCall = validateIdleDescriptorSet(my_data, ds, "VkUpdateDescriptorSets")) == VK_TRUE)
2282 return skipCall;
Tobin Ehlise6e574b2016-01-24 23:25:31 -07002283 // If set is bound to any cmdBuffers, mark them invalid
2284 invalidateBoundCmdBuffers(my_data, pSet);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002285 GENERIC_HEADER* pUpdate = (GENERIC_HEADER*) &pWDS[i];
Tobin Ehlis793ad302015-04-03 12:01:11 -06002286 pLayout = pSet->pLayout;
Tobin Ehlis0174fed2015-05-28 12:10:17 -06002287 // First verify valid update struct
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06002288 if ((skipCall = validUpdateStruct(my_data, device, pUpdate)) == VK_TRUE) {
Tobin Ehlis0174fed2015-05-28 12:10:17 -06002289 break;
2290 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002291 uint32_t binding = 0, endIndex = 0;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002292 binding = pWDS[i].dstBinding;
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002293 // Make sure that layout being updated has the binding being updated
Chia-I Wud46e6ae2015-10-31 00:31:16 +08002294 if (pLayout->bindings.find(binding) == pLayout->bindings.end()) {
Mark Young93ecb1d2016-01-13 13:47:16 -07002295 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)(ds), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
2296 "Descriptor Set %" PRIu64 " does not have binding to match update binding %u for update type %s!", (uint64_t)(ds), binding, string_VkStructureType(pUpdate->sType));
Tobin Ehlis9c536442015-06-19 13:00:59 -06002297 } else {
Tobin Ehlis793ad302015-04-03 12:01:11 -06002298 // Next verify that update falls within size of given binding
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002299 endIndex = getUpdateEndIndex(my_data, device, pLayout, binding, pWDS[i].dstArrayElement, pUpdate);
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002300 if (getBindingEndIndex(pLayout, binding) < endIndex) {
Tobin Ehlis793ad302015-04-03 12:01:11 -06002301 pLayoutCI = &pLayout->createInfo;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06002302 string DSstr = vk_print_vkdescriptorsetlayoutcreateinfo(pLayoutCI, "{DS} ");
Mark Young93ecb1d2016-01-13 13:47:16 -07002303 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)(ds), __LINE__, DRAWSTATE_DESCRIPTOR_UPDATE_OUT_OF_BOUNDS, "DS",
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002304 "Descriptor update type of %s is out of bounds for matching binding %u in Layout w/ CI:\n%s!", string_VkStructureType(pUpdate->sType), binding, DSstr.c_str());
Tobin Ehlis9c536442015-06-19 13:00:59 -06002305 } else { // TODO : should we skip update on a type mismatch or force it?
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002306 uint32_t startIndex;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002307 startIndex = getUpdateStartIndex(my_data, device, pLayout, binding, pWDS[i].dstArrayElement, pUpdate);
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06002308 // Layout bindings match w/ update, now verify that update type & stageFlags are the same for entire update
2309 if ((skipCall = validateUpdateConsistency(my_data, device, pLayout, pUpdate, startIndex, endIndex)) == VK_FALSE) {
2310 // The update is within bounds and consistent, but need to make sure contents make sense as well
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07002311 if ((skipCall = validateUpdateContents(my_data, &pWDS[i], &pLayout->createInfo.pBindings[binding])) == VK_FALSE) {
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002312 // Update is good. Save the update info
2313 // Create new update struct for this set's shadow copy
2314 GENERIC_HEADER* pNewNode = NULL;
2315 skipCall |= shadowUpdateNode(my_data, device, pUpdate, &pNewNode);
2316 if (NULL == pNewNode) {
Mark Young93ecb1d2016-01-13 13:47:16 -07002317 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)(ds), __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS",
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002318 "Out of memory while attempting to allocate UPDATE struct in vkUpdateDescriptors()");
2319 } else {
2320 // Insert shadow node into LL of updates for this set
2321 pNewNode->pNext = pSet->pUpdateStructs;
2322 pSet->pUpdateStructs = pNewNode;
2323 // Now update appropriate descriptor(s) to point to new Update node
Tobin Ehlisa1c28562015-10-23 16:00:08 -06002324 for (uint32_t j = startIndex; j <= endIndex; j++) {
2325 assert(j<pSet->descriptorCount);
2326 pSet->ppDescriptors[j] = pNewNode;
2327 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002328 }
2329 }
2330 }
2331 }
2332 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002333 }
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002334 // Now validate copy updates
Chia-I Wu40cf0ae2015-10-26 17:20:32 +08002335 for (i=0; i < descriptorCopyCount; ++i) {
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002336 SET_NODE *pSrcSet = NULL, *pDstSet = NULL;
2337 LAYOUT_NODE *pSrcLayout = NULL, *pDstLayout = NULL;
2338 uint32_t srcStartIndex = 0, srcEndIndex = 0, dstStartIndex = 0, dstEndIndex = 0;
2339 // For each copy make sure that update falls within given layout and that types match
Chia-I Wue2fc5522015-10-26 20:04:44 +08002340 pSrcSet = my_data->setMap[pCDS[i].srcSet];
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002341 pDstSet = my_data->setMap[pCDS[i].dstSet];
Tobin Ehlis9c4f38d2016-01-14 12:47:19 -07002342 // Set being updated cannot be in-flight
2343 if ((skipCall = validateIdleDescriptorSet(my_data, pDstSet->set, "VkUpdateDescriptorSets")) == VK_TRUE)
2344 return skipCall;
Tobin Ehlise6e574b2016-01-24 23:25:31 -07002345 invalidateBoundCmdBuffers(my_data, pDstSet);
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002346 pSrcLayout = pSrcSet->pLayout;
2347 pDstLayout = pDstSet->pLayout;
2348 // Validate that src binding is valid for src set layout
Chia-I Wud46e6ae2015-10-31 00:31:16 +08002349 if (pSrcLayout->bindings.find(pCDS[i].srcBinding) == pSrcLayout->bindings.end()) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002350 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pSrcSet->set, __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002351 "Copy descriptor update %u has srcBinding %u which is out of bounds for underlying SetLayout %#" PRIxLEAST64 " which only has bindings 0-%u.",
Chia-I Wud50a7d72015-10-26 20:48:51 +08002352 i, pCDS[i].srcBinding, (uint64_t) pSrcLayout->layout, pSrcLayout->createInfo.bindingCount-1);
Chia-I Wud46e6ae2015-10-31 00:31:16 +08002353 } else if (pDstLayout->bindings.find(pCDS[i].dstBinding) == pDstLayout->bindings.end()) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002354 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pDstSet->set, __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002355 "Copy descriptor update %u has dstBinding %u which is out of bounds for underlying SetLayout %#" PRIxLEAST64 " which only has bindings 0-%u.",
2356 i, pCDS[i].dstBinding, (uint64_t) pDstLayout->layout, pDstLayout->createInfo.bindingCount-1);
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002357 } else {
2358 // Proceed with validation. Bindings are ok, but make sure update is within bounds of given layout
2359 srcEndIndex = getUpdateEndIndex(my_data, device, pSrcLayout, pCDS[i].srcBinding, pCDS[i].srcArrayElement, (const GENERIC_HEADER*)&(pCDS[i]));
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002360 dstEndIndex = getUpdateEndIndex(my_data, device, pDstLayout, pCDS[i].dstBinding, pCDS[i].dstArrayElement, (const GENERIC_HEADER*)&(pCDS[i]));
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002361 if (getBindingEndIndex(pSrcLayout, pCDS[i].srcBinding) < srcEndIndex) {
2362 pLayoutCI = &pSrcLayout->createInfo;
2363 string DSstr = vk_print_vkdescriptorsetlayoutcreateinfo(pLayoutCI, "{DS} ");
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002364 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pSrcSet->set, __LINE__, DRAWSTATE_DESCRIPTOR_UPDATE_OUT_OF_BOUNDS, "DS",
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002365 "Copy descriptor src update is out of bounds for matching binding %u in Layout w/ CI:\n%s!", pCDS[i].srcBinding, DSstr.c_str());
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002366 } else if (getBindingEndIndex(pDstLayout, pCDS[i].dstBinding) < dstEndIndex) {
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002367 pLayoutCI = &pDstLayout->createInfo;
2368 string DSstr = vk_print_vkdescriptorsetlayoutcreateinfo(pLayoutCI, "{DS} ");
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002369 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pDstSet->set, __LINE__, DRAWSTATE_DESCRIPTOR_UPDATE_OUT_OF_BOUNDS, "DS",
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002370 "Copy descriptor dest update is out of bounds for matching binding %u in Layout w/ CI:\n%s!", pCDS[i].dstBinding, DSstr.c_str());
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002371 } else {
2372 srcStartIndex = getUpdateStartIndex(my_data, device, pSrcLayout, pCDS[i].srcBinding, pCDS[i].srcArrayElement, (const GENERIC_HEADER*)&(pCDS[i]));
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002373 dstStartIndex = getUpdateStartIndex(my_data, device, pDstLayout, pCDS[i].dstBinding, pCDS[i].dstArrayElement, (const GENERIC_HEADER*)&(pCDS[i]));
Chia-I Wud50a7d72015-10-26 20:48:51 +08002374 for (uint32_t j=0; j<pCDS[i].descriptorCount; ++j) {
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002375 // For copy just make sure that the types match and then perform the update
2376 if (pSrcLayout->descriptorTypes[srcStartIndex+j] != pDstLayout->descriptorTypes[dstStartIndex+j]) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002377 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_DESCRIPTOR_TYPE_MISMATCH, "DS",
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002378 "Copy descriptor update index %u, update count #%u, has src update descriptor type %s that does not match overlapping dest descriptor type of %s!",
2379 i, j+1, string_VkDescriptorType(pSrcLayout->descriptorTypes[srcStartIndex+j]), string_VkDescriptorType(pDstLayout->descriptorTypes[dstStartIndex+j]));
2380 } else {
2381 // point dst descriptor at corresponding src descriptor
Tobin Ehlisf6585052015-12-17 11:48:42 -07002382 // TODO : This may be a hole. I believe copy should be its own copy,
2383 // otherwise a subsequent write update to src will incorrectly affect the copy
Tobin Ehlis8a62e632015-10-27 15:43:38 -06002384 pDstSet->ppDescriptors[j+dstStartIndex] = pSrcSet->ppDescriptors[j+srcStartIndex];
2385 }
2386 }
2387 }
2388 }
2389 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002390 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002391 return skipCall;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002392}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002393
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06002394// Verify that given pool has descriptors that are being requested for allocation
Mark Lobodzinski39298632015-11-18 08:38:27 -07002395static VkBool32 validate_descriptor_availability_in_pool(layer_data* dev_data, DESCRIPTOR_POOL_NODE* pPoolNode, uint32_t count, const VkDescriptorSetLayout* pSetLayouts)
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06002396{
2397 VkBool32 skipCall = VK_FALSE;
2398 uint32_t i = 0, j = 0;
2399 for (i=0; i<count; ++i) {
2400 LAYOUT_NODE* pLayout = getLayoutNode(dev_data, pSetLayouts[i]);
2401 if (NULL == pLayout) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002402 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, (uint64_t) pSetLayouts[i], __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002403 "Unable to find set layout node for layout %#" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call", (uint64_t) pSetLayouts[i]);
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06002404 } else {
Chia-I Wu1b99bb22015-10-27 19:25:11 +08002405 uint32_t typeIndex = 0, poolSizeCount = 0;
Chia-I Wud50a7d72015-10-26 20:48:51 +08002406 for (j=0; j<pLayout->createInfo.bindingCount; ++j) {
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07002407 typeIndex = static_cast<uint32_t>(pLayout->createInfo.pBindings[j].descriptorType);
2408 poolSizeCount = pLayout->createInfo.pBindings[j].descriptorCount;
Chia-I Wu1b99bb22015-10-27 19:25:11 +08002409 if (poolSizeCount > pPoolNode->availableDescriptorTypeCount[typeIndex]) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002410 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, (uint64_t) pLayout->layout, __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY, "DS",
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06002411 "Unable to allocate %u descriptors of type %s from pool %#" PRIxLEAST64 ". This pool only has %u descriptors of this type remaining.",
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07002412 poolSizeCount, string_VkDescriptorType(pLayout->createInfo.pBindings[j].descriptorType), (uint64_t) pPoolNode->pool, pPoolNode->availableDescriptorTypeCount[typeIndex]);
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06002413 } else { // Decrement available descriptors of this type
Chia-I Wu1b99bb22015-10-27 19:25:11 +08002414 pPoolNode->availableDescriptorTypeCount[typeIndex] -= poolSizeCount;
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06002415 }
2416 }
2417 }
2418 }
2419 return skipCall;
2420}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002421
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002422// Free the shadowed update node for this Set
2423// NOTE : Calls to this function should be wrapped in mutex
2424static void freeShadowUpdateTree(SET_NODE* pSet)
2425{
2426 GENERIC_HEADER* pShadowUpdate = pSet->pUpdateStructs;
2427 pSet->pUpdateStructs = NULL;
2428 GENERIC_HEADER* pFreeUpdate = pShadowUpdate;
2429 // Clear the descriptor mappings as they will now be invalid
2430 memset(pSet->ppDescriptors, 0, pSet->descriptorCount*sizeof(GENERIC_HEADER*));
2431 while(pShadowUpdate) {
2432 pFreeUpdate = pShadowUpdate;
2433 pShadowUpdate = (GENERIC_HEADER*)pShadowUpdate->pNext;
2434 uint32_t index = 0;
Chia-I Wu9d00ed72015-05-25 16:27:55 +08002435 VkWriteDescriptorSet * pWDS = NULL;
2436 VkCopyDescriptorSet * pCDS = NULL;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002437 void** ppToFree = NULL;
2438 switch (pFreeUpdate->sType)
2439 {
Chia-I Wu9d00ed72015-05-25 16:27:55 +08002440 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
2441 pWDS = (VkWriteDescriptorSet*)pFreeUpdate;
Tony Barbourb1947942015-11-02 11:46:29 -07002442 switch (pWDS->descriptorType) {
2443 case VK_DESCRIPTOR_TYPE_SAMPLER:
2444 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
2445 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2446 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
2447 {
2448 delete[] pWDS->pImageInfo;
2449 }
2450 break;
2451 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2452 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2453 {
2454 delete[] pWDS->pTexelBufferView;
2455 }
2456 break;
2457 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2458 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2459 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2460 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
2461 {
2462 delete[] pWDS->pBufferInfo;
2463 }
2464 break;
2465 default:
2466 break;
Courtney Goeltzenleuchteraa132e72015-10-22 15:31:56 -06002467 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002468 break;
Chia-I Wu9d00ed72015-05-25 16:27:55 +08002469 case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002470 break;
2471 default:
2472 assert(0);
2473 break;
2474 }
Tobin Ehlisecc6bd02015-04-08 10:58:37 -06002475 delete pFreeUpdate;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002476 }
2477}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002478
Tobin Ehlis793ad302015-04-03 12:01:11 -06002479// Free all DS Pools including their Sets & related sub-structs
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002480// NOTE : Calls to this function should be wrapped in mutex
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002481static void deletePools(layer_data* my_data)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002482{
Mark Lobodzinski39298632015-11-18 08:38:27 -07002483 if (my_data->descriptorPoolMap.size() <= 0)
David Pinedod8f83d82015-04-27 16:36:17 -06002484 return;
Mark Lobodzinski39298632015-11-18 08:38:27 -07002485 for (auto ii=my_data->descriptorPoolMap.begin(); ii!=my_data->descriptorPoolMap.end(); ++ii) {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002486 SET_NODE* pSet = (*ii).second->pSets;
2487 SET_NODE* pFreeSet = pSet;
2488 while (pSet) {
2489 pFreeSet = pSet;
2490 pSet = pSet->pNext;
Tobin Ehlisecc6bd02015-04-08 10:58:37 -06002491 // Freeing layouts handled in deleteLayouts() function
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002492 // Free Update shadow struct tree
2493 freeShadowUpdateTree(pFreeSet);
2494 if (pFreeSet->ppDescriptors) {
Chris Forbes02038792015-06-04 10:49:27 +12002495 delete[] pFreeSet->ppDescriptors;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002496 }
2497 delete pFreeSet;
2498 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002499 delete (*ii).second;
2500 }
Mark Lobodzinski39298632015-11-18 08:38:27 -07002501 my_data->descriptorPoolMap.clear();
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002502}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002503
Tobin Ehlisecc6bd02015-04-08 10:58:37 -06002504// WARN : Once deleteLayouts() called, any layout ptrs in Pool/Set data structure will be invalid
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002505// NOTE : Calls to this function should be wrapped in mutex
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002506static void deleteLayouts(layer_data* my_data)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002507{
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07002508 if (my_data->descriptorSetLayoutMap.size() <= 0)
David Pinedod8f83d82015-04-27 16:36:17 -06002509 return;
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07002510 for (auto ii=my_data->descriptorSetLayoutMap.begin(); ii!=my_data->descriptorSetLayoutMap.end(); ++ii) {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002511 LAYOUT_NODE* pLayout = (*ii).second;
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07002512 if (pLayout->createInfo.pBindings) {
Chia-I Wud50a7d72015-10-26 20:48:51 +08002513 for (uint32_t i=0; i<pLayout->createInfo.bindingCount; i++) {
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07002514 if (pLayout->createInfo.pBindings[i].pImmutableSamplers)
2515 delete[] pLayout->createInfo.pBindings[i].pImmutableSamplers;
Tobin Ehlisecc6bd02015-04-08 10:58:37 -06002516 }
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07002517 delete[] pLayout->createInfo.pBindings;
Tobin Ehlisecc6bd02015-04-08 10:58:37 -06002518 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002519 delete pLayout;
2520 }
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07002521 my_data->descriptorSetLayoutMap.clear();
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002522}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002523
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002524// Currently clearing a set is removing all previous updates to that set
2525// TODO : Validate if this is correct clearing behavior
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002526static void clearDescriptorSet(layer_data* my_data, VkDescriptorSet set)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002527{
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002528 SET_NODE* pSet = getSetNode(my_data, set);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002529 if (!pSet) {
2530 // TODO : Return error
Tobin Ehlisce132d82015-06-19 15:07:05 -06002531 } else {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002532 loader_platform_thread_lock_mutex(&globalLock);
2533 freeShadowUpdateTree(pSet);
2534 loader_platform_thread_unlock_mutex(&globalLock);
2535 }
2536}
2537
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -06002538static void clearDescriptorPool(layer_data* my_data, const VkDevice device, const VkDescriptorPool pool, VkDescriptorPoolResetFlags flags)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002539{
Mark Lobodzinski39298632015-11-18 08:38:27 -07002540 DESCRIPTOR_POOL_NODE* pPool = getPoolNode(my_data, pool);
Tobin Ehlis793ad302015-04-03 12:01:11 -06002541 if (!pPool) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002542 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT, (uint64_t) pool, __LINE__, DRAWSTATE_INVALID_POOL, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002543 "Unable to find pool node for pool %#" PRIxLEAST64 " specified in vkResetDescriptorPool() call", (uint64_t) pool);
Tobin Ehlisce132d82015-06-19 15:07:05 -06002544 } else {
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -06002545 // TODO: validate flags
Tobin Ehlis793ad302015-04-03 12:01:11 -06002546 // For every set off of this pool, clear it
2547 SET_NODE* pSet = pPool->pSets;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002548 while (pSet) {
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002549 clearDescriptorSet(my_data, pSet->set);
Mark Lobodzinskidcce0792016-01-04 09:40:19 -07002550 pSet = pSet->pNext;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002551 }
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06002552 // Reset available count to max count for this pool
2553 for (uint32_t i=0; i<pPool->availableDescriptorTypeCount.size(); ++i) {
2554 pPool->availableDescriptorTypeCount[i] = pPool->maxDescriptorTypeCount[i];
2555 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002556 }
2557}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002558
Tobin Ehlisa16e8922015-06-16 15:50:44 -06002559// For given CB object, fetch associated CB Node from map
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002560static GLOBAL_CB_NODE* getCBNode(layer_data* my_data, const VkCommandBuffer cb)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002561{
2562 loader_platform_thread_lock_mutex(&globalLock);
Mark Lobodzinskib8df78c2015-11-20 14:33:48 -07002563 if (my_data->commandBufferMap.count(cb) == 0) {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002564 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06002565 // TODO : How to pass cb as srcObj here?
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002566 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
Mark Youngee3f3a22016-01-25 12:18:32 -07002567 "Attempt to use CommandBuffer %#" PRIxLEAST64 " that doesn't exist!", (uint64_t)(cb));
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002568 return NULL;
2569 }
2570 loader_platform_thread_unlock_mutex(&globalLock);
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002571 return my_data->commandBufferMap[cb];
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002572}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002573
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002574// Free all CB Nodes
2575// NOTE : Calls to this function should be wrapped in mutex
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002576static void deleteCommandBuffers(layer_data* my_data)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002577{
Mark Lobodzinskib8df78c2015-11-20 14:33:48 -07002578 if (my_data->commandBufferMap.size() <= 0) {
David Pinedod8f83d82015-04-27 16:36:17 -06002579 return;
Mark Lobodzinskib8df78c2015-11-20 14:33:48 -07002580 }
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002581 for (auto ii=my_data->commandBufferMap.begin(); ii!=my_data->commandBufferMap.end(); ++ii) {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002582 delete (*ii).second;
2583 }
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002584 my_data->commandBufferMap.clear();
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002585}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002586
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002587static VkBool32 report_error_no_cb_begin(const layer_data* dev_data, const VkCommandBuffer cb, const char* caller_name)
Tobin Ehlis9c536442015-06-19 13:00:59 -06002588{
Tobin Ehlis61b36f32015-12-16 08:19:42 -07002589 return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002590 (uint64_t)cb, __LINE__, DRAWSTATE_NO_BEGIN_COMMAND_BUFFER, "DS",
Tobin Ehlis61b36f32015-12-16 08:19:42 -07002591 "You must call vkBeginCommandBuffer() before this call to %s", caller_name);
Tobin Ehlis9c536442015-06-19 13:00:59 -06002592}
Michael Lentine3dea6512015-10-28 15:55:18 -07002593
Mark Youngb20a6a82016-01-07 15:41:43 -07002594VkBool32 validateCmdsInCmdBuffer(const layer_data* dev_data, const GLOBAL_CB_NODE* pCB, const CMD_TYPE cmd_type) {
2595 if (!pCB->activeRenderPass) return VK_FALSE;
2596 VkBool32 skip_call = VK_FALSE;
Michael Lentine2e068b22015-12-29 16:05:27 -06002597 if (pCB->activeSubpassContents == VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS && cmd_type != CMD_EXECUTECOMMANDS) {
2598 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
2599 DRAWSTATE_INVALID_COMMAND_BUFFER, "DS", "Commands cannot be called in a subpass using secondary command buffers.");
2600 } else if (pCB->activeSubpassContents == VK_SUBPASS_CONTENTS_INLINE && cmd_type == CMD_EXECUTECOMMANDS) {
2601 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
2602 DRAWSTATE_INVALID_COMMAND_BUFFER, "DS", "vkCmdExecuteCommands() cannot be called in a subpass using inline commands.");
2603 }
Michael Lentine3dea6512015-10-28 15:55:18 -07002604 return skip_call;
2605}
2606
Tobin Ehlis61b36f32015-12-16 08:19:42 -07002607// Add specified CMD to the CmdBuffer in given pCB, flagging errors if CB is not
2608// in the recording state or if there's an issue with the Cmd ordering
2609static VkBool32 addCmd(const layer_data* my_data, GLOBAL_CB_NODE* pCB, const CMD_TYPE cmd, const char* caller_name)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002610{
Tobin Ehlis61b36f32015-12-16 08:19:42 -07002611 VkBool32 skipCall = VK_FALSE;
2612 if (pCB->state != CB_RECORDING) {
2613 skipCall |= report_error_no_cb_begin(my_data, pCB->commandBuffer, caller_name);
2614 skipCall |= validateCmdsInCmdBuffer(my_data, pCB, cmd);
Tobin Ehlis9a874302016-01-20 10:25:29 -07002615 CMD_NODE cmdNode = {};
2616 // init cmd node and append to end of cmd LL
2617 cmdNode.cmdNumber = ++pCB->numCmds;
2618 cmdNode.type = cmd;
2619 pCB->cmds.push_back(cmdNode);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002620 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002621 return skipCall;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002622}
Tobin Ehlisef694652016-01-19 12:03:34 -07002623// Reset the command buffer state
2624// Maintain the createInfo and set state to CB_NEW, but clear all other state
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002625static void resetCB(layer_data* my_data, const VkCommandBuffer cb)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002626{
Tobin Ehlis74714d22016-01-25 15:24:34 -08002627 GLOBAL_CB_NODE* pCB = my_data->commandBufferMap[cb];
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002628 if (pCB) {
Tobin Ehlis9a874302016-01-20 10:25:29 -07002629 pCB->cmds.clear();
Tobin Ehlisef694652016-01-19 12:03:34 -07002630 // Reset CB state (note that createInfo is not cleared)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002631 pCB->commandBuffer = cb;
Michael Lentineabc5e922015-10-12 11:30:14 -05002632 memset(&pCB->beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
Tobin Ehliscd5bfd82016-01-19 13:12:52 -07002633 memset(&pCB->inheritanceInfo, 0, sizeof(VkCommandBufferInheritanceInfo));
Michael Lentineabc5e922015-10-12 11:30:14 -05002634 pCB->fence = 0;
2635 pCB->numCmds = 0;
2636 memset(pCB->drawCount, 0, NUM_DRAW_TYPES * sizeof(uint64_t));
2637 pCB->state = CB_NEW;
2638 pCB->submitCount = 0;
2639 pCB->status = 0;
Michael Lentineabc5e922015-10-12 11:30:14 -05002640 pCB->lastBoundPipeline = 0;
2641 pCB->viewports.clear();
2642 pCB->scissors.clear();
2643 pCB->lineWidth = 0;
2644 pCB->depthBiasConstantFactor = 0;
2645 pCB->depthBiasClamp = 0;
2646 pCB->depthBiasSlopeFactor = 0;
2647 memset(pCB->blendConstants, 0, 4 * sizeof(float));
2648 pCB->minDepthBounds = 0;
2649 pCB->maxDepthBounds = 0;
2650 memset(&pCB->front, 0, sizeof(stencil_data));
2651 memset(&pCB->back, 0, sizeof(stencil_data));
2652 pCB->lastBoundDescriptorSet = 0;
2653 pCB->lastBoundPipelineLayout = 0;
2654 pCB->activeRenderPass = 0;
2655 pCB->activeSubpass = 0;
Tobin Ehlise6e574b2016-01-24 23:25:31 -07002656 // Before clearing uniqueBoundSets, remove this CB off of its boundCBs
2657 for (auto set : pCB->uniqueBoundSets) {
2658 auto set_node = my_data->setMap.find(set);
2659 if (set_node != my_data->setMap.end()) {
2660 set_node->second->boundCmdBuffers.erase(pCB->commandBuffer);
2661 }
2662 }
2663 pCB->uniqueBoundSets.clear();
Michael Lentineabc5e922015-10-12 11:30:14 -05002664 pCB->framebuffer = 0;
Michael Lentineabc5e922015-10-12 11:30:14 -05002665 pCB->boundDescriptorSets.clear();
Michael Lentine700b0aa2015-10-30 17:57:32 -07002666 pCB->drawData.clear();
2667 pCB->currentDrawData.buffers.clear();
Michael Lentineabc5e922015-10-12 11:30:14 -05002668 pCB->imageLayoutMap.clear();
Michael Lentineb887b0a2015-12-29 14:12:11 -06002669 pCB->waitedEvents.clear();
2670 pCB->waitedEventsBeforeQueryReset.clear();
2671 pCB->queryToStateMap.clear();
Tobin Ehlisf4aafc02016-01-15 13:34:44 -07002672 pCB->secondaryCommandBuffers.clear();
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002673 }
2674}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002675
Tobin Ehlis963a4042015-09-29 08:18:34 -06002676// Set PSO-related status bits for CB, including dynamic state set via PSO
Tobin Ehlise382c5a2015-06-10 12:57:07 -06002677static void set_cb_pso_status(GLOBAL_CB_NODE* pCB, const PIPELINE_NODE* pPipe)
2678{
2679 for (uint32_t i = 0; i < pPipe->cbStateCI.attachmentCount; i++) {
Chia-I Wu1b99bb22015-10-27 19:25:11 +08002680 if (0 != pPipe->pAttachments[i].colorWriteMask) {
Tobin Ehlise382c5a2015-06-10 12:57:07 -06002681 pCB->status |= CBSTATUS_COLOR_BLEND_WRITE_ENABLE;
2682 }
2683 }
2684 if (pPipe->dsStateCI.depthWriteEnable) {
Cody Northrop82485a82015-08-18 15:21:16 -06002685 pCB->status |= CBSTATUS_DEPTH_WRITE_ENABLE;
2686 }
Cody Northrop82485a82015-08-18 15:21:16 -06002687 if (pPipe->dsStateCI.stencilTestEnable) {
2688 pCB->status |= CBSTATUS_STENCIL_TEST_ENABLE;
Tobin Ehlise382c5a2015-06-10 12:57:07 -06002689 }
Tobin Ehlisd332f282015-10-02 11:00:56 -06002690 // Account for any dynamic state not set via this PSO
2691 if (!pPipe->dynStateCI.dynamicStateCount) { // All state is static
2692 pCB->status = CBSTATUS_ALL;
2693 } else {
Tobin Ehlis5e5a1e92015-10-01 09:24:40 -06002694 // First consider all state on
2695 // Then unset any state that's noted as dynamic in PSO
2696 // Finally OR that into CB statemask
2697 CBStatusFlags psoDynStateMask = CBSTATUS_ALL;
Tobin Ehlis963a4042015-09-29 08:18:34 -06002698 for (uint32_t i=0; i < pPipe->dynStateCI.dynamicStateCount; i++) {
2699 switch (pPipe->dynStateCI.pDynamicStates[i]) {
2700 case VK_DYNAMIC_STATE_VIEWPORT:
Tobin Ehlis5e5a1e92015-10-01 09:24:40 -06002701 psoDynStateMask &= ~CBSTATUS_VIEWPORT_SET;
Tobin Ehlis963a4042015-09-29 08:18:34 -06002702 break;
2703 case VK_DYNAMIC_STATE_SCISSOR:
Tobin Ehlis5e5a1e92015-10-01 09:24:40 -06002704 psoDynStateMask &= ~CBSTATUS_SCISSOR_SET;
Tobin Ehlis963a4042015-09-29 08:18:34 -06002705 break;
2706 case VK_DYNAMIC_STATE_LINE_WIDTH:
Tobin Ehlis5e5a1e92015-10-01 09:24:40 -06002707 psoDynStateMask &= ~CBSTATUS_LINE_WIDTH_SET;
Tobin Ehlis963a4042015-09-29 08:18:34 -06002708 break;
2709 case VK_DYNAMIC_STATE_DEPTH_BIAS:
Tobin Ehlis5e5a1e92015-10-01 09:24:40 -06002710 psoDynStateMask &= ~CBSTATUS_DEPTH_BIAS_SET;
Tobin Ehlis963a4042015-09-29 08:18:34 -06002711 break;
2712 case VK_DYNAMIC_STATE_BLEND_CONSTANTS:
Tobin Ehlis5e5a1e92015-10-01 09:24:40 -06002713 psoDynStateMask &= ~CBSTATUS_BLEND_SET;
Tobin Ehlis963a4042015-09-29 08:18:34 -06002714 break;
2715 case VK_DYNAMIC_STATE_DEPTH_BOUNDS:
Tobin Ehlis5e5a1e92015-10-01 09:24:40 -06002716 psoDynStateMask &= ~CBSTATUS_DEPTH_BOUNDS_SET;
Tobin Ehlis963a4042015-09-29 08:18:34 -06002717 break;
2718 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:
Tobin Ehlis5e5a1e92015-10-01 09:24:40 -06002719 psoDynStateMask &= ~CBSTATUS_STENCIL_READ_MASK_SET;
Tobin Ehlis963a4042015-09-29 08:18:34 -06002720 break;
2721 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:
Tobin Ehlis5e5a1e92015-10-01 09:24:40 -06002722 psoDynStateMask &= ~CBSTATUS_STENCIL_WRITE_MASK_SET;
Tobin Ehlis963a4042015-09-29 08:18:34 -06002723 break;
2724 case VK_DYNAMIC_STATE_STENCIL_REFERENCE:
Tobin Ehlis5e5a1e92015-10-01 09:24:40 -06002725 psoDynStateMask &= ~CBSTATUS_STENCIL_REFERENCE_SET;
Tobin Ehlis963a4042015-09-29 08:18:34 -06002726 break;
2727 default:
2728 // TODO : Flag error here
2729 break;
2730 }
2731 }
Tobin Ehlis5e5a1e92015-10-01 09:24:40 -06002732 pCB->status |= psoDynStateMask;
Tobin Ehlis963a4042015-09-29 08:18:34 -06002733 }
Tobin Ehlise382c5a2015-06-10 12:57:07 -06002734}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002735
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002736// Print the last bound Gfx Pipeline
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002737static VkBool32 printPipeline(layer_data* my_data, const VkCommandBuffer cb)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002738{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002739 VkBool32 skipCall = VK_FALSE;
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002740 GLOBAL_CB_NODE* pCB = getCBNode(my_data, cb);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002741 if (pCB) {
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002742 PIPELINE_NODE *pPipeTrav = getPipeline(my_data, pCB->lastBoundPipeline);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002743 if (!pPipeTrav) {
2744 // nothing to print
Tobin Ehlisce132d82015-06-19 15:07:05 -06002745 } else {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002746 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_NONE, "DS",
Michael Lentine010f4692015-11-03 16:19:46 -08002747 "%s", vk_print_vkgraphicspipelinecreateinfo(&pPipeTrav->graphicsPipelineCI, "{DS}").c_str());
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002748 }
2749 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002750 return skipCall;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002751}
Tobin Ehlis559c6382015-11-05 09:52:49 -07002752
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002753// Print details of DS config to stdout
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002754static VkBool32 printDSConfig(layer_data* my_data, const VkCommandBuffer cb)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002755{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002756 VkBool32 skipCall = VK_FALSE;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002757 char ds_config_str[1024*256] = {0}; // TODO : Currently making this buffer HUGE w/o overrun protection. Need to be smarter, start smaller, and grow as needed.
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002758 GLOBAL_CB_NODE* pCB = getCBNode(my_data, cb);
Tobin Ehlis93f89e82015-06-09 08:39:32 -06002759 if (pCB && pCB->lastBoundDescriptorSet) {
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002760 SET_NODE* pSet = getSetNode(my_data, pCB->lastBoundDescriptorSet);
Mark Lobodzinski39298632015-11-18 08:38:27 -07002761 DESCRIPTOR_POOL_NODE* pPool = getPoolNode(my_data, pSet->pool);
Tobin Ehlis793ad302015-04-03 12:01:11 -06002762 // Print out pool details
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002763 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_NONE, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002764 "Details for pool %#" PRIxLEAST64 ".", (uint64_t) pPool->pool);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06002765 string poolStr = vk_print_vkdescriptorpoolcreateinfo(&pPool->createInfo, " ");
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002766 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_NONE, "DS",
Tobin Ehlisa16e8922015-06-16 15:50:44 -06002767 "%s", poolStr.c_str());
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002768 // Print out set details
2769 char prefix[10];
2770 uint32_t index = 0;
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002771 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_NONE, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002772 "Details for descriptor set %#" PRIxLEAST64 ".", (uint64_t) pSet->set);
Tobin Ehlis793ad302015-04-03 12:01:11 -06002773 LAYOUT_NODE* pLayout = pSet->pLayout;
2774 // Print layout details
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002775 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_NONE, "DS",
Mark Young93ecb1d2016-01-13 13:47:16 -07002776 "Layout #%u, (object %#" PRIxLEAST64 ") for DS %#" PRIxLEAST64 ".", index+1, (uint64_t)(pLayout->layout), (uint64_t)(pSet->set));
Tobin Ehlis793ad302015-04-03 12:01:11 -06002777 sprintf(prefix, " [L%u] ", index);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06002778 string DSLstr = vk_print_vkdescriptorsetlayoutcreateinfo(&pLayout->createInfo, prefix).c_str();
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002779 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_NONE, "DS",
Tobin Ehlisa16e8922015-06-16 15:50:44 -06002780 "%s", DSLstr.c_str());
Tobin Ehlis793ad302015-04-03 12:01:11 -06002781 index++;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002782 GENERIC_HEADER* pUpdate = pSet->pUpdateStructs;
2783 if (pUpdate) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002784 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_NONE, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002785 "Update Chain [UC] for descriptor set %#" PRIxLEAST64 ":", (uint64_t) pSet->set);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002786 sprintf(prefix, " [UC] ");
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002787 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_NONE, "DS",
Michael Lentine010f4692015-11-03 16:19:46 -08002788 "%s", dynamic_display(pUpdate, prefix).c_str());
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002789 // TODO : If there is a "view" associated with this update, print CI for that view
Tobin Ehlisce132d82015-06-19 15:07:05 -06002790 } else {
Tobin Ehlisbf081f32015-06-15 08:41:17 -06002791 if (0 != pSet->descriptorCount) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002792 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_NONE, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002793 "No Update Chain for descriptor set %#" PRIxLEAST64 " which has %u descriptors (vkUpdateDescriptors has not been called)", (uint64_t) pSet->set, pSet->descriptorCount);
Tobin Ehlisce132d82015-06-19 15:07:05 -06002794 } else {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002795 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_NONE, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002796 "FYI: No descriptors in descriptor set %#" PRIxLEAST64 ".", (uint64_t) pSet->set);
Tobin Ehlisbf081f32015-06-15 08:41:17 -06002797 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002798 }
2799 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002800 return skipCall;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002801}
2802
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002803static void printCB(layer_data* my_data, const VkCommandBuffer cb)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002804{
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002805 GLOBAL_CB_NODE* pCB = getCBNode(my_data, cb);
Tobin Ehlis9a874302016-01-20 10:25:29 -07002806 if (pCB && pCB->cmds.size() > 0) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002807 log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_NONE, "DS",
Tobin Ehlisa16e8922015-06-16 15:50:44 -06002808 "Cmds in CB %p", (void*)cb);
Tobin Ehlis9a874302016-01-20 10:25:29 -07002809 vector<CMD_NODE> cmds = pCB->cmds;
2810 for (auto ii=cmds.begin(); ii!=cmds.end(); ++ii) {
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06002811 // TODO : Need to pass cb as srcObj here
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002812 log_msg(my_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_NONE, "DS",
Tobin Ehlis9a874302016-01-20 10:25:29 -07002813 " CMD#%" PRIu64 ": %s", (*ii).cmdNumber, cmdTypeToString((*ii).type).c_str());
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002814 }
Tobin Ehlisce132d82015-06-19 15:07:05 -06002815 } else {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002816 // Nothing to print
2817 }
2818}
2819
Chia-I Wu3432a0c2015-10-27 18:04:07 +08002820static VkBool32 synchAndPrintDSConfig(layer_data* my_data, const VkCommandBuffer cb)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002821{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002822 VkBool32 skipCall = VK_FALSE;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07002823 if (!(my_data->report_data->active_flags & VK_DEBUG_REPORT_INFO_BIT_EXT)) {
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002824 return skipCall;
Mike Stroyanba35e352015-08-12 17:11:28 -06002825 }
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002826 skipCall |= printDSConfig(my_data, cb);
2827 skipCall |= printPipeline(my_data, cb);
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06002828 return skipCall;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002829}
2830
Mark Lobodzinski5495d132015-09-30 16:19:16 -06002831// Flags validation error if the associated call is made inside a render pass. The apiName
2832// routine should ONLY be called outside a render pass.
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06002833static VkBool32 insideRenderPass(const layer_data* my_data, GLOBAL_CB_NODE *pCB, const char *apiName)
Mark Lobodzinski5495d132015-09-30 16:19:16 -06002834{
2835 VkBool32 inside = VK_FALSE;
2836 if (pCB->activeRenderPass) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07002837 inside = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002838 (uint64_t)pCB->commandBuffer, __LINE__, DRAWSTATE_INVALID_RENDERPASS_CMD, "DS",
Mark Lobodzinski5495d132015-09-30 16:19:16 -06002839 "%s: It is invalid to issue this call inside an active render pass (%#" PRIxLEAST64 ")",
Chia-I Wue2fc5522015-10-26 20:04:44 +08002840 apiName, (uint64_t) pCB->activeRenderPass);
Mark Lobodzinski5495d132015-09-30 16:19:16 -06002841 }
2842 return inside;
2843}
2844
2845// Flags validation error if the associated call is made outside a render pass. The apiName
2846// routine should ONLY be called inside a render pass.
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06002847static VkBool32 outsideRenderPass(const layer_data* my_data, GLOBAL_CB_NODE *pCB, const char *apiName)
Mark Lobodzinski5495d132015-09-30 16:19:16 -06002848{
2849 VkBool32 outside = VK_FALSE;
Mark Lobodzinski74635932015-12-18 15:35:38 -07002850 if (((pCB->createInfo.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) &&
2851 (!pCB->activeRenderPass)) ||
2852 ((pCB->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) &&
2853 (!pCB->activeRenderPass) &&
2854 !(pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT))) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07002855 outside = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07002856 (uint64_t)pCB->commandBuffer, __LINE__, DRAWSTATE_NO_ACTIVE_RENDERPASS, "DS",
Mark Lobodzinski5495d132015-09-30 16:19:16 -06002857 "%s: This call must be issued inside an active render pass.", apiName);
2858 }
2859 return outside;
2860}
2861
Courtney Goeltzenleuchter6d8e8182015-11-25 14:31:49 -07002862static void init_draw_state(layer_data *my_data, const VkAllocationCallbacks *pAllocator)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002863{
Tobin Ehlisa16e8922015-06-16 15:50:44 -06002864 uint32_t report_flags = 0;
2865 uint32_t debug_action = 0;
2866 FILE *log_output = NULL;
2867 const char *option_str;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07002868 VkDebugReportCallbackEXT callback;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002869 // initialize DrawState options
Tobin Ehlisa16e8922015-06-16 15:50:44 -06002870 report_flags = getLayerOptionFlags("DrawStateReportFlags", 0);
2871 getLayerOptionEnum("DrawStateDebugAction", (uint32_t *) &debug_action);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002872
Tobin Ehlisa16e8922015-06-16 15:50:44 -06002873 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002874 {
Tobin Ehlisa16e8922015-06-16 15:50:44 -06002875 option_str = getLayerOption("DrawStateLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -06002876 log_output = getLayerLogOutput(option_str, "DrawState");
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07002877 VkDebugReportCallbackCreateInfoEXT dbgInfo;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -07002878 memset(&dbgInfo, 0, sizeof(dbgInfo));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07002879 dbgInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -07002880 dbgInfo.pfnCallback = log_callback;
2881 dbgInfo.pUserData = log_output;
2882 dbgInfo.flags = report_flags;
2883 layer_create_msg_callback(my_data->report_data, &dbgInfo, pAllocator, &callback);
Courtney Goeltzenleuchter62d945a2015-10-05 15:58:38 -06002884 my_data->logging_callback.push_back(callback);
2885 }
2886
2887 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07002888 VkDebugReportCallbackCreateInfoEXT dbgInfo;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -07002889 memset(&dbgInfo, 0, sizeof(dbgInfo));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07002890 dbgInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -07002891 dbgInfo.pfnCallback = win32_debug_output_msg;
2892 dbgInfo.pUserData = log_output;
2893 dbgInfo.flags = report_flags;
2894 layer_create_msg_callback(my_data->report_data, &dbgInfo, pAllocator, &callback);
Courtney Goeltzenleuchter62d945a2015-10-05 15:58:38 -06002895 my_data->logging_callback.push_back(callback);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002896 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002897
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002898 if (!globalLockInitialized)
2899 {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002900 loader_platform_thread_create_mutex(&globalLock);
2901 globalLockInitialized = 1;
2902 }
2903}
2904
Chia-I Wu9ab61502015-11-06 06:42:02 +08002905VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance)
Courtney Goeltzenleuchterabc035e2015-06-01 14:29:58 -06002906{
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07002907 VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
Courtney Goeltzenleuchterabc035e2015-06-01 14:29:58 -06002908
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07002909 assert(chain_info->u.pLayerInfo);
2910 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
2911 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance) fpGetInstanceProcAddr(NULL, "vkCreateInstance");
2912 if (fpCreateInstance == NULL) {
2913 return VK_ERROR_INITIALIZATION_FAILED;
Courtney Goeltzenleuchterabc035e2015-06-01 14:29:58 -06002914 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07002915
2916 // Advance the link info for the next element on the chain
2917 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
2918
2919 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
2920 if (result != VK_SUCCESS)
2921 return result;
2922
2923 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
2924 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
2925 layer_init_instance_dispatch_table(*pInstance, my_data->instance_dispatch_table, fpGetInstanceProcAddr);
2926
2927 my_data->report_data = debug_report_create_instance(
2928 my_data->instance_dispatch_table,
2929 *pInstance,
2930 pCreateInfo->enabledExtensionCount,
2931 pCreateInfo->ppEnabledExtensionNames);
2932
2933 init_draw_state(my_data, pAllocator);
2934
Courtney Goeltzenleuchterabc035e2015-06-01 14:29:58 -06002935 return result;
2936}
2937
Jon Ashburn3950e1b2015-05-20 09:00:28 -06002938/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wu9ab61502015-11-06 06:42:02 +08002939VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Jon Ashburn3950e1b2015-05-20 09:00:28 -06002940{
Tobin Ehlis8fab6562015-12-01 09:57:09 -07002941 // TODOSC : Shouldn't need any customization here
Tobin Ehlisa16e8922015-06-16 15:50:44 -06002942 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis0b632332015-10-07 09:38:40 -06002943 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
2944 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +08002945 pTable->DestroyInstance(instance, pAllocator);
Tobin Ehlisa16e8922015-06-16 15:50:44 -06002946
2947 // Clean up logging callback, if any
Courtney Goeltzenleuchter62d945a2015-10-05 15:58:38 -06002948 while (my_data->logging_callback.size() > 0) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07002949 VkDebugReportCallbackEXT callback = my_data->logging_callback.back();
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -07002950 layer_destroy_msg_callback(my_data->report_data, callback, pAllocator);
Courtney Goeltzenleuchter62d945a2015-10-05 15:58:38 -06002951 my_data->logging_callback.pop_back();
Tobin Ehlisa16e8922015-06-16 15:50:44 -06002952 }
2953
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06002954 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis0b632332015-10-07 09:38:40 -06002955 delete my_data->instance_dispatch_table;
2956 layer_data_map.erase(key);
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06002957 // TODO : Potential race here with separate threads creating/destroying instance
Tobin Ehlis0b632332015-10-07 09:38:40 -06002958 if (layer_data_map.empty()) {
Mike Stroyanfcb4ba62015-08-18 15:56:18 -06002959 // Release mutex when destroying last instance.
2960 loader_platform_thread_delete_mutex(&globalLock);
2961 globalLockInitialized = 0;
2962 }
Jon Ashburn3950e1b2015-05-20 09:00:28 -06002963}
2964
Jon Ashburne68a9ff2015-05-25 14:11:37 -06002965static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
2966{
Tony Barbour3b4732f2015-07-13 13:37:24 -06002967 uint32_t i;
Tobin Ehlis0b632332015-10-07 09:38:40 -06002968 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
2969 dev_data->device_extensions.debug_marker_enabled = false;
Michael Lentineabc5e922015-10-12 11:30:14 -05002970 dev_data->device_extensions.wsi_enabled = false;
2971
2972
2973 VkLayerDispatchTable *pDisp = dev_data->device_dispatch_table;
2974 PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr;
2975
Michael Lentineabc5e922015-10-12 11:30:14 -05002976 pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR) gpa(device, "vkCreateSwapchainKHR");
2977 pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR) gpa(device, "vkDestroySwapchainKHR");
2978 pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR) gpa(device, "vkGetSwapchainImagesKHR");
2979 pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR) gpa(device, "vkAcquireNextImageKHR");
Mark Lobodzinski8da0d1e2016-01-06 14:58:59 -07002980 pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR) gpa(device, "vkQueuePresentKHR");
Jon Ashburne68a9ff2015-05-25 14:11:37 -06002981
Jon Ashburnf19916e2016-01-11 13:12:43 -07002982 for (i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
Ian Elliott05846062015-11-20 14:13:17 -07002983 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0) {
Michael Lentineabc5e922015-10-12 11:30:14 -05002984 dev_data->device_extensions.wsi_enabled = true;
2985 }
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06002986 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], DEBUG_MARKER_EXTENSION_NAME) == 0) {
Jon Ashburneab34492015-06-01 09:37:38 -06002987 /* Found a matching extension name, mark it enabled and init dispatch table*/
Tobin Ehlis0b632332015-10-07 09:38:40 -06002988 dev_data->device_extensions.debug_marker_enabled = true;
Jon Ashburn1d4b1282015-12-10 19:12:21 -07002989 initDebugMarkerTable(device);
2990
Jon Ashburne68a9ff2015-05-25 14:11:37 -06002991 }
Jon Ashburne68a9ff2015-05-25 14:11:37 -06002992 }
2993}
2994
Chia-I Wu9ab61502015-11-06 06:42:02 +08002995VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06002996{
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07002997 VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
Mark Lobodzinski941aea92016-01-13 10:23:15 -07002998
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07002999 assert(chain_info->u.pLayerInfo);
3000 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
3001 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
3002 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice) fpGetInstanceProcAddr(NULL, "vkCreateDevice");
3003 if (fpCreateDevice == NULL) {
3004 return VK_ERROR_INITIALIZATION_FAILED;
Tobin Ehlisa16e8922015-06-16 15:50:44 -06003005 }
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07003006
3007 // Advance the link info for the next element on the chain
3008 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
3009
3010 VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
3011 if (result != VK_SUCCESS) {
3012 return result;
3013 }
3014
3015 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
3016 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
3017
3018 // Setup device dispatch table
3019 my_device_data->device_dispatch_table = new VkLayerDispatchTable;
3020 layer_init_device_dispatch_table(*pDevice, my_device_data->device_dispatch_table, fpGetDeviceProcAddr);
3021
3022 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
3023 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
3024 // Get physical device limits for this device
3025 my_instance_data->instance_dispatch_table->GetPhysicalDeviceProperties(gpu, &(my_instance_data->physDevPropertyMap[*pDevice]));
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003026 return result;
3027}
Tobin Ehlis559c6382015-11-05 09:52:49 -07003028
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06003029// prototype
3030static void deleteRenderPasses(layer_data*);
Chia-I Wu9ab61502015-11-06 06:42:02 +08003031VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003032{
Tobin Ehlis8fab6562015-12-01 09:57:09 -07003033 // TODOSC : Shouldn't need any customization here
Jeremy Hayes5d29ce32015-06-19 11:37:38 -06003034 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis0b632332015-10-07 09:38:40 -06003035 layer_data* dev_data = get_my_data_ptr(key, layer_data_map);
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06003036 // Free all the memory
3037 loader_platform_thread_lock_mutex(&globalLock);
3038 deletePipelines(dev_data);
3039 deleteRenderPasses(dev_data);
Chia-I Wu3432a0c2015-10-27 18:04:07 +08003040 deleteCommandBuffers(dev_data);
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06003041 deletePools(dev_data);
3042 deleteLayouts(dev_data);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06003043 dev_data->imageViewMap.clear();
3044 dev_data->imageMap.clear();
3045 dev_data->bufferViewMap.clear();
3046 dev_data->bufferMap.clear();
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06003047 loader_platform_thread_unlock_mutex(&globalLock);
3048
Chia-I Wuf7458c52015-10-26 21:10:41 +08003049 dev_data->device_dispatch_table->DestroyDevice(device, pAllocator);
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06003050 tableDebugMarkerMap.erase(key);
Tobin Ehlis0b632332015-10-07 09:38:40 -06003051 delete dev_data->device_dispatch_table;
3052 layer_data_map.erase(key);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003053}
3054
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -07003055static const VkExtensionProperties instance_extensions[] = {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003056 {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07003057 VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
Courtney Goeltzenleuchterb69cd592016-01-19 16:08:39 -07003058 VK_EXT_DEBUG_REPORT_SPEC_VERSION
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06003059 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06003060};
3061
Chia-I Wu9ab61502015-11-06 06:42:02 +08003062VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06003063 const char *pLayerName,
3064 uint32_t *pCount,
3065 VkExtensionProperties* pProperties)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06003066{
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -07003067 return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06003068}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06003069
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -07003070static const VkLayerProperties ds_global_layers[] = {
3071 {
Michael Lentine03107b42015-12-11 10:49:51 -08003072 "VK_LAYER_LUNARG_draw_state",
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -07003073 VK_API_VERSION,
3074 VK_MAKE_VERSION(0, 1, 0),
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -07003075 "Validation layer: draw_state",
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -07003076 }
3077};
3078
Chia-I Wu9ab61502015-11-06 06:42:02 +08003079VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06003080 uint32_t *pCount,
3081 VkLayerProperties* pProperties)
3082{
3083 return util_GetLayerProperties(ARRAY_SIZE(ds_global_layers),
3084 ds_global_layers,
3085 pCount, pProperties);
3086}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06003087
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06003088static const VkExtensionProperties ds_device_extensions[] = {
3089 {
3090 DEBUG_MARKER_EXTENSION_NAME,
3091 VK_MAKE_VERSION(0, 1, 0),
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06003092 }
3093};
3094
3095static const VkLayerProperties ds_device_layers[] = {
3096 {
Michael Lentine1f8d4412016-01-19 14:19:38 -06003097 "VK_LAYER_LUNARG_draw_state",
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06003098 VK_API_VERSION,
3099 VK_MAKE_VERSION(0, 1, 0),
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -07003100 "Validation layer: draw_state",
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06003101 }
3102};
3103
Chia-I Wu9ab61502015-11-06 06:42:02 +08003104VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06003105 VkPhysicalDevice physicalDevice,
3106 const char* pLayerName,
3107 uint32_t* pCount,
3108 VkExtensionProperties* pProperties)
3109{
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07003110 // DrawState does not have any physical device extensions
Jon Ashburn751c4842015-11-02 17:37:20 -07003111 if (pLayerName == NULL) {
3112 dispatch_key key = get_dispatch_key(physicalDevice);
3113 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07003114 return my_data->instance_dispatch_table->EnumerateDeviceExtensionProperties(
Jon Ashburn751c4842015-11-02 17:37:20 -07003115 physicalDevice,
3116 NULL,
3117 pCount,
3118 pProperties);
3119 } else {
3120 return util_GetExtensionProperties(ARRAY_SIZE(ds_device_extensions),
3121 ds_device_extensions,
3122 pCount, pProperties);
3123 }
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06003124}
3125
Chia-I Wu9ab61502015-11-06 06:42:02 +08003126VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06003127 VkPhysicalDevice physicalDevice,
3128 uint32_t* pCount,
3129 VkLayerProperties* pProperties)
3130{
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07003131 /* DrawState physical device layers are the same as global */
Courtney Goeltzenleuchter7ad58c02015-07-06 22:31:52 -06003132 return util_GetLayerProperties(ARRAY_SIZE(ds_device_layers), ds_device_layers,
3133 pCount, pProperties);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06003134}
3135
Courtney Goeltzenleuchtere6dd8082015-12-16 16:07:01 -07003136VkBool32 ValidateCmdBufImageLayouts(VkCommandBuffer cmdBuffer) {
Mark Youngb20a6a82016-01-07 15:41:43 -07003137 VkBool32 skip_call = VK_FALSE;
Michael Lentineabc5e922015-10-12 11:30:14 -05003138 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
3139 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, cmdBuffer);
3140 for (auto cb_image_data : pCB->imageLayoutMap) {
3141 auto image_data = dev_data->imageLayoutMap.find(cb_image_data.first);
3142 if (image_data == dev_data->imageLayoutMap.end()) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07003143 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Young93ecb1d2016-01-13 13:47:16 -07003144 "Cannot submit cmd buffer using deleted image %" PRIu64 ".", (uint64_t)(cb_image_data.first));
Michael Lentineabc5e922015-10-12 11:30:14 -05003145 } else {
3146 if (dev_data->imageLayoutMap[cb_image_data.first]->layout != cb_image_data.second.initialLayout) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07003147 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05003148 "Cannot submit cmd buffer using image with layout %d when first use is %d.", dev_data->imageLayoutMap[cb_image_data.first]->layout, cb_image_data.second.initialLayout);
3149 }
3150 dev_data->imageLayoutMap[cb_image_data.first]->layout = cb_image_data.second.layout;
3151 }
3152 }
3153 return skip_call;
3154}
Tobin Ehlise6e574b2016-01-24 23:25:31 -07003155// Track which resources are in-flight by atomically incrementing their "in_use" count
Mark Young7a69c302016-01-07 09:48:47 -07003156VkBool32 validateAndIncrementResources(layer_data* my_data, GLOBAL_CB_NODE* pCB) {
Mark Youngb20a6a82016-01-07 15:41:43 -07003157 VkBool32 skip_call = VK_FALSE;
Michael Lentine700b0aa2015-10-30 17:57:32 -07003158 for (auto drawDataElement : pCB->drawData) {
3159 for (auto buffer : drawDataElement.buffers) {
3160 auto buffer_data = my_data->bufferMap.find(buffer);
3161 if (buffer_data == my_data->bufferMap.end()) {
Mark Young93ecb1d2016-01-13 13:47:16 -07003162 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer), __LINE__, DRAWSTATE_INVALID_BUFFER, "DS",
3163 "Cannot submit cmd buffer using deleted buffer %" PRIu64 ".", (uint64_t)(buffer));
Michael Lentine700b0aa2015-10-30 17:57:32 -07003164 } else {
3165 buffer_data->second.in_use.fetch_add(1);
3166 }
3167 }
3168 }
Tobin Ehlise6e574b2016-01-24 23:25:31 -07003169 for (auto set : pCB->uniqueBoundSets) {
3170 auto setNode = my_data->setMap.find(set);
3171 if (setNode == my_data->setMap.end()) {
3172 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)(set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
3173 "Cannot submit cmd buffer using deleted descriptor set %" PRIu64 ".", (uint64_t)(set));
3174 } else {
3175 setNode->second->in_use.fetch_add(1);
3176 }
3177 }
Michael Lentine2e068b22015-12-29 16:05:27 -06003178 return skip_call;
Michael Lentine700b0aa2015-10-30 17:57:32 -07003179}
3180
3181void decrementResources(layer_data* my_data, VkCommandBuffer cmdBuffer) {
3182 GLOBAL_CB_NODE* pCB = getCBNode(my_data, cmdBuffer);
3183 for (auto drawDataElement : pCB->drawData) {
3184 for (auto buffer : drawDataElement.buffers) {
3185 auto buffer_data = my_data->bufferMap.find(buffer);
3186 if (buffer_data != my_data->bufferMap.end()) {
3187 buffer_data->second.in_use.fetch_sub(1);
3188 }
3189 }
3190 }
Tobin Ehlise6e574b2016-01-24 23:25:31 -07003191 for (auto set : pCB->uniqueBoundSets) {
Tobin Ehlis9c4f38d2016-01-14 12:47:19 -07003192 auto setNode = my_data->setMap.find(set);
3193 if (setNode != my_data->setMap.end()) {
3194 setNode->second->in_use.fetch_sub(1);
3195 }
3196 }
Michael Lentineb887b0a2015-12-29 14:12:11 -06003197 for (auto queryStatePair : pCB->queryToStateMap) {
3198 my_data->queryToStateMap[queryStatePair.first] = queryStatePair.second;
3199 }
Michael Lentine700b0aa2015-10-30 17:57:32 -07003200}
3201
3202void decrementResources(layer_data* my_data, uint32_t fenceCount, const VkFence* pFences) {
3203 for (uint32_t i = 0; i < fenceCount; ++i) {
3204 auto fence_data = my_data->fenceMap.find(pFences[i]);
3205 if (fence_data == my_data->fenceMap.end() || !fence_data->second.needsSignaled) return;
3206 fence_data->second.needsSignaled = false;
3207 if (fence_data->second.priorFence != VK_NULL_HANDLE) {
3208 decrementResources(my_data, 1, &fence_data->second.priorFence);
3209 }
3210 for (auto cmdBuffer : fence_data->second.cmdBuffers) {
3211 decrementResources(my_data, cmdBuffer);
3212 }
3213 }
3214}
Mark Lobodzinski8da0d1e2016-01-06 14:58:59 -07003215
Michael Lentine700b0aa2015-10-30 17:57:32 -07003216void decrementResources(layer_data* my_data, VkQueue queue) {
3217 auto queue_data = my_data->queueMap.find(queue);
3218 if (queue_data != my_data->queueMap.end()) {
3219 for (auto cmdBuffer : queue_data->second.untrackedCmdBuffers) {
3220 decrementResources(my_data, cmdBuffer);
3221 }
Mark Lobodzinski8da0d1e2016-01-06 14:58:59 -07003222 queue_data->second.untrackedCmdBuffers.clear();
Michael Lentine700b0aa2015-10-30 17:57:32 -07003223 decrementResources(my_data, 1, &queue_data->second.priorFence);
3224 }
3225}
3226
3227void trackCommandBuffers(layer_data* my_data, VkQueue queue, uint32_t cmdBufferCount, const VkCommandBuffer* pCmdBuffers, VkFence fence) {
3228 auto queue_data = my_data->queueMap.find(queue);
3229 if (fence != VK_NULL_HANDLE) {
3230 VkFence priorFence = VK_NULL_HANDLE;
3231 if (queue_data != my_data->queueMap.end()) {
3232 priorFence = queue_data->second.priorFence;
3233 queue_data->second.priorFence = fence;
3234 for (auto cmdBuffer : queue_data->second.untrackedCmdBuffers) {
3235 my_data->fenceMap[fence].cmdBuffers.push_back(cmdBuffer);
3236 }
3237 queue_data->second.untrackedCmdBuffers.clear();
3238 }
3239 my_data->fenceMap[fence].cmdBuffers.clear();
3240 my_data->fenceMap[fence].priorFence = priorFence;
3241 my_data->fenceMap[fence].needsSignaled = true;
Tobin Ehlis74714d22016-01-25 15:24:34 -08003242 my_data->fenceMap[fence].queue = queue;
Michael Lentine700b0aa2015-10-30 17:57:32 -07003243 for (uint32_t i = 0; i < cmdBufferCount; ++i) {
Tobin Ehlisf4aafc02016-01-15 13:34:44 -07003244 for (auto secondaryCmdBuffer : my_data->commandBufferMap[pCmdBuffers[i]]->secondaryCommandBuffers) {
3245 my_data->fenceMap[fence].cmdBuffers.push_back(secondaryCmdBuffer);
3246 }
Michael Lentine700b0aa2015-10-30 17:57:32 -07003247 my_data->fenceMap[fence].cmdBuffers.push_back(pCmdBuffers[i]);
3248 }
3249 } else {
3250 if (queue_data != my_data->queueMap.end()) {
3251 for (uint32_t i = 0; i < cmdBufferCount; ++i) {
Tobin Ehlisf4aafc02016-01-15 13:34:44 -07003252 for (auto secondaryCmdBuffer : my_data->commandBufferMap[pCmdBuffers[i]]->secondaryCommandBuffers) {
3253 queue_data->second.untrackedCmdBuffers.push_back(secondaryCmdBuffer);
3254 }
Michael Lentine700b0aa2015-10-30 17:57:32 -07003255 queue_data->second.untrackedCmdBuffers.push_back(pCmdBuffers[i]);
3256 }
3257 }
3258 }
Michael Lentineb887b0a2015-12-29 14:12:11 -06003259 if (queue_data != my_data->queueMap.end()) {
3260 for (uint32_t i = 0; i < cmdBufferCount; ++i) {
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003261 // Add cmdBuffers to both the global set and queue set
Tobin Ehlisf4aafc02016-01-15 13:34:44 -07003262 for (auto secondaryCmdBuffer : my_data->commandBufferMap[pCmdBuffers[i]]->secondaryCommandBuffers) {
Tobin Ehlis74714d22016-01-25 15:24:34 -08003263 my_data->globalInFlightCmdBuffers.insert(secondaryCmdBuffer);
3264 queue_data->second.inFlightCmdBuffers.insert(secondaryCmdBuffer);
Tobin Ehlisf4aafc02016-01-15 13:34:44 -07003265 }
Tobin Ehlis74714d22016-01-25 15:24:34 -08003266 my_data->globalInFlightCmdBuffers.insert(pCmdBuffers[i]);
3267 queue_data->second.inFlightCmdBuffers.insert(pCmdBuffers[i]);
3268 }
3269 }
3270}
3271
3272static VkBool32 validateCommandBufferState(layer_data* dev_data, GLOBAL_CB_NODE* pCB)
3273{
3274 // Track in-use for resources off of primary and any secondary CBs
3275 VkBool32 skipCall = validateAndIncrementResources(dev_data, pCB);
3276 if (!pCB->secondaryCommandBuffers.empty()) {
3277 for (auto secondaryCmdBuffer : pCB->secondaryCommandBuffers) {
3278 skipCall |= validateAndIncrementResources(dev_data, dev_data->commandBufferMap[secondaryCmdBuffer]);
3279 }
3280 }
3281 if ((pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) && (pCB->submitCount > 1)) {
3282 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_COMMAND_BUFFER_SINGLE_SUBMIT_VIOLATION, "DS",
3283 "CB %#" PRIxLEAST64 " was begun w/ VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT set, but has been submitted %#" PRIxLEAST64 " times.",
3284 (uint64_t)(pCB->commandBuffer), pCB->submitCount);
3285 }
3286 // Validate that cmd buffers have been updated
3287 if (CB_RECORDED != pCB->state) {
3288 if (CB_INVALID == pCB->state) {
3289 // Inform app of reason CB invalid
3290 if (!pCB->destroyedSets.empty()) {
3291 std::stringstream set_string;
3292 for (auto set : pCB->destroyedSets) {
3293 set_string << " " << set;
3294 }
3295 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
3296 "You are submitting command buffer %#" PRIxLEAST64 " that is invalid because it had the following bound descriptor set(s) destroyed: %s", (uint64_t)(pCB->commandBuffer), set_string.str().c_str());
3297 }
3298 if (!pCB->updatedSets.empty()) {
3299 std::stringstream set_string;
3300 for (auto set : pCB->updatedSets) {
3301 set_string << " " << set;
3302 }
3303 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
3304 "You are submitting command buffer %#" PRIxLEAST64 " that is invalid because it had the following bound descriptor set(s) updated: %s", (uint64_t)(pCB->commandBuffer), set_string.str().c_str());
3305 }
3306 } else { // Flag error for using CB w/o vkEndCommandBuffer() called
3307 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_NO_END_COMMAND_BUFFER, "DS",
3308 "You must call vkEndCommandBuffer() on CB %#" PRIxLEAST64 " before this call to vkQueueSubmit()!", (uint64_t)(pCB->commandBuffer));
3309 loader_platform_thread_unlock_mutex(&globalLock);
3310 return VK_ERROR_VALIDATION_FAILED_EXT;
3311 }
3312 }
3313 // If USAGE_SIMULTANEOUS_USE_BIT not set then CB cannot already be executing on device
3314 if (!(pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
3315 if (dev_data->globalInFlightCmdBuffers.find(pCB->commandBuffer) != dev_data->globalInFlightCmdBuffers.end()) {
3316 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_CB_SIMULTANEOUS_USE, "DS",
3317 "Attempt to simultaneously execute CB %#" PRIxLEAST64 " w/o VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT set!", (uint64_t)(pCB->commandBuffer));
Michael Lentineb887b0a2015-12-29 14:12:11 -06003318 }
3319 }
Michael Lentine700b0aa2015-10-30 17:57:32 -07003320}
3321
Chia-I Wu9ab61502015-11-06 06:42:02 +08003322VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003323{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06003324 VkBool32 skipCall = VK_FALSE;
Tobin Ehlisa9f3d762015-05-22 12:38:16 -06003325 GLOBAL_CB_NODE* pCB = NULL;
Tobin Ehlis0b632332015-10-07 09:38:40 -06003326 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
Courtney Goeltzenleuchter646b9072015-10-20 18:04:07 -06003327 for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
Chia-I Wu40cf0ae2015-10-26 17:20:32 +08003328 const VkSubmitInfo *submit = &pSubmits[submit_idx];
Michael Lentine15a47882016-01-06 10:05:48 -06003329 for (uint32_t i=0; i < submit->waitSemaphoreCount; ++i) {
3330 if (dev_data->semaphoreSignaledMap[submit->pWaitSemaphores[i]]) {
3331 dev_data->semaphoreSignaledMap[submit->pWaitSemaphores[i]] = 0;
3332 } else {
3333 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
3334 "Queue %#" PRIx64 " is waiting on semaphore %#" PRIx64 " that has no way to be signaled.",
Mark Youngee3f3a22016-01-25 12:18:32 -07003335 (uint64_t)(queue), (uint64_t)(submit->pWaitSemaphores[i]));
Michael Lentine15a47882016-01-06 10:05:48 -06003336 }
3337 }
3338 for (uint32_t i=0; i < submit->signalSemaphoreCount; ++i) {
3339 dev_data->semaphoreSignaledMap[submit->pSignalSemaphores[i]] = 1;
3340 }
Chia-I Wud50a7d72015-10-26 20:48:51 +08003341 for (uint32_t i=0; i < submit->commandBufferCount; i++) {
Mark Lobodzinski31e5f282015-11-30 16:48:53 -07003342
3343#ifndef DISABLE_IMAGE_LAYOUT_VALIDATION
3344 skipCall |= ValidateCmdBufImageLayouts(submit->pCommandBuffers[i]);
3345#endif // DISABLE_IMAGE_LAYOUT_VALIDATION
3346
Courtney Goeltzenleuchter646b9072015-10-20 18:04:07 -06003347 pCB = getCBNode(dev_data, submit->pCommandBuffers[i]);
3348 loader_platform_thread_lock_mutex(&globalLock);
3349 pCB->submitCount++; // increment submit count
Tobin Ehlise6e574b2016-01-24 23:25:31 -07003350 skipCall |= validateCommandBufferState(dev_data, pCB);
Tobin Ehlisc4bdde12015-05-27 14:30:06 -06003351 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlisa9f3d762015-05-22 12:38:16 -06003352 }
Michael Lentine700b0aa2015-10-30 17:57:32 -07003353 trackCommandBuffers(dev_data, queue, submit->commandBufferCount, submit->pCommandBuffers, fence);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003354 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06003355 if (VK_FALSE == skipCall)
Chia-I Wu40cf0ae2015-10-26 17:20:32 +08003356 return dev_data->device_dispatch_table->QueueSubmit(queue, submitCount, pSubmits, fence);
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07003357 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003358}
3359
Mark Youngb20a6a82016-01-07 15:41:43 -07003360VkBool32 cleanInFlightCmdBuffer(layer_data* my_data, VkCommandBuffer cmdBuffer) {
3361 VkBool32 skip_call = VK_FALSE;
Michael Lentineb887b0a2015-12-29 14:12:11 -06003362 GLOBAL_CB_NODE* pCB = getCBNode(my_data, cmdBuffer);
Mark Lobodzinskia9e7c042016-01-25 14:27:49 -07003363 if (pCB) {
3364 for (auto queryEventsPair : pCB->waitedEventsBeforeQueryReset) {
3365 for (auto event : queryEventsPair.second) {
3366 if (my_data->eventMap[event].needsSignaled) {
3367 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, 0, DRAWSTATE_INVALID_QUERY, "DS",
3368 "Cannot get query results on queryPool %" PRIu64 " with index %d which was guarded by unsignaled event %" PRIu64 ".",
3369 (uint64_t)(queryEventsPair.first.pool), queryEventsPair.first.index, (uint64_t)(event));
3370 }
Michael Lentineb887b0a2015-12-29 14:12:11 -06003371 }
3372 }
3373 }
3374 return skip_call;
3375}
Tobin Ehlis74714d22016-01-25 15:24:34 -08003376// Remove given cmd_buffer from the global inFlight set.
3377// Also, if given queue is valid, then remove the cmd_buffer from that queues
3378// inFlightCmdBuffer set. Finally, check all other queues and if given cmd_buffer
3379// is still in flight on another queue, add it back into the global set.
3380static inline void removeInFlightCmdBuffer(layer_data* dev_data, VkCommandBuffer cmd_buffer, VkQueue queue)
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003381{
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003382 // Pull it off of global list initially, but if we find it in any other queue list, add it back in
Tobin Ehlis74714d22016-01-25 15:24:34 -08003383 dev_data->globalInFlightCmdBuffers.erase(cmd_buffer);
3384 if (dev_data->queueMap.find(queue) != dev_data->queueMap.end()) {
3385 dev_data->queueMap[queue].inFlightCmdBuffers.erase(cmd_buffer);
3386 for (auto q : dev_data->queues) {
3387 if ((q != queue) && (dev_data->queueMap[q].inFlightCmdBuffers.find(cmd_buffer) != dev_data->queueMap[q].inFlightCmdBuffers.end())) {
3388 dev_data->globalInFlightCmdBuffers.insert(cmd_buffer);
3389 break;
3390 }
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003391 }
3392 }
3393}
Michael Lentineb887b0a2015-12-29 14:12:11 -06003394
Michael Lentine700b0aa2015-10-30 17:57:32 -07003395VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkWaitForFences(VkDevice device, uint32_t fenceCount, const VkFence* pFences, VkBool32 waitAll, uint64_t timeout)
3396{
3397 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
3398 VkResult result = dev_data->device_dispatch_table->WaitForFences(device, fenceCount, pFences, waitAll, timeout);
Mark Youngb20a6a82016-01-07 15:41:43 -07003399 VkBool32 skip_call = VK_FALSE;
Tobin Ehlis74714d22016-01-25 15:24:34 -08003400 if (result == VK_SUCCESS) {
Tobin Ehlise6e574b2016-01-24 23:25:31 -07003401 // When we know that all fences are complete we can clean/remove their CBs
Tobin Ehlis74714d22016-01-25 15:24:34 -08003402 if (waitAll || fenceCount == 1) {
3403 for (uint32_t i = 0; i < fenceCount; ++i) {
3404 VkQueue fence_queue = dev_data->fenceMap[pFences[i]].queue;
3405 for (auto cmdBuffer : dev_data->fenceMap[pFences[i]].cmdBuffers) {
3406 skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
3407 removeInFlightCmdBuffer(dev_data, cmdBuffer, fence_queue);
3408 }
3409 }
3410 decrementResources(dev_data, fenceCount, pFences);
Michael Lentineb887b0a2015-12-29 14:12:11 -06003411 }
Tobin Ehlise6e574b2016-01-24 23:25:31 -07003412 // NOTE : Alternate case not handled here is when some fences have completed. In
3413 // this case for app to guarantee which fences completed it will have to call
3414 // vkGetFenceStatus() at which point we'll clean/remove their CBs if complete.
Michael Lentine700b0aa2015-10-30 17:57:32 -07003415 }
Mark Youngb20a6a82016-01-07 15:41:43 -07003416 if (VK_FALSE != skip_call)
Mark Lobodzinskice738852016-01-07 10:04:02 -07003417 return VK_ERROR_VALIDATION_FAILED_EXT;
Michael Lentine700b0aa2015-10-30 17:57:32 -07003418 return result;
3419}
3420
3421
3422VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkGetFenceStatus(VkDevice device, VkFence fence)
3423{
3424 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
3425 VkResult result = dev_data->device_dispatch_table->GetFenceStatus(device, fence);
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003426 VkBool32 skip_call = VK_FALSE;
Michael Lentine700b0aa2015-10-30 17:57:32 -07003427 if (result == VK_SUCCESS) {
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003428 auto fence_queue = dev_data->fenceMap[fence].queue;
Michael Lentineb887b0a2015-12-29 14:12:11 -06003429 for (auto cmdBuffer : dev_data->fenceMap[fence].cmdBuffers) {
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003430 skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
Tobin Ehlis74714d22016-01-25 15:24:34 -08003431 removeInFlightCmdBuffer(dev_data, cmdBuffer, fence_queue);
Michael Lentineb887b0a2015-12-29 14:12:11 -06003432 }
Michael Lentine700b0aa2015-10-30 17:57:32 -07003433 decrementResources(dev_data, 1, &fence);
3434 }
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003435 if (VK_FALSE != skip_call)
3436 return VK_ERROR_VALIDATION_FAILED_EXT;
Michael Lentine700b0aa2015-10-30 17:57:32 -07003437 return result;
3438}
3439
3440VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue)
3441{
3442 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
3443 dev_data->device_dispatch_table->GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003444 dev_data->queues.push_back(*pQueue);
Michael Lentine700b0aa2015-10-30 17:57:32 -07003445 dev_data->queueMap[*pQueue].device = device;
3446}
3447
3448VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueueWaitIdle(VkQueue queue)
3449{
3450 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
3451 decrementResources(dev_data, queue);
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003452 VkBool32 skip_call = VK_FALSE;
3453 // Iterate over local set since we erase set members as we go in for loop
3454 auto local_cb_set = dev_data->queueMap[queue].inFlightCmdBuffers;
3455 for (auto cmdBuffer : local_cb_set) {
3456 skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
Tobin Ehlis74714d22016-01-25 15:24:34 -08003457 removeInFlightCmdBuffer(dev_data, cmdBuffer, queue);
Michael Lentineb887b0a2015-12-29 14:12:11 -06003458 }
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003459 dev_data->queueMap[queue].inFlightCmdBuffers.clear();
3460 if (VK_FALSE != skip_call)
3461 return VK_ERROR_VALIDATION_FAILED_EXT;
Michael Lentine700b0aa2015-10-30 17:57:32 -07003462 return dev_data->device_dispatch_table->QueueWaitIdle(queue);
3463}
3464
3465VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkDeviceWaitIdle(VkDevice device)
3466{
Tobin Ehlis74714d22016-01-25 15:24:34 -08003467 VkBool32 skip_call = VK_FALSE;
Michael Lentine700b0aa2015-10-30 17:57:32 -07003468 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Tobin Ehlisae82e7f2016-01-20 16:23:37 -07003469 for (auto queue : dev_data->queues) {
3470 decrementResources(dev_data, queue);
Tobin Ehlis74714d22016-01-25 15:24:34 -08003471 if (dev_data->queueMap.find(queue) != dev_data->queueMap.end()) {
3472 // Clear all of the queue inFlightCmdBuffers (global set cleared below)
3473 dev_data->queueMap[queue].inFlightCmdBuffers.clear();
3474 }
Michael Lentine700b0aa2015-10-30 17:57:32 -07003475 }
Tobin Ehlis74714d22016-01-25 15:24:34 -08003476 for (auto cmdBuffer : dev_data->globalInFlightCmdBuffers) {
3477 skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
Michael Lentineb887b0a2015-12-29 14:12:11 -06003478 }
Tobin Ehlis74714d22016-01-25 15:24:34 -08003479 dev_data->globalInFlightCmdBuffers.clear();
3480 if (VK_FALSE != skip_call)
3481 return VK_ERROR_VALIDATION_FAILED_EXT;
Michael Lentine700b0aa2015-10-30 17:57:32 -07003482 return dev_data->device_dispatch_table->DeviceWaitIdle(device);
3483}
3484
Chia-I Wu9ab61502015-11-06 06:42:02 +08003485VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyFence(VkDevice device, VkFence fence, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003486{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003487 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyFence(device, fence, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003488 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003489}
3490
Chia-I Wu9ab61502015-11-06 06:42:02 +08003491VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroySemaphore(VkDevice device, VkSemaphore semaphore, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003492{
Michael Lentine15a47882016-01-06 10:05:48 -06003493 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
3494 dev_data->device_dispatch_table->DestroySemaphore(device, semaphore, pAllocator);
3495 dev_data->semaphoreSignaledMap.erase(semaphore);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003496 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003497}
3498
Chia-I Wu9ab61502015-11-06 06:42:02 +08003499VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003500{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003501 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyEvent(device, event, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003502 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003503}
3504
Chia-I Wu9ab61502015-11-06 06:42:02 +08003505VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyQueryPool(VkDevice device, VkQueryPool queryPool, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003506{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003507 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyQueryPool(device, queryPool, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003508 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003509}
3510
Mark Lobodzinskice738852016-01-07 10:04:02 -07003511VKAPI_ATTR VkResult VKAPI_CALL vkGetQueryPoolResults(VkDevice device, VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount,
Michael Lentineb887b0a2015-12-29 14:12:11 -06003512 size_t dataSize, void* pData, VkDeviceSize stride, VkQueryResultFlags flags) {
3513 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
3514 unordered_map<QueryObject, vector<VkCommandBuffer>> queriesInFlight;
3515 GLOBAL_CB_NODE* pCB = nullptr;
Tobin Ehlis74714d22016-01-25 15:24:34 -08003516 for (auto cmdBuffer : dev_data->globalInFlightCmdBuffers) {
Michael Lentineb887b0a2015-12-29 14:12:11 -06003517 pCB = getCBNode(dev_data, cmdBuffer);
3518 for (auto queryStatePair : pCB->queryToStateMap) {
3519 queriesInFlight[queryStatePair.first].push_back(cmdBuffer);
3520 }
3521 }
Mark Youngb20a6a82016-01-07 15:41:43 -07003522 VkBool32 skip_call = VK_FALSE;
Michael Lentineb887b0a2015-12-29 14:12:11 -06003523 for (uint32_t i = 0; i < queryCount; ++i) {
Mark Lobodzinskice738852016-01-07 10:04:02 -07003524 QueryObject query = {queryPool, firstQuery + i};
Michael Lentineb887b0a2015-12-29 14:12:11 -06003525 auto queryElement = queriesInFlight.find(query);
3526 auto queryToStateElement = dev_data->queryToStateMap.find(query);
3527 if (queryToStateElement != dev_data->queryToStateMap.end()) {
3528 }
3529 // Available and in flight
3530 if(queryElement != queriesInFlight.end() && queryToStateElement != dev_data->queryToStateMap.end() && queryToStateElement->second) {
3531 for (auto cmdBuffer : queryElement->second) {
3532 pCB = getCBNode(dev_data, cmdBuffer);
3533 auto queryEventElement = pCB->waitedEventsBeforeQueryReset.find(query);
3534 if (queryEventElement == pCB->waitedEventsBeforeQueryReset.end()) {
Mark Lobodzinskice738852016-01-07 10:04:02 -07003535 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, __LINE__,
3536 DRAWSTATE_INVALID_QUERY, "DS", "Cannot get query results on queryPool %" PRIu64 " with index %d which is in flight.",
Mark Young93ecb1d2016-01-13 13:47:16 -07003537 (uint64_t)(queryPool), firstQuery + i);
Michael Lentineb887b0a2015-12-29 14:12:11 -06003538 } else {
3539 for (auto event : queryEventElement->second) {
3540 dev_data->eventMap[event].needsSignaled = true;
3541 }
3542 }
3543 }
3544 // Unavailable and in flight
3545 } else if (queryElement != queriesInFlight.end() && queryToStateElement != dev_data->queryToStateMap.end() && !queryToStateElement->second) {
3546 // TODO : Can there be the same query in use by multiple command buffers in flight?
3547 bool make_available = false;
3548 for (auto cmdBuffer : queryElement->second) {
3549 pCB = getCBNode(dev_data, cmdBuffer);
3550 make_available |= pCB->queryToStateMap[query];
3551 }
3552 if (!(((flags & VK_QUERY_RESULT_PARTIAL_BIT) || (flags & VK_QUERY_RESULT_WAIT_BIT)) && make_available)) {
Mark Lobodzinskice738852016-01-07 10:04:02 -07003553 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
Michael Lentineb887b0a2015-12-29 14:12:11 -06003554 "Cannot get query results on queryPool %" PRIu64 " with index %d which is unavailable.",
Mark Young93ecb1d2016-01-13 13:47:16 -07003555 (uint64_t)(queryPool), firstQuery + i);
Michael Lentineb887b0a2015-12-29 14:12:11 -06003556 }
3557 // Unavailable
3558 } else if (queryToStateElement != dev_data->queryToStateMap.end() && !queryToStateElement->second) {
Mark Lobodzinskice738852016-01-07 10:04:02 -07003559 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
Michael Lentineb887b0a2015-12-29 14:12:11 -06003560 "Cannot get query results on queryPool %" PRIu64 " with index %d which is unavailable.",
Mark Young93ecb1d2016-01-13 13:47:16 -07003561 (uint64_t)(queryPool), firstQuery + i);
Michael Lentineb887b0a2015-12-29 14:12:11 -06003562 // Unitialized
3563 } else if (queryToStateElement == dev_data->queryToStateMap.end()) {
Mark Lobodzinskice738852016-01-07 10:04:02 -07003564 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
Michael Lentineb887b0a2015-12-29 14:12:11 -06003565 "Cannot get query results on queryPool %" PRIu64 " with index %d which is uninitialized.",
Mark Young93ecb1d2016-01-13 13:47:16 -07003566 (uint64_t)(queryPool), firstQuery + i);
Michael Lentineb887b0a2015-12-29 14:12:11 -06003567 }
3568 }
3569 if (skip_call)
Mark Lobodzinskice738852016-01-07 10:04:02 -07003570 return VK_ERROR_VALIDATION_FAILED_EXT;
3571 return dev_data->device_dispatch_table->GetQueryPoolResults(device, queryPool, firstQuery, queryCount, dataSize, pData, stride, flags);
Michael Lentineb887b0a2015-12-29 14:12:11 -06003572}
3573
Mark Young7a69c302016-01-07 09:48:47 -07003574VkBool32 validateIdleBuffer(const layer_data* my_data, VkBuffer buffer) {
Mark Youngb20a6a82016-01-07 15:41:43 -07003575 VkBool32 skip_call = VK_FALSE;
Michael Lentine700b0aa2015-10-30 17:57:32 -07003576 auto buffer_data = my_data->bufferMap.find(buffer);
3577 if (buffer_data == my_data->bufferMap.end()) {
Mark Young93ecb1d2016-01-13 13:47:16 -07003578 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
3579 "Cannot free buffer %" PRIxLEAST64 " that has not been allocated.", (uint64_t)(buffer));
Michael Lentine700b0aa2015-10-30 17:57:32 -07003580 } else {
3581 if (buffer_data->second.in_use.load()) {
Mark Young93ecb1d2016-01-13 13:47:16 -07003582 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer), __LINE__, DRAWSTATE_OBJECT_INUSE, "DS",
3583 "Cannot free buffer %" PRIxLEAST64 " that is in use by a command buffer.", (uint64_t)(buffer));
Michael Lentine700b0aa2015-10-30 17:57:32 -07003584 }
3585 }
3586 return skip_call;
3587}
3588
Chia-I Wu9ab61502015-11-06 06:42:02 +08003589VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003590{
Tobin Ehlisa1c28562015-10-23 16:00:08 -06003591 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Mark Youngb20a6a82016-01-07 15:41:43 -07003592 VkBool32 skip_call = VK_FALSE;
Michael Lentine700b0aa2015-10-30 17:57:32 -07003593 if (!validateIdleBuffer(dev_data, buffer)) {
3594 dev_data->device_dispatch_table->DestroyBuffer(device, buffer, pAllocator);
3595 }
Chia-I Wue2fc5522015-10-26 20:04:44 +08003596 dev_data->bufferMap.erase(buffer);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003597}
3598
Chia-I Wu9ab61502015-11-06 06:42:02 +08003599VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyBufferView(VkDevice device, VkBufferView bufferView, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003600{
Tobin Ehlisa1c28562015-10-23 16:00:08 -06003601 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08003602 dev_data->device_dispatch_table->DestroyBufferView(device, bufferView, pAllocator);
Chia-I Wue2fc5522015-10-26 20:04:44 +08003603 dev_data->bufferViewMap.erase(bufferView);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003604}
3605
Chia-I Wu9ab61502015-11-06 06:42:02 +08003606VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003607{
Tobin Ehlisa1c28562015-10-23 16:00:08 -06003608 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08003609 dev_data->device_dispatch_table->DestroyImage(device, image, pAllocator);
Chia-I Wue2fc5522015-10-26 20:04:44 +08003610 dev_data->imageMap.erase(image);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003611}
3612
Chia-I Wu9ab61502015-11-06 06:42:02 +08003613VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyImageView(VkDevice device, VkImageView imageView, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003614{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003615 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyImageView(device, imageView, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003616 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003617}
3618
Chia-I Wu9ab61502015-11-06 06:42:02 +08003619VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyShaderModule(VkDevice device, VkShaderModule shaderModule, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003620{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003621 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyShaderModule(device, shaderModule, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003622 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003623}
3624
Chia-I Wu9ab61502015-11-06 06:42:02 +08003625VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003626{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003627 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyPipeline(device, pipeline, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003628 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003629}
3630
Chia-I Wu9ab61502015-11-06 06:42:02 +08003631VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyPipelineLayout(VkDevice device, VkPipelineLayout pipelineLayout, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003632{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003633 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyPipelineLayout(device, pipelineLayout, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003634 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003635}
3636
Chia-I Wu9ab61502015-11-06 06:42:02 +08003637VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroySampler(VkDevice device, VkSampler sampler, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003638{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003639 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroySampler(device, sampler, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003640 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003641}
3642
Chia-I Wu9ab61502015-11-06 06:42:02 +08003643VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDescriptorSetLayout(VkDevice device, VkDescriptorSetLayout descriptorSetLayout, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003644{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003645 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyDescriptorSetLayout(device, descriptorSetLayout, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003646 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003647}
3648
Chia-I Wu9ab61502015-11-06 06:42:02 +08003649VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003650{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003651 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyDescriptorPool(device, descriptorPool, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003652 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003653}
3654
Chia-I Wu9ab61502015-11-06 06:42:02 +08003655VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t count, const VkCommandBuffer *pCommandBuffers)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003656{
Mark Lobodzinski39298632015-11-18 08:38:27 -07003657 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
3658
Courtney Goeltzenleuchter51239472015-12-16 16:06:06 -07003659 for (uint32_t i = 0; i < count; i++) {
Tobin Ehlis74714d22016-01-25 15:24:34 -08003660 loader_platform_thread_lock_mutex(&globalLock);
Mark Lobodzinski39298632015-11-18 08:38:27 -07003661 // Delete CB information structure, and remove from commandBufferMap
3662 auto cb = dev_data->commandBufferMap.find(pCommandBuffers[i]);
3663 if (cb != dev_data->commandBufferMap.end()) {
Tobin Ehlise6e574b2016-01-24 23:25:31 -07003664 // reset prior to delete for data clean-up
3665 resetCB(dev_data, (*cb).second->commandBuffer);
Mark Lobodzinski39298632015-11-18 08:38:27 -07003666 delete (*cb).second;
3667 dev_data->commandBufferMap.erase(cb);
3668 }
3669
3670 // Remove commandBuffer reference from commandPoolMap
Tobin Ehlisac0ef842015-12-14 13:46:38 -07003671 dev_data->commandPoolMap[commandPool].commandBuffers.remove(pCommandBuffers[i]);
Mark Lobodzinskib8df78c2015-11-20 14:33:48 -07003672 loader_platform_thread_unlock_mutex(&globalLock);
Mark Lobodzinski39298632015-11-18 08:38:27 -07003673 }
3674
3675 dev_data->device_dispatch_table->FreeCommandBuffers(device, commandPool, count, pCommandBuffers);
3676}
3677
3678VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool)
3679{
3680 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
3681
3682 VkResult result = dev_data->device_dispatch_table->CreateCommandPool(device, pCreateInfo, pAllocator, pCommandPool);
3683
3684 if (VK_SUCCESS == result) {
Mark Lobodzinskib8df78c2015-11-20 14:33:48 -07003685 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlisac0ef842015-12-14 13:46:38 -07003686 dev_data->commandPoolMap[*pCommandPool].createFlags = pCreateInfo->flags;
Mark Lobodzinskib8df78c2015-11-20 14:33:48 -07003687 loader_platform_thread_unlock_mutex(&globalLock);
Mark Lobodzinski39298632015-11-18 08:38:27 -07003688 }
3689 return result;
3690}
Michael Lentinee063fe42016-01-27 17:52:20 -06003691
Mark Youngbaa677f2016-01-28 09:36:15 -07003692VkBool32 validateCommandBuffersNotInUse(const layer_data* dev_data, VkCommandPool commandPool) {
3693 VkBool32 skipCall = VK_FALSE;
Michael Lentinee063fe42016-01-27 17:52:20 -06003694 loader_platform_thread_lock_mutex(&globalLock);
3695 auto pool_data = dev_data->commandPoolMap.find(commandPool);
3696 if (pool_data != dev_data->commandPoolMap.end()) {
3697 for (auto cmdBuffer : pool_data->second.commandBuffers) {
Tobin Ehlis74714d22016-01-25 15:24:34 -08003698 if (dev_data->globalInFlightCmdBuffers.count(cmdBuffer)) {
Mark Lobodzinskia724e142016-01-29 10:13:51 -07003699 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_POOL_EXT, (uint64_t)(commandPool),
Tobin Ehlis74714d22016-01-25 15:24:34 -08003700 __LINE__, DRAWSTATE_OBJECT_INUSE, "DS", "Cannot reset command pool %" PRIx64 " when allocated command buffer %" PRIx64 " is in use.",
3701 (uint64_t)(commandPool), (uint64_t)(cmdBuffer));
Michael Lentinee063fe42016-01-27 17:52:20 -06003702 }
3703 }
3704 }
3705 loader_platform_thread_unlock_mutex(&globalLock);
Mark Youngbaa677f2016-01-28 09:36:15 -07003706 return skipCall;
Michael Lentinee063fe42016-01-27 17:52:20 -06003707}
3708
Tobin Ehlis9c4f38d2016-01-14 12:47:19 -07003709// Destroy commandPool along with all of the commandBuffers allocated from that pool
Mark Lobodzinski39298632015-11-18 08:38:27 -07003710VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks* pAllocator)
3711{
3712 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinskib8df78c2015-11-20 14:33:48 -07003713 loader_platform_thread_lock_mutex(&globalLock);
Mark Lobodzinski39298632015-11-18 08:38:27 -07003714
3715 // Must remove cmdpool from cmdpoolmap, after removing all cmdbuffers in its list from the commandPoolMap
3716 if (dev_data->commandPoolMap.find(commandPool) != dev_data->commandPoolMap.end()) {
Tobin Ehlisac0ef842015-12-14 13:46:38 -07003717 for (auto poolCb = dev_data->commandPoolMap[commandPool].commandBuffers.begin(); poolCb != dev_data->commandPoolMap[commandPool].commandBuffers.end();) {
Mark Lobodzinski39298632015-11-18 08:38:27 -07003718 auto del_cb = dev_data->commandBufferMap.find(*poolCb);
3719 delete (*del_cb).second; // delete CB info structure
3720 dev_data->commandBufferMap.erase(del_cb); // Remove this command buffer from cbMap
Tobin Ehlisac0ef842015-12-14 13:46:38 -07003721 poolCb = dev_data->commandPoolMap[commandPool].commandBuffers.erase(poolCb); // Remove CB reference from commandPoolMap's list
Mark Lobodzinski39298632015-11-18 08:38:27 -07003722 }
3723 }
3724 dev_data->commandPoolMap.erase(commandPool);
Mark Lobodzinskib8df78c2015-11-20 14:33:48 -07003725
3726 loader_platform_thread_unlock_mutex(&globalLock);
Michael Lentinee063fe42016-01-27 17:52:20 -06003727
Mark Youngbaa677f2016-01-28 09:36:15 -07003728 if (VK_TRUE == validateCommandBuffersNotInUse(dev_data, commandPool))
Michael Lentinee063fe42016-01-27 17:52:20 -06003729 return;
3730
Mark Lobodzinski39298632015-11-18 08:38:27 -07003731 dev_data->device_dispatch_table->DestroyCommandPool(device, commandPool, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003732}
3733
Tobin Ehlisac0ef842015-12-14 13:46:38 -07003734VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkResetCommandPool(
3735 VkDevice device,
3736 VkCommandPool commandPool,
3737 VkCommandPoolResetFlags flags)
3738{
3739 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07003740 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlisac0ef842015-12-14 13:46:38 -07003741
Mark Youngbaa677f2016-01-28 09:36:15 -07003742 if (VK_TRUE == validateCommandBuffersNotInUse(dev_data, commandPool))
Michael Lentined2a7d632016-01-27 17:02:39 -06003743 return VK_ERROR_VALIDATION_FAILED_EXT;
3744
Tobin Ehlisac0ef842015-12-14 13:46:38 -07003745 result = dev_data->device_dispatch_table->ResetCommandPool(device, commandPool, flags);
3746 // Reset all of the CBs allocated from this pool
3747 if (VK_SUCCESS == result) {
Tobin Ehlis74714d22016-01-25 15:24:34 -08003748 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlisac0ef842015-12-14 13:46:38 -07003749 auto it = dev_data->commandPoolMap[commandPool].commandBuffers.begin();
3750 while (it != dev_data->commandPoolMap[commandPool].commandBuffers.end()) {
3751 resetCB(dev_data, (*it));
3752 ++it;
3753 }
Tobin Ehlis74714d22016-01-25 15:24:34 -08003754 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlisac0ef842015-12-14 13:46:38 -07003755 }
3756 return result;
3757}
3758
Chia-I Wu9ab61502015-11-06 06:42:02 +08003759VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003760{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003761 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyFramebuffer(device, framebuffer, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003762 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003763}
3764
Chia-I Wu9ab61502015-11-06 06:42:02 +08003765VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyRenderPass(VkDevice device, VkRenderPass renderPass, const VkAllocationCallbacks* pAllocator)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003766{
Chia-I Wuf7458c52015-10-26 21:10:41 +08003767 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyRenderPass(device, renderPass, pAllocator);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06003768 // TODO : Clean up any internal data structures using this obj.
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003769}
3770
Chia-I Wu9ab61502015-11-06 06:42:02 +08003771VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer)
Tobin Ehlisa1c28562015-10-23 16:00:08 -06003772{
3773 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08003774 VkResult result = dev_data->device_dispatch_table->CreateBuffer(device, pCreateInfo, pAllocator, pBuffer);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06003775 if (VK_SUCCESS == result) {
3776 loader_platform_thread_lock_mutex(&globalLock);
3777 // TODO : This doesn't create deep copy of pQueueFamilyIndices so need to fix that if/when we want that data to be valid
Michael Lentine700b0aa2015-10-30 17:57:32 -07003778 dev_data->bufferMap[*pBuffer].create_info = unique_ptr<VkBufferCreateInfo>(new VkBufferCreateInfo(*pCreateInfo));
3779 dev_data->bufferMap[*pBuffer].in_use.store(0);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06003780 loader_platform_thread_unlock_mutex(&globalLock);
3781 }
3782 return result;
3783}
3784
Chia-I Wu9ab61502015-11-06 06:42:02 +08003785VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateBufferView(VkDevice device, const VkBufferViewCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkBufferView* pView)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003786{
Tobin Ehlis0b632332015-10-07 09:38:40 -06003787 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08003788 VkResult result = dev_data->device_dispatch_table->CreateBufferView(device, pCreateInfo, pAllocator, pView);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06003789 if (VK_SUCCESS == result) {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003790 loader_platform_thread_lock_mutex(&globalLock);
Chia-I Wue2fc5522015-10-26 20:04:44 +08003791 dev_data->bufferViewMap[*pView] = unique_ptr<VkBufferViewCreateInfo>(new VkBufferViewCreateInfo(*pCreateInfo));
Tobin Ehlisa1c28562015-10-23 16:00:08 -06003792 loader_platform_thread_unlock_mutex(&globalLock);
3793 }
3794 return result;
3795}
3796
Chia-I Wu9ab61502015-11-06 06:42:02 +08003797VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkImage* pImage)
Tobin Ehlisa1c28562015-10-23 16:00:08 -06003798{
3799 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08003800 VkResult result = dev_data->device_dispatch_table->CreateImage(device, pCreateInfo, pAllocator, pImage);
Tobin Ehlisa1c28562015-10-23 16:00:08 -06003801 if (VK_SUCCESS == result) {
Michael Lentineabc5e922015-10-12 11:30:14 -05003802 IMAGE_NODE* image_node = new IMAGE_NODE;
3803 image_node->layout = pCreateInfo->initialLayout;
Tobin Ehlis75cd1c52016-01-21 10:21:04 -07003804 image_node->format = pCreateInfo->format;
Tobin Ehlisa1c28562015-10-23 16:00:08 -06003805 loader_platform_thread_lock_mutex(&globalLock);
Chia-I Wue2fc5522015-10-26 20:04:44 +08003806 dev_data->imageMap[*pImage] = unique_ptr<VkImageCreateInfo>(new VkImageCreateInfo(*pCreateInfo));
Michael Lentineabc5e922015-10-12 11:30:14 -05003807 dev_data->imageLayoutMap[*pImage] = image_node;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003808 loader_platform_thread_unlock_mutex(&globalLock);
3809 }
3810 return result;
3811}
3812
Chia-I Wu9ab61502015-11-06 06:42:02 +08003813VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateImageView(VkDevice device, const VkImageViewCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkImageView* pView)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003814{
Tobin Ehlis0b632332015-10-07 09:38:40 -06003815 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08003816 VkResult result = dev_data->device_dispatch_table->CreateImageView(device, pCreateInfo, pAllocator, pView);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06003817 if (VK_SUCCESS == result) {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003818 loader_platform_thread_lock_mutex(&globalLock);
Chia-I Wue2fc5522015-10-26 20:04:44 +08003819 dev_data->imageViewMap[*pView] = unique_ptr<VkImageViewCreateInfo>(new VkImageViewCreateInfo(*pCreateInfo));
Tobin Ehlis53eddda2015-07-01 16:46:13 -06003820 loader_platform_thread_unlock_mutex(&globalLock);
3821 }
3822 return result;
3823}
3824
Jon Ashburnc669cc62015-07-09 15:02:25 -06003825//TODO handle pipeline caches
Chia-I Wu9ab61502015-11-06 06:42:02 +08003826VKAPI_ATTR VkResult VKAPI_CALL vkCreatePipelineCache(
Jon Ashburnc669cc62015-07-09 15:02:25 -06003827 VkDevice device,
3828 const VkPipelineCacheCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08003829 const VkAllocationCallbacks* pAllocator,
Jon Ashburnc669cc62015-07-09 15:02:25 -06003830 VkPipelineCache* pPipelineCache)
3831{
Tobin Ehlis0b632332015-10-07 09:38:40 -06003832 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08003833 VkResult result = dev_data->device_dispatch_table->CreatePipelineCache(device, pCreateInfo, pAllocator, pPipelineCache);
Jon Ashburnc669cc62015-07-09 15:02:25 -06003834 return result;
3835}
3836
Chia-I Wu9ab61502015-11-06 06:42:02 +08003837VKAPI_ATTR void VKAPI_CALL vkDestroyPipelineCache(
Jon Ashburnc669cc62015-07-09 15:02:25 -06003838 VkDevice device,
Chia-I Wuf7458c52015-10-26 21:10:41 +08003839 VkPipelineCache pipelineCache,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08003840 const VkAllocationCallbacks* pAllocator)
Jon Ashburnc669cc62015-07-09 15:02:25 -06003841{
Tobin Ehlis0b632332015-10-07 09:38:40 -06003842 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08003843 dev_data->device_dispatch_table->DestroyPipelineCache(device, pipelineCache, pAllocator);
Jon Ashburnc669cc62015-07-09 15:02:25 -06003844}
3845
Chia-I Wu9ab61502015-11-06 06:42:02 +08003846VKAPI_ATTR VkResult VKAPI_CALL vkGetPipelineCacheData(
Jon Ashburnc669cc62015-07-09 15:02:25 -06003847 VkDevice device,
3848 VkPipelineCache pipelineCache,
Chia-I Wub16facd2015-10-26 19:17:06 +08003849 size_t* pDataSize,
Jon Ashburnc669cc62015-07-09 15:02:25 -06003850 void* pData)
3851{
Tobin Ehlis0b632332015-10-07 09:38:40 -06003852 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wub16facd2015-10-26 19:17:06 +08003853 VkResult result = dev_data->device_dispatch_table->GetPipelineCacheData(device, pipelineCache, pDataSize, pData);
Jon Ashburnc669cc62015-07-09 15:02:25 -06003854 return result;
3855}
3856
Chia-I Wu9ab61502015-11-06 06:42:02 +08003857VKAPI_ATTR VkResult VKAPI_CALL vkMergePipelineCaches(
Jon Ashburnc669cc62015-07-09 15:02:25 -06003858 VkDevice device,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08003859 VkPipelineCache dstCache,
Jon Ashburnc669cc62015-07-09 15:02:25 -06003860 uint32_t srcCacheCount,
3861 const VkPipelineCache* pSrcCaches)
3862{
Tobin Ehlis0b632332015-10-07 09:38:40 -06003863 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wu3432a0c2015-10-27 18:04:07 +08003864 VkResult result = dev_data->device_dispatch_table->MergePipelineCaches(device, dstCache, srcCacheCount, pSrcCaches);
Jon Ashburnc669cc62015-07-09 15:02:25 -06003865 return result;
3866}
3867
Mark Lobodzinski475a2182015-11-10 15:25:01 -07003868VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateGraphicsPipelines(
3869 VkDevice device,
3870 VkPipelineCache pipelineCache,
3871 uint32_t count,
3872 const VkGraphicsPipelineCreateInfo *pCreateInfos,
3873 const VkAllocationCallbacks *pAllocator,
3874 VkPipeline *pPipelines)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003875{
Courtney Goeltzenleuchtered894072015-09-04 13:39:59 -06003876 VkResult result = VK_SUCCESS;
Tobin Ehlis11efc302015-09-16 10:33:53 -06003877 //TODO What to do with pipelineCache?
Tobin Ehlis75283bf2015-06-18 15:59:33 -06003878 // The order of operations here is a little convoluted but gets the job done
3879 // 1. Pipeline create state is first shadowed into PIPELINE_NODE struct
3880 // 2. Create state is then validated (which uses flags setup during shadowing)
3881 // 3. If everything looks good, we'll then create the pipeline and add NODE to pipelineMap
Tobin Ehlis11efc302015-09-16 10:33:53 -06003882 VkBool32 skipCall = VK_FALSE;
Tobin Ehlis65399772015-09-17 16:33:58 -06003883 // TODO : Improve this data struct w/ unique_ptrs so cleanup below is automatic
3884 vector<PIPELINE_NODE*> pPipeNode(count);
Tobin Ehlis0b632332015-10-07 09:38:40 -06003885 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinski475a2182015-11-10 15:25:01 -07003886
Tobin Ehlis11efc302015-09-16 10:33:53 -06003887 uint32_t i=0;
Tobin Ehlis75283bf2015-06-18 15:59:33 -06003888 loader_platform_thread_lock_mutex(&globalLock);
Mark Lobodzinski475a2182015-11-10 15:25:01 -07003889
Tobin Ehlis11efc302015-09-16 10:33:53 -06003890 for (i=0; i<count; i++) {
Mark Lobodzinski475a2182015-11-10 15:25:01 -07003891 pPipeNode[i] = initGraphicsPipeline(dev_data, &pCreateInfos[i], NULL);
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06003892 skipCall |= verifyPipelineCreateState(dev_data, device, pPipeNode[i]);
Tobin Ehlis11efc302015-09-16 10:33:53 -06003893 }
Mark Lobodzinski475a2182015-11-10 15:25:01 -07003894
Tobin Ehlis75283bf2015-06-18 15:59:33 -06003895 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07003896
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07003897 if (VK_FALSE == skipCall) {
Mark Lobodzinski475a2182015-11-10 15:25:01 -07003898 result = dev_data->device_dispatch_table->CreateGraphicsPipelines(device,
3899 pipelineCache, count, pCreateInfos, pAllocator, pPipelines);
Tobin Ehlis75283bf2015-06-18 15:59:33 -06003900 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis11efc302015-09-16 10:33:53 -06003901 for (i=0; i<count; i++) {
3902 pPipeNode[i]->pipeline = pPipelines[i];
Chia-I Wue2fc5522015-10-26 20:04:44 +08003903 dev_data->pipelineMap[pPipeNode[i]->pipeline] = pPipeNode[i];
Tobin Ehlis11efc302015-09-16 10:33:53 -06003904 }
Tobin Ehlis75283bf2015-06-18 15:59:33 -06003905 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlisce132d82015-06-19 15:07:05 -06003906 } else {
Tobin Ehlis11efc302015-09-16 10:33:53 -06003907 for (i=0; i<count; i++) {
3908 if (pPipeNode[i]) {
3909 // If we allocated a pipeNode, need to clean it up here
3910 delete[] pPipeNode[i]->pVertexBindingDescriptions;
3911 delete[] pPipeNode[i]->pVertexAttributeDescriptions;
3912 delete[] pPipeNode[i]->pAttachments;
3913 delete pPipeNode[i];
3914 }
Tobin Ehlis75283bf2015-06-18 15:59:33 -06003915 }
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07003916 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis75283bf2015-06-18 15:59:33 -06003917 }
Courtney Goeltzenleuchtere2aaad02015-04-13 16:16:04 -06003918 return result;
3919}
3920
Mark Lobodzinski475a2182015-11-10 15:25:01 -07003921VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateComputePipelines(
3922 VkDevice device,
3923 VkPipelineCache pipelineCache,
3924 uint32_t count,
3925 const VkComputePipelineCreateInfo *pCreateInfos,
3926 const VkAllocationCallbacks *pAllocator,
3927 VkPipeline *pPipelines)
3928{
3929 VkResult result = VK_SUCCESS;
3930 VkBool32 skipCall = VK_FALSE;
3931
3932 // TODO : Improve this data struct w/ unique_ptrs so cleanup below is automatic
3933 vector<PIPELINE_NODE*> pPipeNode(count);
3934 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
3935
3936 uint32_t i=0;
3937 loader_platform_thread_lock_mutex(&globalLock);
3938 for (i=0; i<count; i++) {
3939 // TODO: Verify compute stage bits
3940
3941 // Create and initialize internal tracking data structure
3942 pPipeNode[i] = new PIPELINE_NODE;
Mark Lobodzinski475a2182015-11-10 15:25:01 -07003943 memcpy(&pPipeNode[i]->computePipelineCI, (const void*)&pCreateInfos[i], sizeof(VkComputePipelineCreateInfo));
3944
3945 // TODO: Add Compute Pipeline Verification
3946 // skipCall |= verifyPipelineCreateState(dev_data, device, pPipeNode[i]);
3947 }
3948 loader_platform_thread_unlock_mutex(&globalLock);
3949
3950 if (VK_FALSE == skipCall) {
3951 result = dev_data->device_dispatch_table->CreateComputePipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines);
3952 loader_platform_thread_lock_mutex(&globalLock);
3953 for (i=0; i<count; i++) {
3954 pPipeNode[i]->pipeline = pPipelines[i];
3955 dev_data->pipelineMap[pPipeNode[i]->pipeline] = pPipeNode[i];
3956 }
3957 loader_platform_thread_unlock_mutex(&globalLock);
3958 } else {
3959 for (i=0; i<count; i++) {
3960 if (pPipeNode[i]) {
3961 // Clean up any locally allocated data structures
3962 delete pPipeNode[i];
3963 }
3964 }
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07003965 return VK_ERROR_VALIDATION_FAILED_EXT;
Mark Lobodzinski475a2182015-11-10 15:25:01 -07003966 }
3967 return result;
3968}
3969
Chia-I Wu9ab61502015-11-06 06:42:02 +08003970VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkSampler* pSampler)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003971{
Tobin Ehlis0b632332015-10-07 09:38:40 -06003972 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08003973 VkResult result = dev_data->device_dispatch_table->CreateSampler(device, pCreateInfo, pAllocator, pSampler);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06003974 if (VK_SUCCESS == result) {
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003975 loader_platform_thread_lock_mutex(&globalLock);
Chia-I Wue2fc5522015-10-26 20:04:44 +08003976 dev_data->sampleMap[*pSampler] = unique_ptr<SAMPLER_NODE>(new SAMPLER_NODE(pSampler, pCreateInfo));
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003977 loader_platform_thread_unlock_mutex(&globalLock);
3978 }
3979 return result;
3980}
3981
Chia-I Wu9ab61502015-11-06 06:42:02 +08003982VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDescriptorSetLayout(VkDevice device, const VkDescriptorSetLayoutCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDescriptorSetLayout* pSetLayout)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003983{
Tobin Ehlis0b632332015-10-07 09:38:40 -06003984 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08003985 VkResult result = dev_data->device_dispatch_table->CreateDescriptorSetLayout(device, pCreateInfo, pAllocator, pSetLayout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06003986 if (VK_SUCCESS == result) {
Tobin Ehlis8fab6562015-12-01 09:57:09 -07003987 // TODOSC : Capture layout bindings set
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003988 LAYOUT_NODE* pNewNode = new LAYOUT_NODE;
3989 if (NULL == pNewNode) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07003990 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, (uint64_t) *pSetLayout, __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS",
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06003991 "Out of memory while attempting to allocate LAYOUT_NODE in vkCreateDescriptorSetLayout()"))
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07003992 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06003993 }
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06003994 memcpy((void*)&pNewNode->createInfo, pCreateInfo, sizeof(VkDescriptorSetLayoutCreateInfo));
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07003995 pNewNode->createInfo.pBindings = new VkDescriptorSetLayoutBinding[pCreateInfo->bindingCount];
3996 memcpy((void*)pNewNode->createInfo.pBindings, pCreateInfo->pBindings, sizeof(VkDescriptorSetLayoutBinding)*pCreateInfo->bindingCount);
Chia-I Wud46e6ae2015-10-31 00:31:16 +08003997 // g++ does not like reserve with size 0
3998 if (pCreateInfo->bindingCount)
3999 pNewNode->bindings.reserve(pCreateInfo->bindingCount);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004000 uint32_t totalCount = 0;
Chia-I Wud50a7d72015-10-26 20:48:51 +08004001 for (uint32_t i=0; i<pCreateInfo->bindingCount; i++) {
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07004002 if (!pNewNode->bindings.insert(pCreateInfo->pBindings[i].binding).second) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004003 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, (uint64_t) *pSetLayout, __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
Chia-I Wud46e6ae2015-10-31 00:31:16 +08004004 "duplicated binding number in VkDescriptorSetLayoutBinding"))
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07004005 return VK_ERROR_VALIDATION_FAILED_EXT;
Chia-I Wud46e6ae2015-10-31 00:31:16 +08004006 }
4007
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07004008 totalCount += pCreateInfo->pBindings[i].descriptorCount;
4009 if (pCreateInfo->pBindings[i].pImmutableSamplers) {
4010 VkSampler** ppIS = (VkSampler**)&pNewNode->createInfo.pBindings[i].pImmutableSamplers;
4011 *ppIS = new VkSampler[pCreateInfo->pBindings[i].descriptorCount];
4012 memcpy(*ppIS, pCreateInfo->pBindings[i].pImmutableSamplers, pCreateInfo->pBindings[i].descriptorCount*sizeof(VkSampler));
Tobin Ehlis793ad302015-04-03 12:01:11 -06004013 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004014 }
Tobin Ehlis43c39c02016-01-11 13:18:40 -07004015 pNewNode->layout = *pSetLayout;
4016 pNewNode->startIndex = 0;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004017 if (totalCount > 0) {
Cody Northrop43751cc2015-10-26 14:07:35 -06004018 pNewNode->descriptorTypes.resize(totalCount);
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06004019 pNewNode->stageFlags.resize(totalCount);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004020 uint32_t offset = 0;
Tobin Ehlis793ad302015-04-03 12:01:11 -06004021 uint32_t j = 0;
Tobin Ehlisda2f0d02015-11-04 12:28:28 -07004022 VkDescriptorType dType;
Chia-I Wud50a7d72015-10-26 20:48:51 +08004023 for (uint32_t i=0; i<pCreateInfo->bindingCount; i++) {
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07004024 dType = pCreateInfo->pBindings[i].descriptorType;
4025 for (j = 0; j < pCreateInfo->pBindings[i].descriptorCount; j++) {
Tobin Ehlisda2f0d02015-11-04 12:28:28 -07004026 pNewNode->descriptorTypes[offset + j] = dType;
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07004027 pNewNode->stageFlags[offset + j] = pCreateInfo->pBindings[i].stageFlags;
Tobin Ehlisda2f0d02015-11-04 12:28:28 -07004028 if ((dType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
4029 (dType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
4030 pNewNode->dynamicDescriptorCount++;
4031 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004032 }
Tobin Ehlis793ad302015-04-03 12:01:11 -06004033 offset += j;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004034 }
Tobin Ehlis43c39c02016-01-11 13:18:40 -07004035 pNewNode->endIndex = pNewNode->startIndex + totalCount - 1;
4036 } else { // no descriptors
4037 pNewNode->endIndex = 0;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004038 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004039 // Put new node at Head of global Layer list
4040 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07004041 dev_data->descriptorSetLayoutMap[*pSetLayout] = pNewNode;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004042 loader_platform_thread_unlock_mutex(&globalLock);
4043 }
4044 return result;
4045}
4046
Chia-I Wu9ab61502015-11-06 06:42:02 +08004047VKAPI_ATTR VkResult VKAPI_CALL vkCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkPipelineLayout* pPipelineLayout)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004048{
Tobin Ehlis0b632332015-10-07 09:38:40 -06004049 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08004050 VkResult result = dev_data->device_dispatch_table->CreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06004051 if (VK_SUCCESS == result) {
Tobin Ehlis8fab6562015-12-01 09:57:09 -07004052 // TODOSC : Merge capture of the setLayouts per pipeline
Tobin Ehlis559c6382015-11-05 09:52:49 -07004053 PIPELINE_LAYOUT_NODE& plNode = dev_data->pipelineLayoutMap[*pPipelineLayout];
Chia-I Wud50a7d72015-10-26 20:48:51 +08004054 plNode.descriptorSetLayouts.resize(pCreateInfo->setLayoutCount);
Tobin Ehlis644ff042015-10-20 10:11:55 -06004055 uint32_t i = 0;
Chia-I Wud50a7d72015-10-26 20:48:51 +08004056 for (i=0; i<pCreateInfo->setLayoutCount; ++i) {
Tobin Ehlis644ff042015-10-20 10:11:55 -06004057 plNode.descriptorSetLayouts[i] = pCreateInfo->pSetLayouts[i];
4058 }
Cody Northrop43751cc2015-10-26 14:07:35 -06004059 plNode.pushConstantRanges.resize(pCreateInfo->pushConstantRangeCount);
Tobin Ehlis644ff042015-10-20 10:11:55 -06004060 for (i=0; i<pCreateInfo->pushConstantRangeCount; ++i) {
4061 plNode.pushConstantRanges[i] = pCreateInfo->pPushConstantRanges[i];
4062 }
Tobin Ehlis793ad302015-04-03 12:01:11 -06004063 }
4064 return result;
4065}
4066
Chia-I Wu9ab61502015-11-06 06:42:02 +08004067VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDescriptorPool(VkDevice device, const VkDescriptorPoolCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDescriptorPool* pDescriptorPool)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004068{
Tobin Ehlis0b632332015-10-07 09:38:40 -06004069 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08004070 VkResult result = dev_data->device_dispatch_table->CreateDescriptorPool(device, pCreateInfo, pAllocator, pDescriptorPool);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06004071 if (VK_SUCCESS == result) {
Tobin Ehlis793ad302015-04-03 12:01:11 -06004072 // Insert this pool into Global Pool LL at head
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004073 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT, (uint64_t) *pDescriptorPool, __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08004074 "Created Descriptor Pool %#" PRIxLEAST64, (uint64_t) *pDescriptorPool))
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07004075 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004076 loader_platform_thread_lock_mutex(&globalLock);
Mark Lobodzinski39298632015-11-18 08:38:27 -07004077 DESCRIPTOR_POOL_NODE* pNewNode = new DESCRIPTOR_POOL_NODE(*pDescriptorPool, pCreateInfo);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004078 if (NULL == pNewNode) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004079 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT, (uint64_t) *pDescriptorPool, __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS",
Mark Lobodzinski39298632015-11-18 08:38:27 -07004080 "Out of memory while attempting to allocate DESCRIPTOR_POOL_NODE in vkCreateDescriptorPool()"))
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07004081 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlisce132d82015-06-19 15:07:05 -06004082 } else {
Mark Lobodzinski39298632015-11-18 08:38:27 -07004083 dev_data->descriptorPoolMap[*pDescriptorPool] = pNewNode;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004084 }
4085 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlisce132d82015-06-19 15:07:05 -06004086 } else {
Tobin Ehlis793ad302015-04-03 12:01:11 -06004087 // Need to do anything if pool create fails?
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004088 }
4089 return result;
4090}
4091
Chia-I Wu9ab61502015-11-06 06:42:02 +08004092VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, VkDescriptorPoolResetFlags flags)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004093{
Tobin Ehlis0b632332015-10-07 09:38:40 -06004094 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -06004095 VkResult result = dev_data->device_dispatch_table->ResetDescriptorPool(device, descriptorPool, flags);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06004096 if (VK_SUCCESS == result) {
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -06004097 clearDescriptorPool(dev_data, device, descriptorPool, flags);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004098 }
4099 return result;
4100}
4101
Chia-I Wu9ab61502015-11-06 06:42:02 +08004102VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo, VkDescriptorSet* pDescriptorSets)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004103{
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06004104 VkBool32 skipCall = VK_FALSE;
Tobin Ehlis0b632332015-10-07 09:38:40 -06004105 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06004106 // Verify that requested descriptorSets are available in pool
Mark Lobodzinski39298632015-11-18 08:38:27 -07004107 DESCRIPTOR_POOL_NODE *pPoolNode = getPoolNode(dev_data, pAllocateInfo->descriptorPool);
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06004108 if (!pPoolNode) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004109 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT, (uint64_t) pAllocateInfo->descriptorPool, __LINE__, DRAWSTATE_INVALID_POOL, "DS",
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004110 "Unable to find pool node for pool %#" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call", (uint64_t) pAllocateInfo->descriptorPool);
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06004111 } else { // Make sure pool has all the available descriptors before calling down chain
Jon Ashburnf19916e2016-01-11 13:12:43 -07004112 skipCall |= validate_descriptor_availability_in_pool(dev_data, pPoolNode, pAllocateInfo->descriptorSetCount, pAllocateInfo->pSetLayouts);
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06004113 }
4114 if (skipCall)
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07004115 return VK_ERROR_VALIDATION_FAILED_EXT;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004116 VkResult result = dev_data->device_dispatch_table->AllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets);
Cody Northrop1e4f8022015-08-03 12:47:29 -06004117 if (VK_SUCCESS == result) {
Mark Lobodzinski39298632015-11-18 08:38:27 -07004118 DESCRIPTOR_POOL_NODE *pPoolNode = getPoolNode(dev_data, pAllocateInfo->descriptorPool);
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06004119 if (pPoolNode) {
Jon Ashburnf19916e2016-01-11 13:12:43 -07004120 if (pAllocateInfo->descriptorSetCount == 0) {
4121 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, pAllocateInfo->descriptorSetCount, __LINE__, DRAWSTATE_NONE, "DS",
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004122 "AllocateDescriptorSets called with 0 count");
Cody Northrop1e4f8022015-08-03 12:47:29 -06004123 }
Jon Ashburnf19916e2016-01-11 13:12:43 -07004124 for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004125 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pDescriptorSets[i], __LINE__, DRAWSTATE_NONE, "DS",
Chia-I Wue2fc5522015-10-26 20:04:44 +08004126 "Created Descriptor Set %#" PRIxLEAST64, (uint64_t) pDescriptorSets[i]);
Tobin Ehlis793ad302015-04-03 12:01:11 -06004127 // Create new set node and add to head of pool nodes
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004128 SET_NODE* pNewNode = new SET_NODE;
4129 if (NULL == pNewNode) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004130 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pDescriptorSets[i], __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS",
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004131 "Out of memory while attempting to allocate SET_NODE in vkAllocateDescriptorSets()"))
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07004132 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlisce132d82015-06-19 15:07:05 -06004133 } else {
Tobin Ehlis8b5b28c2015-10-19 12:09:13 -06004134 // TODO : Pool should store a total count of each type of Descriptor available
4135 // When descriptors are allocated, decrement the count and validate here
4136 // that the count doesn't go below 0. One reset/free need to bump count back up.
Tobin Ehlis793ad302015-04-03 12:01:11 -06004137 // Insert set at head of Set LL for this pool
4138 pNewNode->pNext = pPoolNode->pSets;
Tobin Ehlis9c4f38d2016-01-14 12:47:19 -07004139 pNewNode->in_use.store(0);
Tobin Ehlis793ad302015-04-03 12:01:11 -06004140 pPoolNode->pSets = pNewNode;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004141 LAYOUT_NODE* pLayout = getLayoutNode(dev_data, pAllocateInfo->pSetLayouts[i]);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004142 if (NULL == pLayout) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004143 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, (uint64_t) pAllocateInfo->pSetLayouts[i], __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004144 "Unable to find set layout node for layout %#" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call", (uint64_t) pAllocateInfo->pSetLayouts[i]))
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07004145 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004146 }
Tobin Ehlis793ad302015-04-03 12:01:11 -06004147 pNewNode->pLayout = pLayout;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004148 pNewNode->pool = pAllocateInfo->descriptorPool;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004149 pNewNode->set = pDescriptorSets[i];
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004150 pNewNode->descriptorCount = pLayout->endIndex + 1;
4151 if (pNewNode->descriptorCount) {
4152 size_t descriptorArraySize = sizeof(GENERIC_HEADER*)*pNewNode->descriptorCount;
4153 pNewNode->ppDescriptors = new GENERIC_HEADER*[descriptorArraySize];
4154 memset(pNewNode->ppDescriptors, 0, descriptorArraySize);
4155 }
Chia-I Wue2fc5522015-10-26 20:04:44 +08004156 dev_data->setMap[pDescriptorSets[i]] = pNewNode;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004157 }
4158 }
4159 }
4160 }
4161 return result;
4162}
4163
Chia-I Wu9ab61502015-11-06 06:42:02 +08004164VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t count, const VkDescriptorSet* pDescriptorSets)
Tony Barbour34ec6922015-07-10 10:50:45 -06004165{
Tobin Ehlise735c692015-10-08 13:13:50 -06004166 VkBool32 skipCall = VK_FALSE;
Tobin Ehlis0b632332015-10-07 09:38:40 -06004167 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Tobin Ehlis9c4f38d2016-01-14 12:47:19 -07004168 // Make sure that no sets being destroyed are in-flight
4169 for (uint32_t i=0; i<count; ++i)
4170 skipCall |= validateIdleDescriptorSet(dev_data, pDescriptorSets[i], "vkFreeDesriptorSets");
Mark Lobodzinski39298632015-11-18 08:38:27 -07004171 DESCRIPTOR_POOL_NODE *pPoolNode = getPoolNode(dev_data, descriptorPool);
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -06004172 if (pPoolNode && !(VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT & pPoolNode->createInfo.flags)) {
4173 // Can't Free from a NON_FREE pool
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004174 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, (uint64_t)device, __LINE__, DRAWSTATE_CANT_FREE_FROM_NON_FREE_POOL, "DS",
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -06004175 "It is invalid to call vkFreeDescriptorSets() with a pool created without setting VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT.");
Tobin Ehlise735c692015-10-08 13:13:50 -06004176 }
Mark Youngb20a6a82016-01-07 15:41:43 -07004177 if (VK_FALSE != skipCall)
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07004178 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis0b632332015-10-07 09:38:40 -06004179 VkResult result = dev_data->device_dispatch_table->FreeDescriptorSets(device, descriptorPool, count, pDescriptorSets);
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06004180 if (VK_SUCCESS == result) {
4181 // For each freed descriptor add it back into the pool as available
4182 for (uint32_t i=0; i<count; ++i) {
Chia-I Wue2fc5522015-10-26 20:04:44 +08004183 SET_NODE* pSet = dev_data->setMap[pDescriptorSets[i]]; // getSetNode() without locking
Tobin Ehlise6e574b2016-01-24 23:25:31 -07004184 invalidateBoundCmdBuffers(dev_data, pSet);
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06004185 LAYOUT_NODE* pLayout = pSet->pLayout;
Chia-I Wu1b99bb22015-10-27 19:25:11 +08004186 uint32_t typeIndex = 0, poolSizeCount = 0;
Chia-I Wud50a7d72015-10-26 20:48:51 +08004187 for (uint32_t j=0; j<pLayout->createInfo.bindingCount; ++j) {
Jon Ashburn6e23c1f2015-12-30 18:01:16 -07004188 typeIndex = static_cast<uint32_t>(pLayout->createInfo.pBindings[j].descriptorType);
4189 poolSizeCount = pLayout->createInfo.pBindings[j].descriptorCount;
Chia-I Wu1b99bb22015-10-27 19:25:11 +08004190 pPoolNode->availableDescriptorTypeCount[typeIndex] += poolSizeCount;
Tobin Ehlisf93f1e32015-10-20 16:16:04 -06004191 }
4192 }
4193 }
4194 // TODO : Any other clean-up or book-keeping to do here?
Tony Barbour34ec6922015-07-10 10:50:45 -06004195 return result;
4196}
4197
Chia-I Wu9ab61502015-11-06 06:42:02 +08004198VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount, const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount, const VkCopyDescriptorSet* pDescriptorCopies)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004199{
Tobin Ehlisa1f9b642015-10-27 12:25:35 -06004200 // dsUpdate will return VK_TRUE only if a bailout error occurs, so we want to call down tree when update returns VK_FALSE
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06004201 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wu40cf0ae2015-10-26 17:20:32 +08004202 if (!dsUpdate(dev_data, device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount, pDescriptorCopies)) {
4203 dev_data->device_dispatch_table->UpdateDescriptorSets(device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount, pDescriptorCopies);
Jon Ashburn3950e1b2015-05-20 09:00:28 -06004204 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004205}
4206
Chia-I Wu9ab61502015-11-06 06:42:02 +08004207VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pCreateInfo, VkCommandBuffer* pCommandBuffer)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004208{
Tobin Ehlis0b632332015-10-07 09:38:40 -06004209 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004210 VkResult result = dev_data->device_dispatch_table->AllocateCommandBuffers(device, pCreateInfo, pCommandBuffer);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06004211 if (VK_SUCCESS == result) {
Jon Ashburnf19916e2016-01-11 13:12:43 -07004212 for (uint32_t i = 0; i < pCreateInfo->commandBufferCount; i++) {
Mark Lobodzinski39298632015-11-18 08:38:27 -07004213 // Validate command pool
4214 if (dev_data->commandPoolMap.find(pCreateInfo->commandPool) != dev_data->commandPoolMap.end()) {
Mark Lobodzinskib8df78c2015-11-20 14:33:48 -07004215 loader_platform_thread_lock_mutex(&globalLock);
Mark Lobodzinski39298632015-11-18 08:38:27 -07004216 // Add command buffer to its commandPool map
Tobin Ehlisac0ef842015-12-14 13:46:38 -07004217 dev_data->commandPoolMap[pCreateInfo->commandPool].commandBuffers.push_back(pCommandBuffer[i]);
Mark Lobodzinski39298632015-11-18 08:38:27 -07004218 GLOBAL_CB_NODE* pCB = new GLOBAL_CB_NODE;
4219 // Add command buffer to map
4220 dev_data->commandBufferMap[pCommandBuffer[i]] = pCB;
4221 resetCB(dev_data, pCommandBuffer[i]);
4222 pCB->commandBuffer = pCommandBuffer[i];
4223 pCB->createInfo = *pCreateInfo;
Mark Lobodzinski941aea92016-01-13 10:23:15 -07004224 pCB->device = device;
Tobin Ehlis74714d22016-01-25 15:24:34 -08004225 loader_platform_thread_unlock_mutex(&globalLock);
Mark Lobodzinski39298632015-11-18 08:38:27 -07004226 }
4227 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004228 }
4229 return result;
4230}
4231
Chia-I Wu9ab61502015-11-06 06:42:02 +08004232VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo* pBeginInfo)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004233{
Mark Youngb20a6a82016-01-07 15:41:43 -07004234 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004235 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Mark Lobodzinskid9139a62015-08-04 16:24:20 -06004236 // Validate command buffer level
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004237 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Mark Lobodzinskid9139a62015-08-04 16:24:20 -06004238 if (pCB) {
Jon Ashburnf19916e2016-01-11 13:12:43 -07004239 if (pCB->createInfo.level != VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
4240 // Secondary Command Buffer
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004241 // TODO : Add check here from spec "If commandBuffer is a secondary command buffer and either the
4242 // occlusionQueryEnable member of pBeginInfo is VK_FALSE, or the precise occlusion queries feature
4243 // is not enabled, the queryFlags member of pBeginInfo must not contain VK_QUERY_CONTROL_PRECISE_BIT"
Jon Ashburnf19916e2016-01-11 13:12:43 -07004244 const VkCommandBufferInheritanceInfo *pInfo = pBeginInfo->pInheritanceInfo;
Tobin Ehlis651d9b02015-12-16 05:01:22 -07004245 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
Jon Ashburnf19916e2016-01-11 13:12:43 -07004246 if (!pInfo->renderPass) { // renderpass should NOT be null for an Secondary CB
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004247 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
Tobin Ehlis651d9b02015-12-16 05:01:22 -07004248 "vkBeginCommandBuffer(): Secondary Command Buffers (%p) must specify a valid renderpass parameter.", (void*)commandBuffer);
4249 }
Jon Ashburnf19916e2016-01-11 13:12:43 -07004250 if (!pInfo->framebuffer) { // framebuffer may be null for an Secondary CB, but this affects perf
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004251 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERF_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
Tobin Ehlis651d9b02015-12-16 05:01:22 -07004252 "vkBeginCommandBuffer(): Secondary Command Buffers (%p) may perform better if a valid framebuffer parameter is specified.", (void*)commandBuffer);
4253 } else {
4254 string errorString = "";
Jon Ashburnf19916e2016-01-11 13:12:43 -07004255 VkRenderPass fbRP = dev_data->frameBufferMap[pInfo->framebuffer]->renderPass;
4256 if (!verify_renderpass_compatibility(dev_data, fbRP, pInfo->renderPass, errorString)) {
Tobin Ehlis651d9b02015-12-16 05:01:22 -07004257 // renderPass that framebuffer was created with must be compatible with local renderPass
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004258 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__, DRAWSTATE_RENDERPASS_INCOMPATIBLE, "DS",
Tobin Ehlis651d9b02015-12-16 05:01:22 -07004259 "vkBeginCommandBuffer(): Secondary Command Buffer (%p) renderPass (%#" PRIxLEAST64 ") is incompatible w/ framebuffer (%#" PRIxLEAST64 ") w/ render pass (%#" PRIxLEAST64 ") due to: %s",
Jon Ashburnf19916e2016-01-11 13:12:43 -07004260 (void*)commandBuffer, (uint64_t)pInfo->renderPass, (uint64_t)pInfo->framebuffer, (uint64_t)fbRP, errorString.c_str());
Tobin Ehlis651d9b02015-12-16 05:01:22 -07004261 }
4262 }
Mark Lobodzinskid9139a62015-08-04 16:24:20 -06004263 }
4264 }
Tobin Ehlisac0ef842015-12-14 13:46:38 -07004265 if (CB_RECORDING == pCB->state) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004266 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
Tobin Ehlisac0ef842015-12-14 13:46:38 -07004267 "vkBeginCommandBuffer(): Cannot call Begin on CB (%#" PRIxLEAST64 ") in the RECORDING state. Must first call vkEndCommandBuffer().", (uint64_t)commandBuffer);
4268 } else if (CB_RECORDED == pCB->state) {
4269 VkCommandPool cmdPool = pCB->createInfo.commandPool;
4270 if (!(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT & dev_data->commandPoolMap[cmdPool].createFlags)) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07004271 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer,
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004272 __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER_RESET, "DS",
Tobin Ehlisac0ef842015-12-14 13:46:38 -07004273 "Call to vkBeginCommandBuffer() on command buffer (%#" PRIxLEAST64 ") attempts to implicitly reset cmdBuffer created from command pool (%#" PRIxLEAST64 ") that does NOT have the VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT bit set.",
4274 (uint64_t) commandBuffer, (uint64_t) cmdPool);
4275 }
Tobin Ehlis74714d22016-01-25 15:24:34 -08004276 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlisef694652016-01-19 12:03:34 -07004277 resetCB(dev_data, commandBuffer);
Tobin Ehlis74714d22016-01-25 15:24:34 -08004278 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlisac0ef842015-12-14 13:46:38 -07004279 }
Tobin Ehlisef694652016-01-19 12:03:34 -07004280 // Set updated state here in case implicit reset occurs above
Tobin Ehlis74714d22016-01-25 15:24:34 -08004281 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlisef694652016-01-19 12:03:34 -07004282 pCB->state = CB_RECORDING;
4283 pCB->beginInfo = *pBeginInfo;
Tobin Ehliscd5bfd82016-01-19 13:12:52 -07004284 if (pCB->beginInfo.pInheritanceInfo) {
4285 pCB->inheritanceInfo = *(pCB->beginInfo.pInheritanceInfo);
4286 pCB->beginInfo.pInheritanceInfo = &pCB->inheritanceInfo;
4287 }
Tobin Ehlis74714d22016-01-25 15:24:34 -08004288 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlis59278bf2015-08-18 07:10:58 -06004289 } else {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004290 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004291 "In vkBeginCommandBuffer() and unable to find CommandBuffer Node for CB %p!", (void*)commandBuffer);
Mark Lobodzinskid9139a62015-08-04 16:24:20 -06004292 }
Mark Youngb20a6a82016-01-07 15:41:43 -07004293 if (VK_FALSE != skipCall) {
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07004294 return VK_ERROR_VALIDATION_FAILED_EXT;
Courtney Goeltzenleuchter5fe086f2015-09-04 15:03:52 -06004295 }
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004296 VkResult result = dev_data->device_dispatch_table->BeginCommandBuffer(commandBuffer, pBeginInfo);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004297 return result;
4298}
4299
Chia-I Wu9ab61502015-11-06 06:42:02 +08004300VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEndCommandBuffer(VkCommandBuffer commandBuffer)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004301{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004302 VkBool32 skipCall = VK_FALSE;
Courtney Goeltzenleuchtered894072015-09-04 13:39:59 -06004303 VkResult result = VK_SUCCESS;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004304 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4305 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis9c536442015-06-19 13:00:59 -06004306 if (pCB) {
Tobin Ehlisac0ef842015-12-14 13:46:38 -07004307 if (pCB->state != CB_RECORDING) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004308 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkEndCommandBuffer()");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004309 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004310 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004311 if (VK_FALSE == skipCall) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004312 result = dev_data->device_dispatch_table->EndCommandBuffer(commandBuffer);
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004313 if (VK_SUCCESS == result) {
Tobin Ehlisac0ef842015-12-14 13:46:38 -07004314 pCB->state = CB_RECORDED;
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004315 // Reset CB status flags
4316 pCB->status = 0;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004317 printCB(dev_data, commandBuffer);
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004318 }
4319 } else {
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07004320 result = VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004321 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004322 return result;
4323}
4324
Chia-I Wu9ab61502015-11-06 06:42:02 +08004325VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkResetCommandBuffer(VkCommandBuffer commandBuffer, VkCommandBufferResetFlags flags)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004326{
Tobin Ehlisac0ef842015-12-14 13:46:38 -07004327 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004328 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Tobin Ehlisac0ef842015-12-14 13:46:38 -07004329 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
4330 VkCommandPool cmdPool = pCB->createInfo.commandPool;
4331 if (!(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT & dev_data->commandPoolMap[cmdPool].createFlags)) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07004332 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t) commandBuffer,
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004333 __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER_RESET, "DS",
Tobin Ehlisac0ef842015-12-14 13:46:38 -07004334 "Attempt to reset command buffer (%#" PRIxLEAST64 ") created from command pool (%#" PRIxLEAST64 ") that does NOT have the VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT bit set.",
4335 (uint64_t) commandBuffer, (uint64_t) cmdPool);
4336 }
Mark Youngb20a6a82016-01-07 15:41:43 -07004337 if (skipCall != VK_FALSE)
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07004338 return VK_ERROR_VALIDATION_FAILED_EXT;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004339 VkResult result = dev_data->device_dispatch_table->ResetCommandBuffer(commandBuffer, flags);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06004340 if (VK_SUCCESS == result) {
Tobin Ehlis74714d22016-01-25 15:24:34 -08004341 loader_platform_thread_lock_mutex(&globalLock);
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004342 resetCB(dev_data, commandBuffer);
Tobin Ehlis74714d22016-01-25 15:24:34 -08004343 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004344 }
4345 return result;
4346}
4347
Chia-I Wu9ab61502015-11-06 06:42:02 +08004348VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004349{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004350 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004351 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4352 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004353 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004354 skipCall |= addCmd(dev_data, pCB, CMD_BINDPIPELINE, "vkCmdBindPipeline()");
4355 if ((VK_PIPELINE_BIND_POINT_COMPUTE == pipelineBindPoint) && (pCB->activeRenderPass)) {
4356 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, (uint64_t) pipeline,
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004357 __LINE__, DRAWSTATE_INVALID_RENDERPASS_CMD, "DS",
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004358 "Incorrectly binding compute pipeline (%#" PRIxLEAST64 ") during active RenderPass (%#" PRIxLEAST64 ")",
4359 (uint64_t) pipeline, (uint64_t) pCB->activeRenderPass);
4360 } else if (VK_PIPELINE_BIND_POINT_GRAPHICS == pipelineBindPoint) {
4361 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdBindPipeline");
4362 }
4363
4364 PIPELINE_NODE* pPN = getPipeline(dev_data, pipeline);
4365 if (pPN) {
4366 pCB->lastBoundPipeline = pipeline;
4367 loader_platform_thread_lock_mutex(&globalLock);
4368 set_cb_pso_status(pCB, pPN);
4369 loader_platform_thread_unlock_mutex(&globalLock);
4370 skipCall |= validatePipelineState(dev_data, pCB, pipelineBindPoint, pipeline);
Tobin Ehlisce132d82015-06-19 15:07:05 -06004371 } else {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004372 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004373 (uint64_t) pipeline, __LINE__, DRAWSTATE_INVALID_PIPELINE, "DS",
Mark Young93ecb1d2016-01-13 13:47:16 -07004374 "Attempt to bind Pipeline %#" PRIxLEAST64 " that doesn't exist!", (uint64_t)(pipeline));
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004375 }
4376 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004377 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004378 dev_data->device_dispatch_table->CmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004379}
4380
Chia-I Wu9ab61502015-11-06 06:42:02 +08004381VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetViewport(
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004382 VkCommandBuffer commandBuffer,
Jon Ashburn19d3bf12015-12-30 14:06:55 -07004383 uint32_t firstViewport,
4384 uint32_t viewportCount,
4385 const VkViewport* pViewports)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004386{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004387 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004388 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4389 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis9c536442015-06-19 13:00:59 -06004390 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004391 skipCall |= addCmd(dev_data, pCB, CMD_SETVIEWPORTSTATE, "vkCmdSetViewport()");
4392 loader_platform_thread_lock_mutex(&globalLock);
4393 pCB->status |= CBSTATUS_VIEWPORT_SET;
4394 pCB->viewports.resize(viewportCount);
4395 memcpy(pCB->viewports.data(), pViewports, viewportCount * sizeof(VkViewport));
4396 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehlis9c536442015-06-19 13:00:59 -06004397 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004398 if (VK_FALSE == skipCall)
Jon Ashburn19d3bf12015-12-30 14:06:55 -07004399 dev_data->device_dispatch_table->CmdSetViewport(commandBuffer, firstViewport, viewportCount, pViewports);
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -06004400}
4401
Chia-I Wu9ab61502015-11-06 06:42:02 +08004402VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetScissor(
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004403 VkCommandBuffer commandBuffer,
Jon Ashburn19d3bf12015-12-30 14:06:55 -07004404 uint32_t firstScissor,
4405 uint32_t scissorCount,
4406 const VkRect2D* pScissors)
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -06004407{
4408 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004409 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4410 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -06004411 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004412 skipCall |= addCmd(dev_data, pCB, CMD_SETSCISSORSTATE, "vkCmdSetScissor()");
4413 loader_platform_thread_lock_mutex(&globalLock);
4414 pCB->status |= CBSTATUS_SCISSOR_SET;
4415 pCB->scissors.resize(scissorCount);
4416 memcpy(pCB->scissors.data(), pScissors, scissorCount * sizeof(VkRect2D));
4417 loader_platform_thread_unlock_mutex(&globalLock);
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -06004418 }
4419 if (VK_FALSE == skipCall)
Jon Ashburn19d3bf12015-12-30 14:06:55 -07004420 dev_data->device_dispatch_table->CmdSetScissor(commandBuffer, firstScissor, scissorCount, pScissors);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004421}
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004422
Chia-I Wu9ab61502015-11-06 06:42:02 +08004423VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetLineWidth(VkCommandBuffer commandBuffer, float lineWidth)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004424{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004425 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004426 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4427 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004428 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004429 skipCall |= addCmd(dev_data, pCB, CMD_SETLINEWIDTHSTATE, "vkCmdSetLineWidth()");
4430 /* TODO: Do we still need this lock? */
4431 loader_platform_thread_lock_mutex(&globalLock);
4432 pCB->status |= CBSTATUS_LINE_WIDTH_SET;
4433 pCB->lineWidth = lineWidth;
4434 loader_platform_thread_unlock_mutex(&globalLock);
Cody Northrop12365112015-08-17 11:10:49 -06004435 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004436 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004437 dev_data->device_dispatch_table->CmdSetLineWidth(commandBuffer, lineWidth);
Cody Northrop12365112015-08-17 11:10:49 -06004438}
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004439
Chia-I Wu9ab61502015-11-06 06:42:02 +08004440VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetDepthBias(
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004441 VkCommandBuffer commandBuffer,
Chia-I Wud8c946a2015-10-26 19:08:09 +08004442 float depthBiasConstantFactor,
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004443 float depthBiasClamp,
Chia-I Wud8c946a2015-10-26 19:08:09 +08004444 float depthBiasSlopeFactor)
Cody Northrop12365112015-08-17 11:10:49 -06004445{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004446 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004447 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4448 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Cody Northrop12365112015-08-17 11:10:49 -06004449 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004450 skipCall |= addCmd(dev_data, pCB, CMD_SETDEPTHBIASSTATE, "vkCmdSetDepthBias()");
4451 pCB->status |= CBSTATUS_DEPTH_BIAS_SET;
4452 pCB->depthBiasConstantFactor = depthBiasConstantFactor;
4453 pCB->depthBiasClamp = depthBiasClamp;
4454 pCB->depthBiasSlopeFactor = depthBiasSlopeFactor;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004455 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004456 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004457 dev_data->device_dispatch_table->CmdSetDepthBias(commandBuffer, depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004458}
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004459
Chia-I Wu9ab61502015-11-06 06:42:02 +08004460VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetBlendConstants(VkCommandBuffer commandBuffer, const float blendConstants[4])
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004461{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004462 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004463 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4464 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004465 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004466 skipCall |= addCmd(dev_data, pCB, CMD_SETBLENDSTATE, "vkCmdSetBlendConstants()");
4467 pCB->status |= CBSTATUS_BLEND_SET;
4468 memcpy(pCB->blendConstants, blendConstants, 4 * sizeof(float));
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004469 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004470 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004471 dev_data->device_dispatch_table->CmdSetBlendConstants(commandBuffer, blendConstants);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004472}
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004473
Chia-I Wu9ab61502015-11-06 06:42:02 +08004474VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetDepthBounds(
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004475 VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004476 float minDepthBounds,
4477 float maxDepthBounds)
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004478{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004479 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004480 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4481 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004482 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004483 skipCall |= addCmd(dev_data, pCB, CMD_SETDEPTHBOUNDSSTATE, "vkCmdSetDepthBounds()");
4484 pCB->status |= CBSTATUS_DEPTH_BOUNDS_SET;
4485 pCB->minDepthBounds = minDepthBounds;
4486 pCB->maxDepthBounds = maxDepthBounds;
Cody Northrop82485a82015-08-18 15:21:16 -06004487 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004488 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004489 dev_data->device_dispatch_table->CmdSetDepthBounds(commandBuffer, minDepthBounds, maxDepthBounds);
Cody Northrop82485a82015-08-18 15:21:16 -06004490}
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004491
Chia-I Wu9ab61502015-11-06 06:42:02 +08004492VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetStencilCompareMask(
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004493 VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004494 VkStencilFaceFlags faceMask,
Chia-I Wu1b99bb22015-10-27 19:25:11 +08004495 uint32_t compareMask)
Cody Northrop82485a82015-08-18 15:21:16 -06004496{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004497 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004498 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4499 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Cody Northrop82485a82015-08-18 15:21:16 -06004500 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004501 skipCall |= addCmd(dev_data, pCB, CMD_SETSTENCILREADMASKSTATE, "vkCmdSetStencilCompareMask()");
4502 if (faceMask & VK_STENCIL_FACE_FRONT_BIT) {
4503 pCB->front.compareMask = compareMask;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004504 }
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004505 if (faceMask & VK_STENCIL_FACE_BACK_BIT) {
4506 pCB->back.compareMask = compareMask;
4507 }
4508 /* TODO: Do we need to track front and back separately? */
4509 /* TODO: We aren't capturing the faceMask, do we need to? */
4510 pCB->status |= CBSTATUS_STENCIL_READ_MASK_SET;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004511 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004512 if (VK_FALSE == skipCall)
Chia-I Wu1b99bb22015-10-27 19:25:11 +08004513 dev_data->device_dispatch_table->CmdSetStencilCompareMask(commandBuffer, faceMask, compareMask);
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06004514}
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004515
Chia-I Wu9ab61502015-11-06 06:42:02 +08004516VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetStencilWriteMask(
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004517 VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004518 VkStencilFaceFlags faceMask,
Chia-I Wu1b99bb22015-10-27 19:25:11 +08004519 uint32_t writeMask)
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004520{
4521 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004522 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4523 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004524 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004525 skipCall |= addCmd(dev_data, pCB, CMD_SETSTENCILWRITEMASKSTATE, "vkCmdSetStencilWriteMask()");
4526 if (faceMask & VK_STENCIL_FACE_FRONT_BIT) {
4527 pCB->front.writeMask = writeMask;
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004528 }
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004529 if (faceMask & VK_STENCIL_FACE_BACK_BIT) {
4530 pCB->back.writeMask = writeMask;
4531 }
4532 pCB->status |= CBSTATUS_STENCIL_WRITE_MASK_SET;
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004533 }
4534 if (VK_FALSE == skipCall)
Chia-I Wu1b99bb22015-10-27 19:25:11 +08004535 dev_data->device_dispatch_table->CmdSetStencilWriteMask(commandBuffer, faceMask, writeMask);
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004536}
4537
Chia-I Wu9ab61502015-11-06 06:42:02 +08004538VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetStencilReference(
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004539 VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004540 VkStencilFaceFlags faceMask,
Chia-I Wu1b99bb22015-10-27 19:25:11 +08004541 uint32_t reference)
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004542{
4543 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004544 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4545 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004546 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004547 skipCall |= addCmd(dev_data, pCB, CMD_SETSTENCILREFERENCESTATE, "vkCmdSetStencilReference()");
4548 if (faceMask & VK_STENCIL_FACE_FRONT_BIT) {
4549 pCB->front.reference = reference;
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004550 }
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004551 if (faceMask & VK_STENCIL_FACE_BACK_BIT) {
4552 pCB->back.reference = reference;
4553 }
4554 pCB->status |= CBSTATUS_STENCIL_REFERENCE_SET;
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004555 }
4556 if (VK_FALSE == skipCall)
Chia-I Wu1b99bb22015-10-27 19:25:11 +08004557 dev_data->device_dispatch_table->CmdSetStencilReference(commandBuffer, faceMask, reference);
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06004558}
4559
Chia-I Wu9ab61502015-11-06 06:42:02 +08004560VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdBindDescriptorSets(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout, uint32_t firstSet, uint32_t setCount, const VkDescriptorSet* pDescriptorSets, uint32_t dynamicOffsetCount, const uint32_t* pDynamicOffsets)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004561{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004562 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004563 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4564 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004565 if (pCB) {
Tobin Ehlisf6585052015-12-17 11:48:42 -07004566 if (pCB->state == CB_RECORDING) {
4567 if ((VK_PIPELINE_BIND_POINT_COMPUTE == pipelineBindPoint) && (pCB->activeRenderPass)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004568 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_RENDERPASS_CMD, "DS",
Tobin Ehlisf6585052015-12-17 11:48:42 -07004569 "Incorrectly binding compute DescriptorSets during active RenderPass (%#" PRIxLEAST64 ")", (uint64_t) pCB->activeRenderPass);
4570 } else if (VK_PIPELINE_BIND_POINT_GRAPHICS == pipelineBindPoint) {
4571 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdBindDescriptorSets");
Mark Lobodzinski74635932015-12-18 15:35:38 -07004572 }
Tobin Ehlisf6585052015-12-17 11:48:42 -07004573 if (VK_FALSE == skipCall) {
4574 // Track total count of dynamic descriptor types to make sure we have an offset for each one
4575 uint32_t totalDynamicDescriptors = 0;
4576 string errorString = "";
4577 uint32_t lastSetIndex = firstSet+setCount-1;
4578 if (lastSetIndex >= pCB->boundDescriptorSets.size())
Tobin Ehlis559c6382015-11-05 09:52:49 -07004579 pCB->boundDescriptorSets.resize(lastSetIndex+1);
Tobin Ehlisf6585052015-12-17 11:48:42 -07004580 VkDescriptorSet oldFinalBoundSet = pCB->boundDescriptorSets[lastSetIndex];
4581 for (uint32_t i=0; i<setCount; i++) {
4582 SET_NODE* pSet = getSetNode(dev_data, pDescriptorSets[i]);
4583 if (pSet) {
4584 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlise6e574b2016-01-24 23:25:31 -07004585 pCB->uniqueBoundSets.insert(pDescriptorSets[i]);
4586 pSet->boundCmdBuffers.insert(commandBuffer);
Tobin Ehlisf6585052015-12-17 11:48:42 -07004587 pCB->lastBoundDescriptorSet = pDescriptorSets[i];
4588 pCB->lastBoundPipelineLayout = layout;
4589 pCB->boundDescriptorSets[i+firstSet] = pDescriptorSets[i];
4590 loader_platform_thread_unlock_mutex(&globalLock);
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004591 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pDescriptorSets[i], __LINE__, DRAWSTATE_NONE, "DS",
Tobin Ehlisf6585052015-12-17 11:48:42 -07004592 "DS %#" PRIxLEAST64 " bound on pipeline %s", (uint64_t) pDescriptorSets[i], string_VkPipelineBindPoint(pipelineBindPoint));
Tobin Ehlis43c39c02016-01-11 13:18:40 -07004593 if (!pSet->pUpdateStructs && (pSet->descriptorCount != 0)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004594 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pDescriptorSets[i], __LINE__, DRAWSTATE_DESCRIPTOR_SET_NOT_UPDATED, "DS",
Tobin Ehlisf6585052015-12-17 11:48:42 -07004595 "DS %#" PRIxLEAST64 " bound but it was never updated. You may want to either update it or not bind it.", (uint64_t) pDescriptorSets[i]);
4596 }
4597 // Verify that set being bound is compatible with overlapping setLayout of pipelineLayout
4598 if (!verify_set_layout_compatibility(dev_data, pSet, layout, i+firstSet, errorString)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004599 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pDescriptorSets[i], __LINE__, DRAWSTATE_PIPELINE_LAYOUTS_INCOMPATIBLE, "DS",
Tobin Ehlisf6585052015-12-17 11:48:42 -07004600 "descriptorSet #%u being bound is not compatible with overlapping layout in pipelineLayout due to: %s", i, errorString.c_str());
4601 }
4602 if (pSet->pLayout->dynamicDescriptorCount) {
4603 // First make sure we won't overstep bounds of pDynamicOffsets array
4604 if ((totalDynamicDescriptors + pSet->pLayout->dynamicDescriptorCount) > dynamicOffsetCount) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004605 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pDescriptorSets[i], __LINE__, DRAWSTATE_INVALID_DYNAMIC_OFFSET_COUNT, "DS",
Tobin Ehlisf6585052015-12-17 11:48:42 -07004606 "descriptorSet #%u (%#" PRIxLEAST64 ") requires %u dynamicOffsets, but only %u dynamicOffsets are left in pDynamicOffsets array. There must be one dynamic offset for each dynamic descriptor being bound.",
4607 i, (uint64_t) pDescriptorSets[i], pSet->pLayout->dynamicDescriptorCount, (dynamicOffsetCount - totalDynamicDescriptors));
Mark Lobodzinski941aea92016-01-13 10:23:15 -07004608 } else { // Validate and store dynamic offsets with the set
4609 // Validate Dynamic Offset Minimums
4610 uint32_t cur_dyn_offset = totalDynamicDescriptors;
4611 for (uint32_t d = 0; d < pSet->descriptorCount; d++) {
4612 if (pSet->pLayout->descriptorTypes[i] == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
4613 if (vk_safe_modulo(pDynamicOffsets[cur_dyn_offset], dev_data->physDevPropertyMap[pCB->device].limits.minUniformBufferOffsetAlignment) != 0) {
4614 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0,
4615 __LINE__, DRAWSTATE_INVALID_UNIFORM_BUFFER_OFFSET, "DS",
4616 "vkCmdBindDescriptorSets(): pDynamicOffsets[%d] is %d but must be a multiple of device limit minUniformBufferOffsetAlignment %#" PRIxLEAST64,
4617 cur_dyn_offset, pDynamicOffsets[cur_dyn_offset], dev_data->physDevPropertyMap[pCB->device].limits.minUniformBufferOffsetAlignment);
4618 }
4619 cur_dyn_offset++;
4620 } else if (pSet->pLayout->descriptorTypes[i] == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
4621 if (vk_safe_modulo(pDynamicOffsets[cur_dyn_offset], dev_data->physDevPropertyMap[pCB->device].limits.minStorageBufferOffsetAlignment) != 0) {
4622 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0,
4623 __LINE__, DRAWSTATE_INVALID_STORAGE_BUFFER_OFFSET, "DS",
4624 "vkCmdBindDescriptorSets(): pDynamicOffsets[%d] is %d but must be a multiple of device limit minStorageBufferOffsetAlignment %#" PRIxLEAST64,
4625 cur_dyn_offset, pDynamicOffsets[cur_dyn_offset], dev_data->physDevPropertyMap[pCB->device].limits.minStorageBufferOffsetAlignment);
4626 }
4627 cur_dyn_offset++;
4628 }
4629 }
4630 // Store offsets
Tobin Ehlisf6585052015-12-17 11:48:42 -07004631 const uint32_t* pStartDynOffset = pDynamicOffsets + totalDynamicDescriptors;
4632 pSet->dynamicOffsets.assign(pStartDynOffset, pStartDynOffset + pSet->pLayout->dynamicDescriptorCount);
4633 totalDynamicDescriptors += pSet->pLayout->dynamicDescriptorCount;
4634 }
4635 }
4636 } else {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004637 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pDescriptorSets[i], __LINE__, DRAWSTATE_INVALID_SET, "DS",
Tobin Ehlisf6585052015-12-17 11:48:42 -07004638 "Attempt to bind DS %#" PRIxLEAST64 " that doesn't exist!", (uint64_t) pDescriptorSets[i]);
4639 }
4640 }
4641 skipCall |= addCmd(dev_data, pCB, CMD_BINDDESCRIPTORSETS, "vkCmdBindDescrsiptorSets()");
4642 // For any previously bound sets, need to set them to "invalid" if they were disturbed by this update
4643 if (firstSet > 0) { // Check set #s below the first bound set
4644 for (uint32_t i=0; i<firstSet; ++i) {
4645 if (pCB->boundDescriptorSets[i] && !verify_set_layout_compatibility(dev_data, dev_data->setMap[pCB->boundDescriptorSets[i]], layout, i, errorString)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004646 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERF_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) pCB->boundDescriptorSets[i], __LINE__, DRAWSTATE_NONE, "DS",
Tobin Ehlisf6585052015-12-17 11:48:42 -07004647 "DescriptorSetDS %#" PRIxLEAST64 " previously bound as set #%u was disturbed by newly bound pipelineLayout (%#" PRIxLEAST64 ")", (uint64_t) pCB->boundDescriptorSets[i], i, (uint64_t) layout);
4648 pCB->boundDescriptorSets[i] = VK_NULL_HANDLE;
4649 }
4650 }
4651 }
4652 // Check if newly last bound set invalidates any remaining bound sets
4653 if ((pCB->boundDescriptorSets.size()-1) > (lastSetIndex)) {
4654 if (oldFinalBoundSet && !verify_set_layout_compatibility(dev_data, dev_data->setMap[oldFinalBoundSet], layout, lastSetIndex, errorString)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004655 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERF_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t) oldFinalBoundSet, __LINE__, DRAWSTATE_NONE, "DS",
Tobin Ehlisf6585052015-12-17 11:48:42 -07004656 "DescriptorSetDS %#" PRIxLEAST64 " previously bound as set #%u is incompatible with set %#" PRIxLEAST64 " newly bound as set #%u so set #%u and any subsequent sets were disturbed by newly bound pipelineLayout (%#" PRIxLEAST64 ")", (uint64_t) oldFinalBoundSet, lastSetIndex, (uint64_t) pCB->boundDescriptorSets[lastSetIndex], lastSetIndex, lastSetIndex+1, (uint64_t) layout);
4657 pCB->boundDescriptorSets.resize(lastSetIndex+1);
4658 }
4659 }
4660 // dynamicOffsetCount must equal the total number of dynamic descriptors in the sets being bound
4661 if (totalDynamicDescriptors != dynamicOffsetCount) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004662 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t) commandBuffer, __LINE__, DRAWSTATE_INVALID_DYNAMIC_OFFSET_COUNT, "DS",
Tobin Ehlisf6585052015-12-17 11:48:42 -07004663 "Attempting to bind %u descriptorSets with %u dynamic descriptors, but dynamicOffsetCount is %u. It should exactly match the number of dynamic descriptors.", setCount, totalDynamicDescriptors, dynamicOffsetCount);
Tobin Ehlisda2f0d02015-11-04 12:28:28 -07004664 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004665 }
Tobin Ehlisf6585052015-12-17 11:48:42 -07004666 } else {
4667 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdBindDescriptorSets()");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004668 }
4669 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004670 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004671 dev_data->device_dispatch_table->CmdBindDescriptorSets(commandBuffer, pipelineBindPoint, layout, firstSet, setCount, pDescriptorSets, dynamicOffsetCount, pDynamicOffsets);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004672}
4673
Chia-I Wu9ab61502015-11-06 06:42:02 +08004674VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdBindIndexBuffer(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004675{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004676 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004677 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4678 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004679 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004680 skipCall |= addCmd(dev_data, pCB, CMD_BINDINDEXBUFFER, "vkCmdBindIndexBuffer()");
4681 VkDeviceSize offset_align = 0;
4682 switch (indexType) {
4683 case VK_INDEX_TYPE_UINT16:
4684 offset_align = 2;
4685 break;
4686 case VK_INDEX_TYPE_UINT32:
4687 offset_align = 4;
4688 break;
4689 default:
4690 // ParamChecker should catch bad enum, we'll also throw alignment error below if offset_align stays 0
4691 break;
4692 }
4693 if (!offset_align || (offset % offset_align)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004694 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_VTX_INDEX_ALIGNMENT_ERROR, "DS",
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004695 "vkCmdBindIndexBuffer() offset (%#" PRIxLEAST64 ") does not fall on alignment (%s) boundary.", offset, string_VkIndexType(indexType));
Tobin Ehlis9c536442015-06-19 13:00:59 -06004696 }
Tobin Ehlisc4c23182015-09-17 12:24:13 -06004697 pCB->status |= CBSTATUS_INDEX_BUFFER_BOUND;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004698 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004699 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004700 dev_data->device_dispatch_table->CmdBindIndexBuffer(commandBuffer, buffer, offset, indexType);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004701}
4702
Mark Lobodzinski8da0d1e2016-01-06 14:58:59 -07004703void updateResourceTracking(GLOBAL_CB_NODE* pCB, uint32_t firstBinding, uint32_t bindingCount, const VkBuffer* pBuffers) {
4704 uint32_t end = firstBinding + bindingCount;
Michael Lentine700b0aa2015-10-30 17:57:32 -07004705 if (pCB->currentDrawData.buffers.size() < end) {
4706 pCB->currentDrawData.buffers.resize(end);
4707 }
4708 for (uint32_t i = 0; i < bindingCount; ++i) {
Mark Lobodzinski8da0d1e2016-01-06 14:58:59 -07004709 pCB->currentDrawData.buffers[i + firstBinding] = pBuffers[i];
Michael Lentine700b0aa2015-10-30 17:57:32 -07004710 }
4711}
4712
4713void updateResourceTrackingOnDraw(GLOBAL_CB_NODE* pCB) {
4714 pCB->drawData.push_back(pCB->currentDrawData);
4715}
4716
Chia-I Wu9ab61502015-11-06 06:42:02 +08004717VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdBindVertexBuffers(
Mark Lobodzinskidfcd9b62015-12-14 15:14:10 -07004718 VkCommandBuffer commandBuffer,
Jon Ashburn19d3bf12015-12-30 14:06:55 -07004719 uint32_t firstBinding,
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06004720 uint32_t bindingCount,
Mark Lobodzinskidfcd9b62015-12-14 15:14:10 -07004721 const VkBuffer *pBuffers,
4722 const VkDeviceSize *pOffsets)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004723{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004724 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004725 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4726 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004727 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004728 addCmd(dev_data, pCB, CMD_BINDVERTEXBUFFER, "vkCmdBindVertexBuffer()");
Mark Lobodzinski8da0d1e2016-01-06 14:58:59 -07004729 updateResourceTracking(pCB, firstBinding, bindingCount, pBuffers);
Michael Lentine700b0aa2015-10-30 17:57:32 -07004730 } else {
Mark Youngad779052016-01-06 14:26:04 -07004731 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdBindVertexBuffer()");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004732 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004733 if (VK_FALSE == skipCall)
Jon Ashburn19d3bf12015-12-30 14:06:55 -07004734 dev_data->device_dispatch_table->CmdBindVertexBuffers(commandBuffer, firstBinding, bindingCount, pBuffers, pOffsets);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004735}
4736
Chia-I Wu9ab61502015-11-06 06:42:02 +08004737VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004738{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004739 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004740 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4741 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004742 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004743 skipCall |= addCmd(dev_data, pCB, CMD_DRAW, "vkCmdDraw()");
4744 pCB->drawCount[DRAW]++;
4745 skipCall |= validate_draw_state(dev_data, pCB, VK_FALSE);
4746 // TODO : Need to pass commandBuffer as srcObj here
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004747 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_NONE, "DS",
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004748 "vkCmdDraw() call #%" PRIu64 ", reporting DS state:", g_drawCount[DRAW]++);
4749 skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
Michael Lentine700b0aa2015-10-30 17:57:32 -07004750 if (VK_FALSE == skipCall) {
4751 updateResourceTrackingOnDraw(pCB);
4752 }
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06004753 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDraw");
Jon Ashburn3950e1b2015-05-20 09:00:28 -06004754 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004755 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004756 dev_data->device_dispatch_table->CmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004757}
4758
Chia-I Wu9ab61502015-11-06 06:42:02 +08004759VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004760{
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004761 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4762 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004763 VkBool32 skipCall = VK_FALSE;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004764 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004765 skipCall |= addCmd(dev_data, pCB, CMD_DRAWINDEXED, "vkCmdDrawIndexed()");
4766 pCB->drawCount[DRAW_INDEXED]++;
4767 skipCall |= validate_draw_state(dev_data, pCB, VK_TRUE);
4768 // TODO : Need to pass commandBuffer as srcObj here
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004769 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_NONE, "DS",
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004770 "vkCmdDrawIndexed() call #%" PRIu64 ", reporting DS state:", g_drawCount[DRAW_INDEXED]++);
4771 skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
Michael Lentine700b0aa2015-10-30 17:57:32 -07004772 if (VK_FALSE == skipCall) {
4773 updateResourceTrackingOnDraw(pCB);
4774 }
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06004775 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDrawIndexed");
Jon Ashburn3950e1b2015-05-20 09:00:28 -06004776 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004777 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004778 dev_data->device_dispatch_table->CmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004779}
4780
Chia-I Wu9ab61502015-11-06 06:42:02 +08004781VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count, uint32_t stride)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004782{
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004783 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4784 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004785 VkBool32 skipCall = VK_FALSE;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004786 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004787 skipCall |= addCmd(dev_data, pCB, CMD_DRAWINDIRECT, "vkCmdDrawIndirect()");
4788 pCB->drawCount[DRAW_INDIRECT]++;
4789 skipCall |= validate_draw_state(dev_data, pCB, VK_FALSE);
4790 // TODO : Need to pass commandBuffer as srcObj here
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004791 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_NONE, "DS",
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004792 "vkCmdDrawIndirect() call #%" PRIu64 ", reporting DS state:", g_drawCount[DRAW_INDIRECT]++);
4793 skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
Michael Lentine700b0aa2015-10-30 17:57:32 -07004794 if (VK_FALSE == skipCall) {
4795 updateResourceTrackingOnDraw(pCB);
4796 }
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06004797 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDrawIndirect");
Jon Ashburn3950e1b2015-05-20 09:00:28 -06004798 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004799 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004800 dev_data->device_dispatch_table->CmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004801}
4802
Chia-I Wu9ab61502015-11-06 06:42:02 +08004803VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count, uint32_t stride)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004804{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004805 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004806 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4807 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004808 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004809 skipCall |= addCmd(dev_data, pCB, CMD_DRAWINDEXEDINDIRECT, "vkCmdDrawIndexedIndirect()");
4810 pCB->drawCount[DRAW_INDEXED_INDIRECT]++;
4811 skipCall |= validate_draw_state(dev_data, pCB, VK_TRUE);
4812 // TODO : Need to pass commandBuffer as srcObj here
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004813 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFO_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_NONE, "DS",
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004814 "vkCmdDrawIndexedIndirect() call #%" PRIu64 ", reporting DS state:", g_drawCount[DRAW_INDEXED_INDIRECT]++);
4815 skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
Michael Lentine700b0aa2015-10-30 17:57:32 -07004816 if (VK_FALSE == skipCall) {
4817 updateResourceTrackingOnDraw(pCB);
4818 }
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06004819 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDrawIndexedIndirect");
Jon Ashburn3950e1b2015-05-20 09:00:28 -06004820 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004821 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004822 dev_data->device_dispatch_table->CmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004823}
4824
Chia-I Wu9ab61502015-11-06 06:42:02 +08004825VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004826{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004827 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004828 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4829 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004830 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004831 skipCall |= addCmd(dev_data, pCB, CMD_DISPATCH, "vkCmdDispatch()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06004832 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdDispatch");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004833 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004834 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004835 dev_data->device_dispatch_table->CmdDispatch(commandBuffer, x, y, z);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004836}
4837
Chia-I Wu9ab61502015-11-06 06:42:02 +08004838VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004839{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004840 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004841 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4842 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004843 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004844 skipCall |= addCmd(dev_data, pCB, CMD_DISPATCHINDIRECT, "vkCmdDispatchIndirect()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06004845 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdDispatchIndirect");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004846 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004847 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004848 dev_data->device_dispatch_table->CmdDispatchIndirect(commandBuffer, buffer, offset);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004849}
4850
Chia-I Wu9ab61502015-11-06 06:42:02 +08004851VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, uint32_t regionCount, const VkBufferCopy* pRegions)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004852{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004853 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004854 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4855 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004856 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004857 skipCall |= addCmd(dev_data, pCB, CMD_COPYBUFFER, "vkCmdCopyBuffer()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06004858 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyBuffer");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004859 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004860 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004861 dev_data->device_dispatch_table->CmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, regionCount, pRegions);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004862}
4863
Courtney Goeltzenleuchtere6dd8082015-12-16 16:07:01 -07004864VkBool32 VerifySourceImageLayout(VkCommandBuffer cmdBuffer, VkImage srcImage, VkImageLayout srcImageLayout) {
Mark Youngb20a6a82016-01-07 15:41:43 -07004865 VkBool32 skip_call = VK_FALSE;
Mark Lobodzinski31e5f282015-11-30 16:48:53 -07004866
4867#ifdef DISABLE_IMAGE_LAYOUT_VALIDATION
4868 // TODO: Fix -- initialLayout may have been set in a previous command buffer
4869 return skip_call;
4870#endif // DISABLE_IMAGE_LAYOUT_VALIDATION
4871
Michael Lentineabc5e922015-10-12 11:30:14 -05004872 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
4873 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, cmdBuffer);
4874 auto src_image_element = pCB->imageLayoutMap.find(srcImage);
4875 if (src_image_element == pCB->imageLayoutMap.end()) {
4876 pCB->imageLayoutMap[srcImage].initialLayout = srcImageLayout;
4877 pCB->imageLayoutMap[srcImage].layout = srcImageLayout;
Mark Youngb20a6a82016-01-07 15:41:43 -07004878 return VK_FALSE;
Michael Lentineabc5e922015-10-12 11:30:14 -05004879 }
4880 if (src_image_element->second.layout != srcImageLayout) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004881 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05004882 "Cannot copy from an image whose source layout is %d and doesn't match the current layout %d.", srcImageLayout, src_image_element->second.layout);
4883 }
4884 if (srcImageLayout != VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL) {
4885 if (srcImageLayout == VK_IMAGE_LAYOUT_GENERAL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07004886 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004887 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERF_WARN_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineecc32b72015-10-16 18:08:09 -05004888 "Layout for input image should be TRANSFER_SRC_OPTIMAL instead of GENERAL.");
Michael Lentineabc5e922015-10-12 11:30:14 -05004889 } else {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004890 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineecc32b72015-10-16 18:08:09 -05004891 "Layout for input image is %d but can only be TRANSFER_SRC_OPTIMAL or GENERAL.", srcImageLayout);
Michael Lentineabc5e922015-10-12 11:30:14 -05004892 }
4893 }
4894 return skip_call;
4895}
4896
Courtney Goeltzenleuchtere6dd8082015-12-16 16:07:01 -07004897VkBool32 VerifyDestImageLayout(VkCommandBuffer cmdBuffer, VkImage destImage, VkImageLayout destImageLayout) {
Mark Youngb20a6a82016-01-07 15:41:43 -07004898 VkBool32 skip_call = VK_FALSE;
Mark Lobodzinski31e5f282015-11-30 16:48:53 -07004899
4900#ifdef DISABLE_IMAGE_LAYOUT_VALIDATION
4901 // TODO: Fix -- initialLayout may have been set in a previous command buffer
4902 return skip_call;
4903#endif // DISABLE_IMAGE_LAYOUT_VALIDATION
4904
Michael Lentineabc5e922015-10-12 11:30:14 -05004905 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
4906 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, cmdBuffer);
4907 auto dest_image_element = pCB->imageLayoutMap.find(destImage);
4908 if (dest_image_element == pCB->imageLayoutMap.end()) {
4909 pCB->imageLayoutMap[destImage].initialLayout = destImageLayout;
4910 pCB->imageLayoutMap[destImage].layout = destImageLayout;
Mark Youngb20a6a82016-01-07 15:41:43 -07004911 return VK_FALSE;
Michael Lentineabc5e922015-10-12 11:30:14 -05004912 }
4913 if (dest_image_element->second.layout != destImageLayout) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004914 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05004915 "Cannot copy from an image whose dest layout is %d and doesn't match the current layout %d.", destImageLayout, dest_image_element->second.layout);
4916 }
4917 if (destImageLayout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
4918 if (destImageLayout == VK_IMAGE_LAYOUT_GENERAL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07004919 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004920 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERF_WARN_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05004921 "Layout for output image should be TRANSFER_DST_OPTIMAL instead of GENERAL.");
4922 } else {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07004923 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05004924 "Layout for output image is %d but can only be TRANSFER_DST_OPTIMAL or GENERAL.", destImageLayout);
4925 }
4926 }
4927 return skip_call;
4928}
4929
Chia-I Wu9ab61502015-11-06 06:42:02 +08004930VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdCopyImage(VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06004931 VkImage srcImage,
4932 VkImageLayout srcImageLayout,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004933 VkImage dstImage,
4934 VkImageLayout dstImageLayout,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06004935 uint32_t regionCount, const VkImageCopy* pRegions)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004936{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004937 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004938 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4939 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004940 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004941 skipCall |= addCmd(dev_data, pCB, CMD_COPYIMAGE, "vkCmdCopyImage()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06004942 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyImage");
Michael Lentineabc5e922015-10-12 11:30:14 -05004943 skipCall |= VerifySourceImageLayout(commandBuffer, srcImage, srcImageLayout);
4944 skipCall |= VerifyDestImageLayout(commandBuffer, dstImage, dstImageLayout);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004945 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004946 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004947 dev_data->device_dispatch_table->CmdCopyImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004948}
4949
Chia-I Wu9ab61502015-11-06 06:42:02 +08004950VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdBlitImage(VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06004951 VkImage srcImage, VkImageLayout srcImageLayout,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004952 VkImage dstImage, VkImageLayout dstImageLayout,
Mark Lobodzinskiee5eef12015-05-22 14:43:25 -05004953 uint32_t regionCount, const VkImageBlit* pRegions,
Chia-I Wub99df442015-10-26 16:49:32 +08004954 VkFilter filter)
Courtney Goeltzenleuchter91f9cee2015-04-03 14:42:51 -06004955{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004956 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004957 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4958 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Courtney Goeltzenleuchter91f9cee2015-04-03 14:42:51 -06004959 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004960 skipCall |= addCmd(dev_data, pCB, CMD_BLITIMAGE, "vkCmdBlitImage()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06004961 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdBlitImage");
Courtney Goeltzenleuchter91f9cee2015-04-03 14:42:51 -06004962 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004963 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004964 dev_data->device_dispatch_table->CmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter);
Courtney Goeltzenleuchter91f9cee2015-04-03 14:42:51 -06004965}
4966
Chia-I Wu9ab61502015-11-06 06:42:02 +08004967VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdCopyBufferToImage(VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06004968 VkBuffer srcBuffer,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004969 VkImage dstImage, VkImageLayout dstImageLayout,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06004970 uint32_t regionCount, const VkBufferImageCopy* pRegions)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004971{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004972 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004973 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4974 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004975 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004976 skipCall |= addCmd(dev_data, pCB, CMD_COPYBUFFERTOIMAGE, "vkCmdCopyBufferToImage()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06004977 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyBufferToImage");
Michael Lentineabc5e922015-10-12 11:30:14 -05004978 skipCall |= VerifyDestImageLayout(commandBuffer, dstImage, dstImageLayout);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004979 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004980 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004981 dev_data->device_dispatch_table->CmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004982}
4983
Chia-I Wu9ab61502015-11-06 06:42:02 +08004984VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdCopyImageToBuffer(VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06004985 VkImage srcImage, VkImageLayout srcImageLayout,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004986 VkBuffer dstBuffer,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06004987 uint32_t regionCount, const VkBufferImageCopy* pRegions)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004988{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004989 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004990 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
4991 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004992 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07004993 skipCall |= addCmd(dev_data, pCB, CMD_COPYIMAGETOBUFFER, "vkCmdCopyImageToBuffer()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06004994 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyImageToBuffer");
Michael Lentineabc5e922015-10-12 11:30:14 -05004995 skipCall |= VerifySourceImageLayout(commandBuffer, srcImage, srcImageLayout);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004996 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06004997 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08004998 dev_data->device_dispatch_table->CmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06004999}
5000
Chia-I Wu9ab61502015-11-06 06:42:02 +08005001VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize dataSize, const uint32_t* pData)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005002{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005003 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005004 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5005 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005006 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005007 skipCall |= addCmd(dev_data, pCB, CMD_UPDATEBUFFER, "vkCmdUpdateBuffer()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005008 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyUpdateBuffer");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005009 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005010 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005011 dev_data->device_dispatch_table->CmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005012}
5013
Chia-I Wu9ab61502015-11-06 06:42:02 +08005014VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize size, uint32_t data)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005015{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005016 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005017 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5018 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005019 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005020 skipCall |= addCmd(dev_data, pCB, CMD_FILLBUFFER, "vkCmdFillBuffer()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005021 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyFillBuffer");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005022 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005023 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005024 dev_data->device_dispatch_table->CmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005025}
5026
Chia-I Wu9ab61502015-11-06 06:42:02 +08005027VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdClearAttachments(
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005028 VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchterc9323e02015-10-15 16:51:05 -06005029 uint32_t attachmentCount,
5030 const VkClearAttachment* pAttachments,
Tobin Ehlis53eddda2015-07-01 16:46:13 -06005031 uint32_t rectCount,
Courtney Goeltzenleuchter4ca43f62015-10-15 18:22:08 -06005032 const VkClearRect* pRects)
Tobin Ehlis53eddda2015-07-01 16:46:13 -06005033{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005034 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005035 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5036 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis53eddda2015-07-01 16:46:13 -06005037 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005038 skipCall |= addCmd(dev_data, pCB, CMD_CLEARATTACHMENTS, "vkCmdClearAttachments()");
5039 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
5040 if (!hasDrawCmd(pCB) &&
5041 (pCB->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
5042 (pCB->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
5043 // TODO : commandBuffer should be srcObj
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005044 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_CLEAR_CMD_BEFORE_DRAW, "DS",
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005045 "vkCmdClearAttachments() issued on CB object 0x%" PRIxLEAST64 " prior to any Draw Cmds."
Mark Youngee3f3a22016-01-25 12:18:32 -07005046 " It is recommended you use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.", (uint64_t)(commandBuffer));
Tobin Ehlis53eddda2015-07-01 16:46:13 -06005047 }
Courtney Goeltzenleuchterc9323e02015-10-15 16:51:05 -06005048 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdClearAttachments");
5049 }
5050
5051 // Validate that attachment is in reference list of active subpass
5052 if (pCB->activeRenderPass) {
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07005053 const VkRenderPassCreateInfo *pRPCI = dev_data->renderPassMap[pCB->activeRenderPass]->pCreateInfo;
Courtney Goeltzenleuchterc9323e02015-10-15 16:51:05 -06005054 const VkSubpassDescription *pSD = &pRPCI->pSubpasses[pCB->activeSubpass];
5055
5056 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
5057 const VkClearAttachment *attachment = &pAttachments[attachment_idx];
5058 if (attachment->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
5059 VkBool32 found = VK_FALSE;
Chia-I Wud50a7d72015-10-26 20:48:51 +08005060 for (uint32_t i = 0; i < pSD->colorAttachmentCount; i++) {
Courtney Goeltzenleuchterc9323e02015-10-15 16:51:05 -06005061 if (attachment->colorAttachment == pSD->pColorAttachments[i].attachment) {
5062 found = VK_TRUE;
5063 break;
5064 }
5065 }
5066 if (VK_FALSE == found) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07005067 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005068 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Courtney Goeltzenleuchterc9323e02015-10-15 16:51:05 -06005069 "vkCmdClearAttachments() attachment index %d not found in attachment reference array of active subpass %d",
5070 attachment->colorAttachment, pCB->activeSubpass);
5071 }
5072 } else if (attachment->aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
Mark Lobodzinskid6e7bff2015-11-17 15:11:24 -07005073 if (!pSD->pDepthStencilAttachment || // Says no DS will be used in active subpass
Mark Lobodzinski2fd355a2015-11-18 08:58:31 -07005074 (pSD->pDepthStencilAttachment->attachment == VK_ATTACHMENT_UNUSED)) { // Says no DS will be used in active subpass
Mark Lobodzinskid6e7bff2015-11-17 15:11:24 -07005075
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07005076 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005077 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskid6e7bff2015-11-17 15:11:24 -07005078 "vkCmdClearAttachments() attachment index %d does not match depthStencilAttachment.attachment (%d) found in active subpass %d",
5079 attachment->colorAttachment,
5080 (pSD->pDepthStencilAttachment) ? pSD->pDepthStencilAttachment->attachment : VK_ATTACHMENT_UNUSED,
5081 pCB->activeSubpass);
Courtney Goeltzenleuchterc9323e02015-10-15 16:51:05 -06005082 }
5083 }
5084 }
Tobin Ehlis53eddda2015-07-01 16:46:13 -06005085 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005086 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005087 dev_data->device_dispatch_table->CmdClearAttachments(commandBuffer, attachmentCount, pAttachments, rectCount, pRects);
Tobin Ehlis53eddda2015-07-01 16:46:13 -06005088}
5089
Chia-I Wu9ab61502015-11-06 06:42:02 +08005090VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdClearColorImage(
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005091 VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06005092 VkImage image, VkImageLayout imageLayout,
Chris Forbesf0796e12015-06-24 14:34:53 +12005093 const VkClearColorValue *pColor,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06005094 uint32_t rangeCount, const VkImageSubresourceRange* pRanges)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005095{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005096 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005097 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5098 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005099 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005100 skipCall |= addCmd(dev_data, pCB, CMD_CLEARCOLORIMAGE, "vkCmdClearColorImage()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005101 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdClearColorImage");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005102 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005103 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005104 dev_data->device_dispatch_table->CmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005105}
5106
Chia-I Wu9ab61502015-11-06 06:42:02 +08005107VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdClearDepthStencilImage(
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005108 VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchter45df9e12015-09-15 18:03:22 -06005109 VkImage image, VkImageLayout imageLayout,
5110 const VkClearDepthStencilValue *pDepthStencil,
5111 uint32_t rangeCount,
5112 const VkImageSubresourceRange* pRanges)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005113{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005114 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005115 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5116 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005117 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005118 skipCall |= addCmd(dev_data, pCB, CMD_CLEARDEPTHSTENCILIMAGE, "vkCmdClearDepthStencilImage()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005119 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdClearDepthStencilImage");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005120 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005121 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005122 dev_data->device_dispatch_table->CmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005123}
5124
Chia-I Wu9ab61502015-11-06 06:42:02 +08005125VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdResolveImage(VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06005126 VkImage srcImage, VkImageLayout srcImageLayout,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005127 VkImage dstImage, VkImageLayout dstImageLayout,
Tony Barbour6865d4a2015-04-13 15:02:52 -06005128 uint32_t regionCount, const VkImageResolve* pRegions)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005129{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005130 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005131 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5132 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005133 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005134 skipCall |= addCmd(dev_data, pCB, CMD_RESOLVEIMAGE, "vkCmdResolveImage()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005135 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdResolveImage");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005136 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005137 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005138 dev_data->device_dispatch_table->CmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005139}
5140
Chia-I Wu9ab61502015-11-06 06:42:02 +08005141VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005142{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005143 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005144 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5145 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005146 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005147 skipCall |= addCmd(dev_data, pCB, CMD_SETEVENT, "vkCmdSetEvent()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005148 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdSetEvent");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005149 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005150 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005151 dev_data->device_dispatch_table->CmdSetEvent(commandBuffer, event, stageMask);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005152}
5153
Chia-I Wu9ab61502015-11-06 06:42:02 +08005154VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005155{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005156 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005157 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5158 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005159 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005160 skipCall |= addCmd(dev_data, pCB, CMD_RESETEVENT, "vkCmdResetEvent()");
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005161 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdResetEvent");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005162 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005163 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005164 dev_data->device_dispatch_table->CmdResetEvent(commandBuffer, event, stageMask);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005165}
5166
Jon Ashburnf19916e2016-01-11 13:12:43 -07005167VkBool32 TransitionImageLayouts(VkCommandBuffer cmdBuffer, uint32_t memBarrierCount, const VkImageMemoryBarrier* pImgMemBarriers) {
Michael Lentineabc5e922015-10-12 11:30:14 -05005168 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
5169 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, cmdBuffer);
Mark Youngb20a6a82016-01-07 15:41:43 -07005170 VkBool32 skip = VK_FALSE;
Mark Lobodzinski31e5f282015-11-30 16:48:53 -07005171
5172#ifdef DISABLE_IMAGE_LAYOUT_VALIDATION
5173 // TODO: Fix -- pay attention to image subresource ranges -- not all subresources transition at the same time
5174 return skip;
5175#endif // DISABLE_IMAGE_LAYOUT_VALIDATION
5176
Michael Lentineabc5e922015-10-12 11:30:14 -05005177 for (uint32_t i = 0; i < memBarrierCount; ++i) {
Jon Ashburnf19916e2016-01-11 13:12:43 -07005178 auto mem_barrier = &pImgMemBarriers[i];
Michael Lentineabc5e922015-10-12 11:30:14 -05005179 if (mem_barrier && mem_barrier->sType == VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER) {
Jon Ashburnf19916e2016-01-11 13:12:43 -07005180 auto image_data = pCB->imageLayoutMap.find(mem_barrier->image);
Michael Lentineabc5e922015-10-12 11:30:14 -05005181 if (image_data == pCB->imageLayoutMap.end()) {
Jon Ashburnf19916e2016-01-11 13:12:43 -07005182 pCB->imageLayoutMap[mem_barrier->image].initialLayout = mem_barrier->oldLayout;
5183 pCB->imageLayoutMap[mem_barrier->image].layout = mem_barrier->newLayout;
Michael Lentineabc5e922015-10-12 11:30:14 -05005184 } else {
Jon Ashburnf19916e2016-01-11 13:12:43 -07005185 if (image_data->second.layout != mem_barrier->oldLayout) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005186 skip |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Jon Ashburnf19916e2016-01-11 13:12:43 -07005187 "You cannot transition the layout from %d when current layout is %d.", mem_barrier->oldLayout, image_data->second.layout);
Michael Lentineabc5e922015-10-12 11:30:14 -05005188 }
Jon Ashburnf19916e2016-01-11 13:12:43 -07005189 image_data->second.layout = mem_barrier->newLayout;
Michael Lentineabc5e922015-10-12 11:30:14 -05005190 }
5191 }
5192 }
5193 return skip;
5194}
5195
Mark Lobodzinskia3a86112016-01-05 17:17:55 -07005196// Print readable FlagBits in FlagMask
5197std::string string_VkAccessFlags(VkAccessFlags accessMask)
5198{
5199 std::string result;
5200 std::string separator;
5201
5202 if (accessMask == 0) {
5203 result = "[None]";
5204 } else {
5205 result = "[";
5206 for (auto i = 0; i < 32; i++) {
5207 if (accessMask & (1 << i)) {
5208 result = result + separator + string_VkAccessFlagBits((VkAccessFlagBits)(1 << i));
5209 separator = " | ";
5210 }
5211 }
5212 result = result + "]";
5213 }
5214 return result;
5215}
5216
Michael Lentine97eb7462015-11-20 09:48:52 -08005217// AccessFlags MUST have 'required_bit' set, and may have one or more of 'optional_bits' set.
5218// If required_bit is zero, accessMask must have at least one of 'optional_bits' set
Mark Lobodzinskifd740fc2016-01-06 12:56:29 -07005219// TODO: Add tracking to ensure that at least one barrier has been set for these layout transitions
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005220VkBool32 ValidateMaskBits(const layer_data* my_data, VkCommandBuffer cmdBuffer, const VkAccessFlags& accessMask, const VkImageLayout& layout,
5221 VkAccessFlags required_bit, VkAccessFlags optional_bits, const char* type) {
Mark Youngb20a6a82016-01-07 15:41:43 -07005222 VkBool32 skip_call = VK_FALSE;
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005223
Michael Lentine97eb7462015-11-20 09:48:52 -08005224 if ((accessMask & required_bit) || (!required_bit && (accessMask & optional_bits))) {
5225 if (accessMask & !(required_bit | optional_bits)) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07005226 // TODO: Verify against Valid Use
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005227 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005228 "Additional bits in %s accessMask %d %s are specified when layout is %s.",
5229 type, accessMask, string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
Michael Lentineecc32b72015-10-16 18:08:09 -05005230 }
5231 } else {
Michael Lentine97eb7462015-11-20 09:48:52 -08005232 if (!required_bit) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005233 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinskifd740fc2016-01-06 12:56:29 -07005234 "%s AccessMask %d %s must contain at least one of access bits %d %s when layout is %s, unless the app has previously added a barrier for this transition.",
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005235 type, accessMask, string_VkAccessFlags(accessMask).c_str(), optional_bits,
5236 string_VkAccessFlags(optional_bits).c_str(), string_VkImageLayout(layout));
Michael Lentine97eb7462015-11-20 09:48:52 -08005237 } else {
Mark Lobodzinskif2cc2a52016-01-05 13:35:29 -07005238 std::string opt_bits;
5239 if (optional_bits != 0) {
Michael Lentine6bd4f122016-01-19 14:00:53 -06005240 std::stringstream ss;
5241 ss << optional_bits;
5242 opt_bits = "and may have optional bits " + ss.str() + ' ' + string_VkAccessFlags(optional_bits);
Mark Lobodzinskif2cc2a52016-01-05 13:35:29 -07005243 }
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005244 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinskifd740fc2016-01-06 12:56:29 -07005245 "%s AccessMask %d %s must have required access bit %d %s %s when layout is %s, unless the app has previously added a barrier for this transition.",
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005246 type, accessMask, string_VkAccessFlags(accessMask).c_str(),
5247 required_bit, string_VkAccessFlags(required_bit).c_str(),
Mark Lobodzinskif2cc2a52016-01-05 13:35:29 -07005248 opt_bits.c_str(), string_VkImageLayout(layout));
Michael Lentine97eb7462015-11-20 09:48:52 -08005249 }
Michael Lentineecc32b72015-10-16 18:08:09 -05005250 }
5251 return skip_call;
5252}
5253
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005254VkBool32 ValidateMaskBitsFromLayouts(const layer_data* my_data, VkCommandBuffer cmdBuffer, const VkAccessFlags& accessMask, const VkImageLayout& layout, const char* type) {
Mark Youngb20a6a82016-01-07 15:41:43 -07005255 VkBool32 skip_call = VK_FALSE;
Michael Lentine97eb7462015-11-20 09:48:52 -08005256 switch (layout) {
5257 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: {
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005258 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_COLOR_ATTACHMENT_READ_BIT, type);
Michael Lentine97eb7462015-11-20 09:48:52 -08005259 break;
Michael Lentineecc32b72015-10-16 18:08:09 -05005260 }
Michael Lentine97eb7462015-11-20 09:48:52 -08005261 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: {
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005262 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT, type);
Michael Lentine97eb7462015-11-20 09:48:52 -08005263 break;
Michael Lentineecc32b72015-10-16 18:08:09 -05005264 }
Michael Lentine97eb7462015-11-20 09:48:52 -08005265 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: {
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005266 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_WRITE_BIT, 0, type);
Michael Lentine97eb7462015-11-20 09:48:52 -08005267 break;
5268 }
5269 case VK_IMAGE_LAYOUT_PREINITIALIZED: {
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005270 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_HOST_WRITE_BIT, 0, type);
Michael Lentine97eb7462015-11-20 09:48:52 -08005271 break;
5272 }
5273 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL: {
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005274 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, 0, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
Michael Lentine97eb7462015-11-20 09:48:52 -08005275 break;
5276 }
5277 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: {
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005278 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, 0, VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
Michael Lentine97eb7462015-11-20 09:48:52 -08005279 break;
5280 }
5281 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: {
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005282 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_READ_BIT, 0, type);
Michael Lentine97eb7462015-11-20 09:48:52 -08005283 break;
5284 }
5285 case VK_IMAGE_LAYOUT_UNDEFINED: {
5286 if (accessMask != 0) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07005287 // TODO: Verify against Valid Use section spec
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005288 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski882655d2016-01-05 11:32:53 -07005289 "Additional bits in %s accessMask %d %s are specified when layout is %s.", type, accessMask, string_VkAccessFlags(accessMask).c_str(),
5290 string_VkImageLayout(layout));
Michael Lentine97eb7462015-11-20 09:48:52 -08005291 }
5292 break;
5293 }
5294 case VK_IMAGE_LAYOUT_GENERAL:
5295 default: {
5296 break;
5297 }
Michael Lentineecc32b72015-10-16 18:08:09 -05005298 }
5299 return skip_call;
5300}
5301
Jon Ashburnf19916e2016-01-11 13:12:43 -07005302VkBool32 ValidateBarriers(VkCommandBuffer cmdBuffer, uint32_t memBarrierCount, const VkMemoryBarrier* pMemBarriers, uint32_t imageMemBarrierCount, const VkImageMemoryBarrier *pImageMemBarriers)
5303{
Mark Youngb20a6a82016-01-07 15:41:43 -07005304 VkBool32 skip_call = VK_FALSE;
Michael Lentine48930b82015-10-15 17:07:00 -05005305 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
5306 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, cmdBuffer);
5307 if (pCB->activeRenderPass && memBarrierCount) {
5308 for (uint32_t i = 0; i < memBarrierCount; ++i) {
Jon Ashburnf19916e2016-01-11 13:12:43 -07005309 auto mem_barrier = &pMemBarriers[i];
Michael Lentine48930b82015-10-15 17:07:00 -05005310 if (mem_barrier && mem_barrier->sType != VK_STRUCTURE_TYPE_MEMORY_BARRIER) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005311 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Michael Lentine48930b82015-10-15 17:07:00 -05005312 "Image or Buffers Barriers cannot be used during a render pass.");
5313 }
5314 }
5315 if (!dev_data->renderPassMap[pCB->activeRenderPass]->hasSelfDependency[pCB->activeSubpass]) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005316 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Michael Lentine48930b82015-10-15 17:07:00 -05005317 "Barriers cannot be set during subpass %d with no self dependency specified.", pCB->activeSubpass);
5318 }
5319 }
Mark Lobodzinski73a37ec2015-11-20 09:27:27 -07005320
Jon Ashburnf19916e2016-01-11 13:12:43 -07005321 for (uint32_t i = 0; i < imageMemBarrierCount; ++i) {
5322 auto mem_barrier = &pImageMemBarriers[i];
Michael Lentineecc32b72015-10-16 18:08:09 -05005323 if (mem_barrier && mem_barrier->sType == VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER) {
Jon Ashburnf19916e2016-01-11 13:12:43 -07005324 skip_call |= ValidateMaskBitsFromLayouts(dev_data, cmdBuffer, mem_barrier->srcAccessMask, mem_barrier->oldLayout, "Source");
5325 skip_call |= ValidateMaskBitsFromLayouts(dev_data, cmdBuffer, mem_barrier->dstAccessMask, mem_barrier->newLayout, "Dest");
Michael Lentineecc32b72015-10-16 18:08:09 -05005326 }
5327 }
Mark Lobodzinski3cba24a2015-12-03 15:42:32 -07005328
Michael Lentine48930b82015-10-15 17:07:00 -05005329 return skip_call;
5330}
5331
Jon Ashburnf19916e2016-01-11 13:12:43 -07005332VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdWaitEvents(
5333 VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
5334 VkPipelineStageFlags sourceStageMask, VkPipelineStageFlags dstStageMask,
5335 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
5336 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier* pBufferMemoryBarriers,
5337 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier* pImageMemoryBarriers)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005338{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005339 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005340 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5341 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005342 if (pCB) {
Michael Lentineb887b0a2015-12-29 14:12:11 -06005343 for (uint32_t i = 0; i < eventCount; ++i) {
5344 pCB->waitedEvents.push_back(pEvents[i]);
5345 }
Mark Lobodzinskice738852016-01-07 10:04:02 -07005346 if (pCB->state == CB_RECORDING) {
5347 skipCall |= addCmd(dev_data, pCB, CMD_WAITEVENTS, "vkCmdWaitEvents()");
Michael Lentineb887b0a2015-12-29 14:12:11 -06005348 } else {
5349 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdWaitEvents()");
5350 }
Jon Ashburnf19916e2016-01-11 13:12:43 -07005351 skipCall |= TransitionImageLayouts(commandBuffer, imageMemoryBarrierCount, pImageMemoryBarriers);
5352 skipCall |= ValidateBarriers(commandBuffer, memoryBarrierCount, pMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005353 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005354 if (VK_FALSE == skipCall)
Jon Ashburnf19916e2016-01-11 13:12:43 -07005355 dev_data->device_dispatch_table->CmdWaitEvents(commandBuffer, eventCount, pEvents, sourceStageMask, dstStageMask,
5356 memoryBarrierCount, pMemoryBarriers,
5357 bufferMemoryBarrierCount, pBufferMemoryBarriers,
5358 imageMemoryBarrierCount, pImageMemoryBarriers);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005359}
5360
Jon Ashburnf19916e2016-01-11 13:12:43 -07005361VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdPipelineBarrier(
5362 VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
5363 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
5364 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
5365 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier* pBufferMemoryBarriers,
5366 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier* pImageMemoryBarriers)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005367{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005368 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005369 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5370 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005371 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005372 skipCall |= addCmd(dev_data, pCB, CMD_PIPELINEBARRIER, "vkCmdPipelineBarrier()");
Jon Ashburnf19916e2016-01-11 13:12:43 -07005373 skipCall |= TransitionImageLayouts(commandBuffer, imageMemoryBarrierCount, pImageMemoryBarriers);
5374 skipCall |= ValidateBarriers(commandBuffer, memoryBarrierCount, pMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005375 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005376 if (VK_FALSE == skipCall)
Jon Ashburnf19916e2016-01-11 13:12:43 -07005377 dev_data->device_dispatch_table->CmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, dependencyFlags,
5378 memoryBarrierCount, pMemoryBarriers,
5379 bufferMemoryBarrierCount, pBufferMemoryBarriers,
5380 imageMemoryBarrierCount, pImageMemoryBarriers);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005381}
5382
Chia-I Wu9ab61502015-11-06 06:42:02 +08005383VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdBeginQuery(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t slot, VkFlags flags)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005384{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005385 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005386 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5387 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005388 if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005389 skipCall |= addCmd(dev_data, pCB, CMD_BEGINQUERY, "vkCmdBeginQuery()");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005390 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005391 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005392 dev_data->device_dispatch_table->CmdBeginQuery(commandBuffer, queryPool, slot, flags);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005393}
5394
Chia-I Wu9ab61502015-11-06 06:42:02 +08005395VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdEndQuery(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t slot)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005396{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005397 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005398 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5399 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005400 if (pCB) {
Michael Lentineb887b0a2015-12-29 14:12:11 -06005401 QueryObject query = {queryPool, slot};
5402 pCB->queryToStateMap[query] = 1;
Mark Lobodzinskice738852016-01-07 10:04:02 -07005403 if (pCB->state == CB_RECORDING) {
5404 skipCall |= addCmd(dev_data, pCB, CMD_ENDQUERY, "VkCmdEndQuery()");
Michael Lentineb887b0a2015-12-29 14:12:11 -06005405 } else {
5406 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdEndQuery()");
5407 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005408 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005409 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005410 dev_data->device_dispatch_table->CmdEndQuery(commandBuffer, queryPool, slot);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005411}
5412
Jon Ashburn19d3bf12015-12-30 14:06:55 -07005413VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdResetQueryPool(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005414{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005415 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005416 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5417 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005418 if (pCB) {
Michael Lentineb887b0a2015-12-29 14:12:11 -06005419 for (uint32_t i = 0; i < queryCount; i++) {
Mark Lobodzinskice738852016-01-07 10:04:02 -07005420 QueryObject query = {queryPool, firstQuery + i};
Michael Lentineb887b0a2015-12-29 14:12:11 -06005421 pCB->waitedEventsBeforeQueryReset[query] = pCB->waitedEvents;
5422 pCB->queryToStateMap[query] = 0;
5423 }
Mark Lobodzinskice738852016-01-07 10:04:02 -07005424 if (pCB->state == CB_RECORDING) {
5425 skipCall |= addCmd(dev_data, pCB, CMD_RESETQUERYPOOL, "VkCmdResetQueryPool()");
Michael Lentineb887b0a2015-12-29 14:12:11 -06005426 } else {
5427 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdResetQueryPool()");
5428 }
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005429 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdQueryPool");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005430 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005431 if (VK_FALSE == skipCall)
Jon Ashburn19d3bf12015-12-30 14:06:55 -07005432 dev_data->device_dispatch_table->CmdResetQueryPool(commandBuffer, queryPool, firstQuery, queryCount);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005433}
5434
Jon Ashburn19d3bf12015-12-30 14:06:55 -07005435VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005436 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
Chia-I Wuccc93a72015-10-26 18:36:20 +08005437 VkDeviceSize stride, VkQueryResultFlags flags)
Mark Lobodzinski5495d132015-09-30 16:19:16 -06005438{
5439 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005440 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5441 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Mark Lobodzinski5495d132015-09-30 16:19:16 -06005442 if (pCB) {
Michael Lentineb887b0a2015-12-29 14:12:11 -06005443 for (uint32_t i = 0; i < queryCount; i++) {
Mark Lobodzinskice738852016-01-07 10:04:02 -07005444 QueryObject query = {queryPool, firstQuery + i};
Michael Lentineb887b0a2015-12-29 14:12:11 -06005445 if(!pCB->queryToStateMap[query]) {
Mark Lobodzinskice738852016-01-07 10:04:02 -07005446 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
Mark Young93ecb1d2016-01-13 13:47:16 -07005447 "Requesting a copy from query to buffer with invalid query: queryPool %" PRIu64 ", index %d", (uint64_t)(queryPool), firstQuery + i);
Michael Lentineb887b0a2015-12-29 14:12:11 -06005448 }
5449 }
Mark Lobodzinskice738852016-01-07 10:04:02 -07005450 if (pCB->state == CB_RECORDING) {
5451 skipCall |= addCmd(dev_data, pCB, CMD_COPYQUERYPOOLRESULTS, "vkCmdCopyQueryPoolResults()");
Michael Lentineb887b0a2015-12-29 14:12:11 -06005452 } else {
5453 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdCopyQueryPoolResults()");
5454 }
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005455 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyQueryPoolResults");
Mark Lobodzinski5495d132015-09-30 16:19:16 -06005456 }
5457 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005458 dev_data->device_dispatch_table->CmdCopyQueryPoolResults(commandBuffer, queryPool,
Jon Ashburn19d3bf12015-12-30 14:06:55 -07005459 firstQuery, queryCount, dstBuffer, dstOffset, stride, flags);
Mark Lobodzinski5495d132015-09-30 16:19:16 -06005460}
5461
Chia-I Wu9ab61502015-11-06 06:42:02 +08005462VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage, VkQueryPool queryPool, uint32_t slot)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005463{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005464 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005465 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5466 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005467 if (pCB) {
Michael Lentineb887b0a2015-12-29 14:12:11 -06005468 QueryObject query = {queryPool, slot};
5469 pCB->queryToStateMap[query] = 1;
Mark Lobodzinskice738852016-01-07 10:04:02 -07005470 if (pCB->state == CB_RECORDING) {
5471 skipCall |= addCmd(dev_data, pCB, CMD_WRITETIMESTAMP, "vkCmdWriteTimestamp()");
Michael Lentineb887b0a2015-12-29 14:12:11 -06005472 } else {
5473 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdWriteTimestamp()");
5474 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005475 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005476 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005477 dev_data->device_dispatch_table->CmdWriteTimestamp(commandBuffer, pipelineStage, queryPool, slot);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005478}
5479
Chia-I Wu9ab61502015-11-06 06:42:02 +08005480VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer)
Tobin Ehliseba312c2015-04-01 08:40:34 -06005481{
Tobin Ehlis0b632332015-10-07 09:38:40 -06005482 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08005483 VkResult result = dev_data->device_dispatch_table->CreateFramebuffer(device, pCreateInfo, pAllocator, pFramebuffer);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06005484 if (VK_SUCCESS == result) {
Tobin Ehliseba312c2015-04-01 08:40:34 -06005485 // Shadow create info and store in map
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06005486 VkFramebufferCreateInfo* localFBCI = new VkFramebufferCreateInfo(*pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +08005487 if (pCreateInfo->pAttachments) {
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06005488 localFBCI->pAttachments = new VkImageView[localFBCI->attachmentCount];
5489 memcpy((void*)localFBCI->pAttachments, pCreateInfo->pAttachments, localFBCI->attachmentCount*sizeof(VkImageView));
Tobin Ehliseba312c2015-04-01 08:40:34 -06005490 }
Chia-I Wue2fc5522015-10-26 20:04:44 +08005491 dev_data->frameBufferMap[*pFramebuffer] = localFBCI;
Tobin Ehliseba312c2015-04-01 08:40:34 -06005492 }
5493 return result;
5494}
5495
Michael Lentineb6986752015-10-06 14:56:18 -07005496// Store the DAG.
5497struct DAGNode {
5498 uint32_t pass;
5499 std::vector<uint32_t> prev;
5500 std::vector<uint32_t> next;
5501};
5502
Courtney Goeltzenleuchter6ed5dc22015-11-03 15:41:43 -07005503VkBool32 FindDependency(const int index, const int dependent, const std::vector<DAGNode>& subpass_to_node, std::unordered_set<uint32_t>& processed_nodes) {
Michael Lentineb6986752015-10-06 14:56:18 -07005504 // If we have already checked this node we have not found a dependency path so return false.
5505 if (processed_nodes.count(index))
Mark Youngb20a6a82016-01-07 15:41:43 -07005506 return VK_FALSE;
Michael Lentineb6986752015-10-06 14:56:18 -07005507 processed_nodes.insert(index);
5508 const DAGNode& node = subpass_to_node[index];
5509 // Look for a dependency path. If one exists return true else recurse on the previous nodes.
5510 if (std::find(node.prev.begin(), node.prev.end(), dependent) == node.prev.end()) {
5511 for (auto elem : node.prev) {
5512 if (FindDependency(elem, dependent, subpass_to_node, processed_nodes))
Mark Youngb20a6a82016-01-07 15:41:43 -07005513 return VK_TRUE;
Michael Lentineb6986752015-10-06 14:56:18 -07005514 }
5515 } else {
Mark Youngb20a6a82016-01-07 15:41:43 -07005516 return VK_TRUE;
Michael Lentineb6986752015-10-06 14:56:18 -07005517 }
Mark Youngb20a6a82016-01-07 15:41:43 -07005518 return VK_FALSE;
Michael Lentineb6986752015-10-06 14:56:18 -07005519}
5520
Courtney Goeltzenleuchter6ed5dc22015-11-03 15:41:43 -07005521VkBool32 CheckDependencyExists(const layer_data* my_data, VkDevice device, const int subpass, const std::vector<uint32_t>& dependent_subpasses, const std::vector<DAGNode>& subpass_to_node, VkBool32& skip_call) {
Mark Youngb20a6a82016-01-07 15:41:43 -07005522 VkBool32 result = VK_TRUE;
Michael Lentineb6986752015-10-06 14:56:18 -07005523 // Loop through all subpasses that share the same attachment and make sure a dependency exists
5524 for (uint32_t k = 0; k < dependent_subpasses.size(); ++k) {
5525 if (subpass == dependent_subpasses[k])
5526 continue;
5527 const DAGNode& node = subpass_to_node[subpass];
5528 // Check for a specified dependency between the two nodes. If one exists we are done.
5529 auto prev_elem = std::find(node.prev.begin(), node.prev.end(), dependent_subpasses[k]);
5530 auto next_elem = std::find(node.next.begin(), node.next.end(), dependent_subpasses[k]);
5531 if (prev_elem == node.prev.end() && next_elem == node.next.end()) {
5532 // If no dependency exits an implicit dependency still might. If so, warn and if not throw an error.
5533 std::unordered_set<uint32_t> processed_nodes;
5534 if (FindDependency(subpass, dependent_subpasses[k], subpass_to_node, processed_nodes) ||
5535 FindDependency(dependent_subpasses[k], subpass, subpass_to_node, processed_nodes)) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07005536 // TODO: Verify against Valid Use section of spec
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005537 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
Michael Lentineb6986752015-10-06 14:56:18 -07005538 "A dependency between subpasses %d and %d must exist but only an implicit one is specified.",
5539 subpass, dependent_subpasses[k]);
5540 } else {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005541 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
Michael Lentineb6986752015-10-06 14:56:18 -07005542 "A dependency between subpasses %d and %d must exist but one is not specified.",
5543 subpass, dependent_subpasses[k]);
Mark Youngb20a6a82016-01-07 15:41:43 -07005544 result = VK_FALSE;
Michael Lentineb6986752015-10-06 14:56:18 -07005545 }
5546 }
5547 }
5548 return result;
5549}
5550
Jon Ashburnf19916e2016-01-11 13:12:43 -07005551VkBool32 CheckPreserved(const layer_data* my_data, VkDevice device, const VkRenderPassCreateInfo* pCreateInfo, const int index, const uint32_t attachment, const std::vector<DAGNode>& subpass_to_node, int depth, VkBool32& skip_call) {
Michael Lentineb6986752015-10-06 14:56:18 -07005552 const DAGNode& node = subpass_to_node[index];
5553 // If this node writes to the attachment return true as next nodes need to preserve the attachment.
5554 const VkSubpassDescription& subpass = pCreateInfo->pSubpasses[index];
Chia-I Wud50a7d72015-10-26 20:48:51 +08005555 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
Michael Lentineb6986752015-10-06 14:56:18 -07005556 if (attachment == subpass.pColorAttachments[j].attachment)
Courtney Goeltzenleuchter6ed5dc22015-11-03 15:41:43 -07005557 return VK_TRUE;
Michael Lentineb6986752015-10-06 14:56:18 -07005558 }
Chia-I Wu1efb7e52015-10-26 17:32:47 +08005559 if (subpass.pDepthStencilAttachment &&
5560 subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
5561 if (attachment == subpass.pDepthStencilAttachment->attachment)
Courtney Goeltzenleuchter6ed5dc22015-11-03 15:41:43 -07005562 return VK_TRUE;
Michael Lentineb6986752015-10-06 14:56:18 -07005563 }
Courtney Goeltzenleuchter6ed5dc22015-11-03 15:41:43 -07005564 VkBool32 result = VK_FALSE;
Michael Lentineb6986752015-10-06 14:56:18 -07005565 // Loop through previous nodes and see if any of them write to the attachment.
5566 for (auto elem : node.prev) {
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005567 result |= CheckPreserved(my_data, device, pCreateInfo, elem, attachment, subpass_to_node, depth + 1, skip_call);
Michael Lentineb6986752015-10-06 14:56:18 -07005568 }
5569 // If the attachment was written to by a previous node than this node needs to preserve it.
5570 if (result && depth > 0) {
5571 const VkSubpassDescription& subpass = pCreateInfo->pSubpasses[index];
Mark Youngb20a6a82016-01-07 15:41:43 -07005572 VkBool32 has_preserved = VK_FALSE;
Chia-I Wud50a7d72015-10-26 20:48:51 +08005573 for (uint32_t j = 0; j < subpass.preserveAttachmentCount; ++j) {
Jon Ashburnf19916e2016-01-11 13:12:43 -07005574 if (subpass.pPreserveAttachments[j] == attachment) {
Mark Youngb20a6a82016-01-07 15:41:43 -07005575 has_preserved = VK_TRUE;
Michael Lentineb6986752015-10-06 14:56:18 -07005576 break;
5577 }
5578 }
Mark Youngb20a6a82016-01-07 15:41:43 -07005579 if (has_preserved == VK_FALSE) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005580 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
Michael Lentineb6986752015-10-06 14:56:18 -07005581 "Attachment %d is used by a later subpass and must be preserved in subpass %d.", attachment, index);
5582 }
5583 }
5584 return result;
5585}
5586
Michael Lentineb4979492015-12-22 11:36:14 -06005587VkBool32 ValidateDependencies(const layer_data* my_data, VkDevice device, const VkRenderPassCreateInfo* pCreateInfo, const std::vector<DAGNode>& subpass_to_node) {
Mark Youngb20a6a82016-01-07 15:41:43 -07005588 VkBool32 skip_call = VK_FALSE;
Michael Lentineb6986752015-10-06 14:56:18 -07005589 std::vector<std::vector<uint32_t>> output_attachment_to_subpass(pCreateInfo->attachmentCount);
5590 std::vector<std::vector<uint32_t>> input_attachment_to_subpass(pCreateInfo->attachmentCount);
Michael Lentineb6986752015-10-06 14:56:18 -07005591 // Find for each attachment the subpasses that use them.
5592 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
5593 const VkSubpassDescription& subpass = pCreateInfo->pSubpasses[i];
Chia-I Wud50a7d72015-10-26 20:48:51 +08005594 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
Michael Lentineb6986752015-10-06 14:56:18 -07005595 input_attachment_to_subpass[subpass.pInputAttachments[j].attachment].push_back(i);
5596 }
Chia-I Wud50a7d72015-10-26 20:48:51 +08005597 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
Michael Lentineb6986752015-10-06 14:56:18 -07005598 output_attachment_to_subpass[subpass.pColorAttachments[j].attachment].push_back(i);
5599 }
Chia-I Wu1efb7e52015-10-26 17:32:47 +08005600 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
5601 output_attachment_to_subpass[subpass.pDepthStencilAttachment->attachment].push_back(i);
Michael Lentineb6986752015-10-06 14:56:18 -07005602 }
5603 }
5604 // If there is a dependency needed make sure one exists
5605 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
5606 const VkSubpassDescription& subpass = pCreateInfo->pSubpasses[i];
5607 // If the attachment is an input then all subpasses that output must have a dependency relationship
Chia-I Wud50a7d72015-10-26 20:48:51 +08005608 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
Michael Lentineb6986752015-10-06 14:56:18 -07005609 const uint32_t& attachment = subpass.pInputAttachments[j].attachment;
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005610 CheckDependencyExists(my_data, device, i, output_attachment_to_subpass[attachment], subpass_to_node, skip_call);
Michael Lentineb6986752015-10-06 14:56:18 -07005611 }
5612 // If the attachment is an output then all subpasses that use the attachment must have a dependency relationship
Chia-I Wud50a7d72015-10-26 20:48:51 +08005613 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
Michael Lentineb6986752015-10-06 14:56:18 -07005614 const uint32_t& attachment = subpass.pColorAttachments[j].attachment;
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005615 CheckDependencyExists(my_data, device, i, output_attachment_to_subpass[attachment], subpass_to_node, skip_call);
5616 CheckDependencyExists(my_data, device, i, input_attachment_to_subpass[attachment], subpass_to_node, skip_call);
Michael Lentineb6986752015-10-06 14:56:18 -07005617 }
Chia-I Wu1efb7e52015-10-26 17:32:47 +08005618 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
5619 const uint32_t& attachment = subpass.pDepthStencilAttachment->attachment;
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005620 CheckDependencyExists(my_data, device, i, output_attachment_to_subpass[attachment], subpass_to_node, skip_call);
5621 CheckDependencyExists(my_data, device, i, input_attachment_to_subpass[attachment], subpass_to_node, skip_call);
Michael Lentineb6986752015-10-06 14:56:18 -07005622 }
5623 }
5624 // Loop through implicit dependencies, if this pass reads make sure the attachment is preserved for all passes after it was written.
5625 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
5626 const VkSubpassDescription& subpass = pCreateInfo->pSubpasses[i];
Chia-I Wud50a7d72015-10-26 20:48:51 +08005627 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005628 CheckPreserved(my_data, device, pCreateInfo, i, subpass.pInputAttachments[j].attachment, subpass_to_node, 0, skip_call);
Michael Lentineb6986752015-10-06 14:56:18 -07005629 }
5630 }
5631 return skip_call;
5632}
5633
Courtney Goeltzenleuchtere6dd8082015-12-16 16:07:01 -07005634VkBool32 ValidateLayouts(const layer_data* my_data, VkDevice device, const VkRenderPassCreateInfo* pCreateInfo) {
Mark Youngb20a6a82016-01-07 15:41:43 -07005635 VkBool32 skip = VK_FALSE;
Mark Lobodzinski31e5f282015-11-30 16:48:53 -07005636
5637#ifdef DISABLE_IMAGE_LAYOUT_VALIDATION
5638 return skip;
5639#endif // DISABLE_IMAGE_LAYOUT_VALIDATION
5640
Michael Lentineabc5e922015-10-12 11:30:14 -05005641 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
5642 const VkSubpassDescription& subpass = pCreateInfo->pSubpasses[i];
5643 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
5644 if (subpass.pInputAttachments[j].layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL &&
5645 subpass.pInputAttachments[j].layout != VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
5646 if (subpass.pInputAttachments[j].layout == VK_IMAGE_LAYOUT_GENERAL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07005647 // TODO: Verify Valid Use in spec. I believe this is allowed (valid) but may not be optimal performance
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005648 skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05005649 "Layout for input attachment is GENERAL but should be READ_ONLY_OPTIMAL.");
5650 } else {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005651 skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05005652 "Layout for input attachment is %d but can only be READ_ONLY_OPTIMAL or GENERAL.", subpass.pInputAttachments[j].attachment);
5653 }
5654 }
5655 }
5656 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
5657 if (subpass.pColorAttachments[j].layout != VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
5658 if (subpass.pColorAttachments[j].layout == VK_IMAGE_LAYOUT_GENERAL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07005659 // TODO: Verify Valid Use in spec. I believe this is allowed (valid) but may not be optimal performance
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005660 skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05005661 "Layout for color attachment is GENERAL but should be COLOR_ATTACHMENT_OPTIMAL.");
5662 } else {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005663 skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05005664 "Layout for color attachment is %d but can only be COLOR_ATTACHMENT_OPTIMAL or GENERAL.", subpass.pColorAttachments[j].attachment);
5665 }
5666 }
5667 }
Mark Lobodzinski0f30f9e2015-10-28 13:03:56 -06005668 if ((subpass.pDepthStencilAttachment != NULL) &&
5669 (subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
Michael Lentineabc5e922015-10-12 11:30:14 -05005670 if (subpass.pDepthStencilAttachment->layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
5671 if (subpass.pDepthStencilAttachment->layout == VK_IMAGE_LAYOUT_GENERAL) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07005672 // TODO: Verify Valid Use in spec. I believe this is allowed (valid) but may not be optimal performance
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005673 skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05005674 "Layout for depth attachment is GENERAL but should be DEPTH_STENCIL_ATTACHMENT_OPTIMAL.");
5675 } else {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005676 skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05005677 "Layout for depth attachment is %d but can only be DEPTH_STENCIL_ATTACHMENT_OPTIMAL or GENERAL.", subpass.pDepthStencilAttachment->attachment);
5678 }
5679 }
5680 }
5681 }
5682 return skip;
5683}
5684
Courtney Goeltzenleuchtere6dd8082015-12-16 16:07:01 -07005685VkBool32 CreatePassDAG(const layer_data* my_data, VkDevice device, const VkRenderPassCreateInfo* pCreateInfo, std::vector<DAGNode>& subpass_to_node, std::vector<bool>& has_self_dependency) {
Mark Youngb20a6a82016-01-07 15:41:43 -07005686 VkBool32 skip_call = VK_FALSE;
Michael Lentineabc5e922015-10-12 11:30:14 -05005687 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
5688 DAGNode& subpass_node = subpass_to_node[i];
5689 subpass_node.pass = i;
5690 }
5691 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; ++i) {
5692 const VkSubpassDependency& dependency = pCreateInfo->pDependencies[i];
Michael Lentine6bd4f122016-01-19 14:00:53 -06005693 if (dependency.srcSubpass > dependency.dstSubpass && dependency.srcSubpass != VK_SUBPASS_EXTERNAL && dependency.dstSubpass != VK_SUBPASS_EXTERNAL) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005694 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05005695 "Depedency graph must be specified such that an earlier pass cannot depend on a later pass.");
Michael Lentine6bd4f122016-01-19 14:00:53 -06005696 } else if (dependency.srcSubpass == VK_SUBPASS_EXTERNAL && dependency.dstSubpass == VK_SUBPASS_EXTERNAL) {
5697 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
5698 "The src and dest subpasses cannot both be external.");
Michael Lentine48930b82015-10-15 17:07:00 -05005699 } else if (dependency.srcSubpass == dependency.dstSubpass) {
5700 has_self_dependency[dependency.srcSubpass] = true;
Michael Lentineabc5e922015-10-12 11:30:14 -05005701 }
Michael Lentine6bd4f122016-01-19 14:00:53 -06005702 if (dependency.dstSubpass != VK_SUBPASS_EXTERNAL) {
5703 subpass_to_node[dependency.dstSubpass].prev.push_back(dependency.srcSubpass);
5704 }
5705 if (dependency.srcSubpass != VK_SUBPASS_EXTERNAL) {
5706 subpass_to_node[dependency.srcSubpass].next.push_back(dependency.dstSubpass);
5707 }
Michael Lentineabc5e922015-10-12 11:30:14 -05005708 }
5709 return skip_call;
5710}
Tobin Ehlis8fab6562015-12-01 09:57:09 -07005711// TODOSC : Add intercept of vkCreateShaderModule
Michael Lentineabc5e922015-10-12 11:30:14 -05005712
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07005713VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateShaderModule(
5714 VkDevice device,
5715 const VkShaderModuleCreateInfo *pCreateInfo,
5716 const VkAllocationCallbacks* pAllocator,
5717 VkShaderModule *pShaderModule)
5718{
5719 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Mark Youngb20a6a82016-01-07 15:41:43 -07005720 VkBool32 skip_call = VK_FALSE;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07005721 if (!shader_is_spirv(pCreateInfo)) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07005722 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005723 /* dev */ 0, __LINE__, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07005724 "Shader is not SPIR-V");
5725 }
5726
Mark Youngb20a6a82016-01-07 15:41:43 -07005727 if (VK_FALSE != skip_call)
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07005728 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07005729
5730 VkResult res = my_data->device_dispatch_table->CreateShaderModule(device, pCreateInfo, pAllocator, pShaderModule);
5731
5732 if (res == VK_SUCCESS) {
5733 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07005734 my_data->shaderModuleMap[*pShaderModule] = new shader_module(pCreateInfo);
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07005735 loader_platform_thread_unlock_mutex(&globalLock);
5736 }
5737 return res;
5738}
5739
Chia-I Wu9ab61502015-11-06 06:42:02 +08005740VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass)
Tobin Ehliseba312c2015-04-01 08:40:34 -06005741{
Mark Youngb20a6a82016-01-07 15:41:43 -07005742 VkBool32 skip_call = VK_FALSE;
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005743 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Michael Lentineabc5e922015-10-12 11:30:14 -05005744 // Create DAG
Michael Lentine48930b82015-10-15 17:07:00 -05005745 std::vector<bool> has_self_dependency(pCreateInfo->subpassCount);
Michael Lentineabc5e922015-10-12 11:30:14 -05005746 std::vector<DAGNode> subpass_to_node(pCreateInfo->subpassCount);
Michael Lentine48930b82015-10-15 17:07:00 -05005747 skip_call |= CreatePassDAG(dev_data, device, pCreateInfo, subpass_to_node, has_self_dependency);
Michael Lentineabc5e922015-10-12 11:30:14 -05005748 // Validate using DAG
5749 skip_call |= ValidateDependencies(dev_data, device, pCreateInfo, subpass_to_node);
5750 skip_call |= ValidateLayouts(dev_data, device, pCreateInfo);
Mark Youngb20a6a82016-01-07 15:41:43 -07005751 if (VK_FALSE != skip_call) {
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07005752 return VK_ERROR_VALIDATION_FAILED_EXT;
Michael Lentineb6986752015-10-06 14:56:18 -07005753 }
Chia-I Wuf7458c52015-10-26 21:10:41 +08005754 VkResult result = dev_data->device_dispatch_table->CreateRenderPass(device, pCreateInfo, pAllocator, pRenderPass);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06005755 if (VK_SUCCESS == result) {
Tobin Ehlis8fab6562015-12-01 09:57:09 -07005756 // TODOSC : Merge in tracking of renderpass from ShaderChecker
Tobin Ehliseba312c2015-04-01 08:40:34 -06005757 // Shadow create info and store in map
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06005758 VkRenderPassCreateInfo* localRPCI = new VkRenderPassCreateInfo(*pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +08005759 if (pCreateInfo->pAttachments) {
5760 localRPCI->pAttachments = new VkAttachmentDescription[localRPCI->attachmentCount];
5761 memcpy((void*)localRPCI->pAttachments, pCreateInfo->pAttachments, localRPCI->attachmentCount*sizeof(VkAttachmentDescription));
Tobin Ehliseba312c2015-04-01 08:40:34 -06005762 }
Chia-I Wu08accc62015-07-07 11:50:03 +08005763 if (pCreateInfo->pSubpasses) {
5764 localRPCI->pSubpasses = new VkSubpassDescription[localRPCI->subpassCount];
5765 memcpy((void*)localRPCI->pSubpasses, pCreateInfo->pSubpasses, localRPCI->subpassCount*sizeof(VkSubpassDescription));
5766
5767 for (uint32_t i = 0; i < localRPCI->subpassCount; i++) {
5768 VkSubpassDescription *subpass = (VkSubpassDescription *) &localRPCI->pSubpasses[i];
Chia-I Wud50a7d72015-10-26 20:48:51 +08005769 const uint32_t attachmentCount = subpass->inputAttachmentCount +
5770 subpass->colorAttachmentCount * (1 + (subpass->pResolveAttachments?1:0)) +
Chia-I Wu1efb7e52015-10-26 17:32:47 +08005771 ((subpass->pDepthStencilAttachment) ? 1 : 0) + subpass->preserveAttachmentCount;
Chia-I Wu08accc62015-07-07 11:50:03 +08005772 VkAttachmentReference *attachments = new VkAttachmentReference[attachmentCount];
5773
Cody Northropa505dda2015-08-04 11:16:41 -06005774 memcpy(attachments, subpass->pInputAttachments,
Chia-I Wud50a7d72015-10-26 20:48:51 +08005775 sizeof(attachments[0]) * subpass->inputAttachmentCount);
Cody Northropa505dda2015-08-04 11:16:41 -06005776 subpass->pInputAttachments = attachments;
Chia-I Wud50a7d72015-10-26 20:48:51 +08005777 attachments += subpass->inputAttachmentCount;
Chia-I Wu08accc62015-07-07 11:50:03 +08005778
Cody Northropa505dda2015-08-04 11:16:41 -06005779 memcpy(attachments, subpass->pColorAttachments,
Chia-I Wud50a7d72015-10-26 20:48:51 +08005780 sizeof(attachments[0]) * subpass->colorAttachmentCount);
Cody Northropa505dda2015-08-04 11:16:41 -06005781 subpass->pColorAttachments = attachments;
Chia-I Wud50a7d72015-10-26 20:48:51 +08005782 attachments += subpass->colorAttachmentCount;
Chia-I Wu08accc62015-07-07 11:50:03 +08005783
Cody Northropa505dda2015-08-04 11:16:41 -06005784 if (subpass->pResolveAttachments) {
5785 memcpy(attachments, subpass->pResolveAttachments,
Chia-I Wud50a7d72015-10-26 20:48:51 +08005786 sizeof(attachments[0]) * subpass->colorAttachmentCount);
Cody Northropa505dda2015-08-04 11:16:41 -06005787 subpass->pResolveAttachments = attachments;
Chia-I Wud50a7d72015-10-26 20:48:51 +08005788 attachments += subpass->colorAttachmentCount;
Chia-I Wu08accc62015-07-07 11:50:03 +08005789 }
5790
Chia-I Wu1efb7e52015-10-26 17:32:47 +08005791 if (subpass->pDepthStencilAttachment) {
5792 memcpy(attachments, subpass->pDepthStencilAttachment,
5793 sizeof(attachments[0]) * 1);
5794 subpass->pDepthStencilAttachment = attachments;
5795 attachments += 1;
5796 }
5797
Cody Northropa505dda2015-08-04 11:16:41 -06005798 memcpy(attachments, subpass->pPreserveAttachments,
Chia-I Wud50a7d72015-10-26 20:48:51 +08005799 sizeof(attachments[0]) * subpass->preserveAttachmentCount);
Jon Ashburnf19916e2016-01-11 13:12:43 -07005800 subpass->pPreserveAttachments = &attachments->attachment;
Chia-I Wu08accc62015-07-07 11:50:03 +08005801 }
Tobin Ehliseba312c2015-04-01 08:40:34 -06005802 }
Chia-I Wu08accc62015-07-07 11:50:03 +08005803 if (pCreateInfo->pDependencies) {
5804 localRPCI->pDependencies = new VkSubpassDependency[localRPCI->dependencyCount];
5805 memcpy((void*)localRPCI->pDependencies, pCreateInfo->pDependencies, localRPCI->dependencyCount*sizeof(VkSubpassDependency));
Tobin Ehliseba312c2015-04-01 08:40:34 -06005806 }
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07005807 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07005808 dev_data->renderPassMap[*pRenderPass] = new RENDER_PASS_NODE(localRPCI);
Michael Lentine48930b82015-10-15 17:07:00 -05005809 dev_data->renderPassMap[*pRenderPass]->hasSelfDependency = has_self_dependency;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07005810 loader_platform_thread_unlock_mutex(&globalLock);
Tobin Ehliseba312c2015-04-01 08:40:34 -06005811 }
5812 return result;
5813}
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06005814// Free the renderpass shadow
5815static void deleteRenderPasses(layer_data* my_data)
5816{
5817 if (my_data->renderPassMap.size() <= 0)
5818 return;
5819 for (auto ii=my_data->renderPassMap.begin(); ii!=my_data->renderPassMap.end(); ++ii) {
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07005820 const VkRenderPassCreateInfo* pRenderPassInfo = (*ii).second->pCreateInfo;
Michael Lentine48930b82015-10-15 17:07:00 -05005821 if (pRenderPassInfo->pAttachments) {
5822 delete[] pRenderPassInfo->pAttachments;
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06005823 }
Michael Lentine48930b82015-10-15 17:07:00 -05005824 if (pRenderPassInfo->pSubpasses) {
5825 for (uint32_t i=0; i<pRenderPassInfo->subpassCount; ++i) {
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06005826 // Attachements are all allocated in a block, so just need to
5827 // find the first non-null one to delete
Michael Lentine48930b82015-10-15 17:07:00 -05005828 if (pRenderPassInfo->pSubpasses[i].pInputAttachments) {
5829 delete[] pRenderPassInfo->pSubpasses[i].pInputAttachments;
5830 } else if (pRenderPassInfo->pSubpasses[i].pColorAttachments) {
5831 delete[] pRenderPassInfo->pSubpasses[i].pColorAttachments;
5832 } else if (pRenderPassInfo->pSubpasses[i].pResolveAttachments) {
5833 delete[] pRenderPassInfo->pSubpasses[i].pResolveAttachments;
5834 } else if (pRenderPassInfo->pSubpasses[i].pPreserveAttachments) {
5835 delete[] pRenderPassInfo->pSubpasses[i].pPreserveAttachments;
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06005836 }
5837 }
Michael Lentine48930b82015-10-15 17:07:00 -05005838 delete[] pRenderPassInfo->pSubpasses;
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06005839 }
Michael Lentine48930b82015-10-15 17:07:00 -05005840 if (pRenderPassInfo->pDependencies) {
5841 delete[] pRenderPassInfo->pDependencies;
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06005842 }
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07005843 delete pRenderPassInfo;
Michael Lentine48930b82015-10-15 17:07:00 -05005844 delete (*ii).second;
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06005845 }
5846 my_data->renderPassMap.clear();
5847}
Michael Lentineabc5e922015-10-12 11:30:14 -05005848
Courtney Goeltzenleuchtere6dd8082015-12-16 16:07:01 -07005849VkBool32 VerifyFramebufferAndRenderPassLayouts(VkCommandBuffer cmdBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
Mark Youngb20a6a82016-01-07 15:41:43 -07005850 VkBool32 skip_call = VK_FALSE;
Michael Lentineabc5e922015-10-12 11:30:14 -05005851 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
5852 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, cmdBuffer);
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07005853 const VkRenderPassCreateInfo* pRenderPassInfo = dev_data->renderPassMap[pRenderPassBegin->renderPass]->pCreateInfo;
Michael Lentineabc5e922015-10-12 11:30:14 -05005854 const VkFramebufferCreateInfo* pFramebufferInfo = dev_data->frameBufferMap[pRenderPassBegin->framebuffer];
5855 if (pRenderPassInfo->attachmentCount != pFramebufferInfo->attachmentCount) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005856 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05005857 "You cannot start a render pass using a framebuffer with a different number of attachments.");
5858 }
5859 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
5860 const VkImageView& image_view = pFramebufferInfo->pAttachments[i];
5861 const VkImage& image = dev_data->imageViewMap[image_view]->image;
5862 auto image_data = pCB->imageLayoutMap.find(image);
5863 if (image_data == pCB->imageLayoutMap.end()) {
5864 pCB->imageLayoutMap[image].initialLayout = pRenderPassInfo->pAttachments[i].initialLayout;
5865 pCB->imageLayoutMap[image].layout = pRenderPassInfo->pAttachments[i].initialLayout;
5866 } else if (pRenderPassInfo->pAttachments[i].initialLayout != image_data->second.layout) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005867 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05005868 "You cannot start a render pass using attachment %i where the intial layout differs from the starting layout.", i);
5869 }
5870 }
5871 return skip_call;
5872}
5873
5874void TransitionSubpassLayouts(VkCommandBuffer cmdBuffer, const VkRenderPassBeginInfo* pRenderPassBegin, const int subpass_index) {
5875 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
5876 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, cmdBuffer);
5877 auto render_pass_data = dev_data->renderPassMap.find(pRenderPassBegin->renderPass);
5878 if (render_pass_data == dev_data->renderPassMap.end()) {
5879 return;
5880 }
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07005881 const VkRenderPassCreateInfo* pRenderPassInfo = render_pass_data->second->pCreateInfo;
Michael Lentineabc5e922015-10-12 11:30:14 -05005882 auto framebuffer_data = dev_data->frameBufferMap.find(pRenderPassBegin->framebuffer);
5883 if (framebuffer_data == dev_data->frameBufferMap.end()) {
5884 return;
5885 }
5886 const VkFramebufferCreateInfo* pFramebufferInfo = framebuffer_data->second;
5887 const VkSubpassDescription& subpass = pRenderPassInfo->pSubpasses[subpass_index];
5888 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
5889 const VkImageView& image_view = pFramebufferInfo->pAttachments[subpass.pInputAttachments[j].attachment];
5890 auto image_view_data = dev_data->imageViewMap.find(image_view);
5891 if (image_view_data != dev_data->imageViewMap.end()) {
5892 auto image_layout = pCB->imageLayoutMap.find(image_view_data->second->image);
5893 if (image_layout != pCB->imageLayoutMap.end()) {
5894 image_layout->second.layout = subpass.pInputAttachments[j].layout;
5895 }
5896 }
5897 }
5898 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
5899 const VkImageView& image_view = pFramebufferInfo->pAttachments[subpass.pColorAttachments[j].attachment];
5900 auto image_view_data = dev_data->imageViewMap.find(image_view);
5901 if (image_view_data != dev_data->imageViewMap.end()) {
5902 auto image_layout = pCB->imageLayoutMap.find(image_view_data->second->image);
5903 if (image_layout != pCB->imageLayoutMap.end()) {
5904 image_layout->second.layout = subpass.pColorAttachments[j].layout;
5905 }
5906 }
5907 }
Michael Lentine3dea6512015-10-28 15:55:18 -07005908 if ((subpass.pDepthStencilAttachment != NULL) &&
Mark Lobodzinski0f30f9e2015-10-28 13:03:56 -06005909 (subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
Michael Lentineabc5e922015-10-12 11:30:14 -05005910 const VkImageView& image_view = pFramebufferInfo->pAttachments[subpass.pDepthStencilAttachment->attachment];
5911 auto image_view_data = dev_data->imageViewMap.find(image_view);
5912 if (image_view_data != dev_data->imageViewMap.end()) {
5913 auto image_layout = pCB->imageLayoutMap.find(image_view_data->second->image);
5914 if (image_layout != pCB->imageLayoutMap.end()) {
5915 image_layout->second.layout = subpass.pDepthStencilAttachment->layout;
5916 }
5917 }
5918 }
5919}
5920
Courtney Goeltzenleuchtere6dd8082015-12-16 16:07:01 -07005921VkBool32 validatePrimaryCommandBuffer(const layer_data* my_data, const GLOBAL_CB_NODE* pCB, const std::string& cmd_name) {
Mark Youngb20a6a82016-01-07 15:41:43 -07005922 VkBool32 skip_call = VK_FALSE;
Michael Lentine3dea6512015-10-28 15:55:18 -07005923 if (pCB->createInfo.level != VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005924 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
Michael Lentine3dea6512015-10-28 15:55:18 -07005925 "Cannot execute command %s on a secondary command buffer.", cmd_name.c_str());
5926 }
5927 return skip_call;
5928}
5929
Michael Lentineabc5e922015-10-12 11:30:14 -05005930void TransitionFinalSubpassLayouts(VkCommandBuffer cmdBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
5931 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
5932 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, cmdBuffer);
5933 auto render_pass_data = dev_data->renderPassMap.find(pRenderPassBegin->renderPass);
5934 if (render_pass_data == dev_data->renderPassMap.end()) {
5935 return;
5936 }
Tobin Ehlis3f5ddbb2015-12-02 13:53:34 -07005937 const VkRenderPassCreateInfo* pRenderPassInfo = render_pass_data->second->pCreateInfo;
Michael Lentineabc5e922015-10-12 11:30:14 -05005938 auto framebuffer_data = dev_data->frameBufferMap.find(pRenderPassBegin->framebuffer);
5939 if (framebuffer_data == dev_data->frameBufferMap.end()) {
5940 return;
5941 }
5942 const VkFramebufferCreateInfo* pFramebufferInfo = framebuffer_data->second;
5943 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
5944 const VkImageView& image_view = pFramebufferInfo->pAttachments[i];
5945 auto image_view_data = dev_data->imageViewMap.find(image_view);
5946 if (image_view_data != dev_data->imageViewMap.end()) {
5947 auto image_layout = pCB->imageLayoutMap.find(image_view_data->second->image);
5948 if (image_layout != pCB->imageLayoutMap.end()) {
5949 image_layout->second.layout = pRenderPassInfo->pAttachments[i].finalLayout;
5950 }
5951 }
5952 }
5953}
5954
Chia-I Wu9ab61502015-11-06 06:42:02 +08005955VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, VkSubpassContents contents)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005956{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005957 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005958 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5959 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005960 if (pCB) {
Tobin Ehlis259730a2015-06-23 16:13:03 -06005961 if (pRenderPassBegin && pRenderPassBegin->renderPass) {
Mark Lobodzinski0f30f9e2015-10-28 13:03:56 -06005962 skipCall |= VerifyFramebufferAndRenderPassLayouts(commandBuffer, pRenderPassBegin);
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06005963 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdBeginRenderPass");
Michael Lentine3dea6512015-10-28 15:55:18 -07005964 skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdBeginRenderPass");
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005965 skipCall |= addCmd(dev_data, pCB, CMD_BEGINRENDERPASS, "vkCmdBeginRenderPass()");
Mark Lobodzinski5495d132015-09-30 16:19:16 -06005966 pCB->activeRenderPass = pRenderPassBegin->renderPass;
Michael Lentineabc5e922015-10-12 11:30:14 -05005967 // This is a shallow copy as that is all that is needed for now
5968 pCB->activeRenderPassBeginInfo = *pRenderPassBegin;
Mark Lobodzinski5495d132015-09-30 16:19:16 -06005969 pCB->activeSubpass = 0;
Michael Lentine2e068b22015-12-29 16:05:27 -06005970 pCB->activeSubpassContents = contents;
Mark Lobodzinski5495d132015-09-30 16:19:16 -06005971 pCB->framebuffer = pRenderPassBegin->framebuffer;
Tobin Ehlis502480b2015-06-24 15:53:07 -06005972 } else {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07005973 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
Tobin Ehlis259730a2015-06-23 16:13:03 -06005974 "You cannot use a NULL RenderPass object in vkCmdBeginRenderPass()");
Tony Barbourbadda992015-04-06 11:09:26 -06005975 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005976 }
Mark Lobodzinski0f30f9e2015-10-28 13:03:56 -06005977 if (VK_FALSE == skipCall) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005978 dev_data->device_dispatch_table->CmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski0f30f9e2015-10-28 13:03:56 -06005979 // This is a shallow copy as that is all that is needed for now
5980 dev_data->renderPassBeginInfo = *pRenderPassBegin;
5981 dev_data->currentSubpass = 0;
5982 }
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06005983}
5984
Chia-I Wu9ab61502015-11-06 06:42:02 +08005985VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents)
Chia-I Wu08accc62015-07-07 11:50:03 +08005986{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06005987 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08005988 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
5989 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Michael Lentineabc5e922015-10-12 11:30:14 -05005990 TransitionSubpassLayouts(commandBuffer, &dev_data->renderPassBeginInfo, ++dev_data->currentSubpass);
Chia-I Wu08accc62015-07-07 11:50:03 +08005991 if (pCB) {
Michael Lentine3dea6512015-10-28 15:55:18 -07005992 skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdNextSubpass");
Tobin Ehlis61b36f32015-12-16 08:19:42 -07005993 skipCall |= addCmd(dev_data, pCB, CMD_NEXTSUBPASS, "vkCmdNextSubpass()");
Mark Lobodzinski5495d132015-09-30 16:19:16 -06005994 pCB->activeSubpass++;
Michael Lentine2e068b22015-12-29 16:05:27 -06005995 pCB->activeSubpassContents = contents;
Tony Barbourfc5bb6f2016-01-12 11:16:52 -07005996 TransitionSubpassLayouts(commandBuffer, &pCB->activeRenderPassBeginInfo, pCB->activeSubpass);
Mark Lobodzinski5495d132015-09-30 16:19:16 -06005997 if (pCB->lastBoundPipeline) {
Tobin Ehlisb212dfc2015-10-07 15:40:22 -06005998 skipCall |= validatePipelineState(dev_data, pCB, VK_PIPELINE_BIND_POINT_GRAPHICS, pCB->lastBoundPipeline);
Chia-I Wu08accc62015-07-07 11:50:03 +08005999 }
Tobin Ehlis432ef5c2015-10-20 17:06:16 -06006000 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdNextSubpass");
Chia-I Wu08accc62015-07-07 11:50:03 +08006001 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06006002 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006003 dev_data->device_dispatch_table->CmdNextSubpass(commandBuffer, contents);
Chia-I Wu08accc62015-07-07 11:50:03 +08006004}
6005
Chia-I Wu9ab61502015-11-06 06:42:02 +08006006VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdEndRenderPass(VkCommandBuffer commandBuffer)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006007{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06006008 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006009 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
6010 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Michael Lentineabc5e922015-10-12 11:30:14 -05006011 TransitionFinalSubpassLayouts(commandBuffer, &dev_data->renderPassBeginInfo);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006012 if (pCB) {
Michael Lentine3dea6512015-10-28 15:55:18 -07006013 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdEndRenderpass");
Michael Lentine3dea6512015-10-28 15:55:18 -07006014 skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdEndRenderPass");
Tobin Ehlis61b36f32015-12-16 08:19:42 -07006015 skipCall |= addCmd(dev_data, pCB, CMD_ENDRENDERPASS, "vkCmdEndRenderPass()");
Michael Lentineabc5e922015-10-12 11:30:14 -05006016 TransitionFinalSubpassLayouts(commandBuffer, &pCB->activeRenderPassBeginInfo);
Mark Lobodzinski5495d132015-09-30 16:19:16 -06006017 pCB->activeRenderPass = 0;
6018 pCB->activeSubpass = 0;
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08006019 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06006020 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006021 dev_data->device_dispatch_table->CmdEndRenderPass(commandBuffer);
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08006022}
6023
Chia-I Wu9ab61502015-11-06 06:42:02 +08006024VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBuffersCount, const VkCommandBuffer* pCommandBuffers)
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08006025{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06006026 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006027 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
6028 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08006029 if (pCB) {
Tobin Ehlis4b34ddc2015-09-17 14:18:16 -06006030 GLOBAL_CB_NODE* pSubCB = NULL;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006031 for (uint32_t i=0; i<commandBuffersCount; i++) {
6032 pSubCB = getCBNode(dev_data, pCommandBuffers[i]);
Tobin Ehlis4b34ddc2015-09-17 14:18:16 -06006033 if (!pSubCB) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07006034 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006035 "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p in element %u of pCommandBuffers array.", (void*)pCommandBuffers[i], i);
6036 } else if (VK_COMMAND_BUFFER_LEVEL_PRIMARY == pSubCB->createInfo.level) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07006037 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006038 "vkCmdExecuteCommands() called w/ Primary Cmd Buffer %p in element %u of pCommandBuffers array. All cmd buffers in pCommandBuffers array must be secondary.", (void*)pCommandBuffers[i], i);
Tobin Ehlis651d9b02015-12-16 05:01:22 -07006039 } else if (pCB->activeRenderPass) { // Secondary CB w/i RenderPass must have *CONTINUE_BIT set
6040 if (!(pSubCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07006041 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)pCommandBuffers[i], __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
Tobin Ehlis651d9b02015-12-16 05:01:22 -07006042 "vkCmdExecuteCommands(): Secondary Command Buffer (%p) executed within render pass (%#" PRIxLEAST64 ") must have had vkBeginCommandBuffer() called w/ VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT set.", (void*)pCommandBuffers[i], (uint64_t)pCB->activeRenderPass);
6043 }
6044 string errorString = "";
Jon Ashburnf19916e2016-01-11 13:12:43 -07006045 if (!verify_renderpass_compatibility(dev_data, pCB->activeRenderPass, pSubCB->beginInfo.pInheritanceInfo->renderPass, errorString)) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07006046 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)pCommandBuffers[i], __LINE__, DRAWSTATE_RENDERPASS_INCOMPATIBLE, "DS",
Tobin Ehlis651d9b02015-12-16 05:01:22 -07006047 "vkCmdExecuteCommands(): Secondary Command Buffer (%p) w/ render pass (%#" PRIxLEAST64 ") is incompatible w/ primary command buffer (%p) w/ render pass (%#" PRIxLEAST64 ") due to: %s",
Jon Ashburnf19916e2016-01-11 13:12:43 -07006048 (void*)pCommandBuffers[i], (uint64_t)pSubCB->beginInfo.pInheritanceInfo->renderPass, (void*)commandBuffer, (uint64_t)pCB->activeRenderPass, errorString.c_str());
Tobin Ehlis651d9b02015-12-16 05:01:22 -07006049 }
6050 // If framebuffer for secondary CB is not NULL, then it must match FB from vkCmdBeginRenderPass()
6051 // that this CB will be executed in AND framebuffer must have been created w/ RP compatible w/ renderpass
Jon Ashburnf19916e2016-01-11 13:12:43 -07006052 if (pSubCB->beginInfo.pInheritanceInfo->framebuffer) {
6053 if (pSubCB->beginInfo.pInheritanceInfo->framebuffer != pCB->activeRenderPassBeginInfo.framebuffer) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07006054 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)pCommandBuffers[i], __LINE__, DRAWSTATE_FRAMEBUFFER_INCOMPATIBLE, "DS",
Tobin Ehlis651d9b02015-12-16 05:01:22 -07006055 "vkCmdExecuteCommands(): Secondary Command Buffer (%p) references framebuffer (%#" PRIxLEAST64 ") that does not match framebuffer (%#" PRIxLEAST64 ") in active renderpass (%#" PRIxLEAST64 ").",
Jon Ashburnf19916e2016-01-11 13:12:43 -07006056 (void*)pCommandBuffers[i], (uint64_t)pSubCB->beginInfo.pInheritanceInfo->framebuffer, (uint64_t)pCB->activeRenderPassBeginInfo.framebuffer, (uint64_t)pCB->activeRenderPass);
Tobin Ehlis651d9b02015-12-16 05:01:22 -07006057 }
6058 }
Tobin Ehlis4b34ddc2015-09-17 14:18:16 -06006059 }
Tobin Ehlisf4aafc02016-01-15 13:34:44 -07006060 // Secondary cmdBuffers are considered pending execution starting w/ being recorded
6061 if (!(pSubCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
Tobin Ehlis74714d22016-01-25 15:24:34 -08006062 if (dev_data->globalInFlightCmdBuffers.find(pSubCB->commandBuffer) != dev_data->globalInFlightCmdBuffers.end()) {
Mark Youngee3f3a22016-01-25 12:18:32 -07006063 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_CB_SIMULTANEOUS_USE, "DS",
6064 "Attempt to simultaneously execute CB %#" PRIxLEAST64 " w/o VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT set!", (uint64_t)(pCB->commandBuffer));
Tobin Ehlisf4aafc02016-01-15 13:34:44 -07006065 }
6066 if (pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
6067 // Warn that non-simultaneous secondary cmd buffer renders primary non-simultaneous
Mark Youngee3f3a22016-01-25 12:18:32 -07006068 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)(pCommandBuffers[i]), __LINE__, DRAWSTATE_INVALID_CB_SIMULTANEOUS_USE, "DS",
Tobin Ehlisf4aafc02016-01-15 13:34:44 -07006069 "vkCmdExecuteCommands(): Secondary Command Buffer (%#" PRIxLEAST64 ") does not have VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT set and will cause primary command buffer (%#" PRIxLEAST64 ") to be treated as if it does not have VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT set, even though it does.",
Mark Youngee3f3a22016-01-25 12:18:32 -07006070 (uint64_t)(pCommandBuffers[i]), (uint64_t)(pCB->commandBuffer));
Tobin Ehlisf4aafc02016-01-15 13:34:44 -07006071 pCB->beginInfo.flags &= ~VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
6072 }
6073 }
6074 pCB->secondaryCommandBuffers.insert(pSubCB->commandBuffer);
Tobin Ehlis74714d22016-01-25 15:24:34 -08006075 dev_data->globalInFlightCmdBuffers.insert(pSubCB->commandBuffer);
Tobin Ehlis4b34ddc2015-09-17 14:18:16 -06006076 }
Michael Lentine3dea6512015-10-28 15:55:18 -07006077 skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdExecuteComands");
Tobin Ehlis61b36f32015-12-16 08:19:42 -07006078 skipCall |= addCmd(dev_data, pCB, CMD_EXECUTECOMMANDS, "vkCmdExecuteComands()");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006079 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06006080 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006081 dev_data->device_dispatch_table->CmdExecuteCommands(commandBuffer, commandBuffersCount, pCommandBuffers);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006082}
6083
Courtney Goeltzenleuchtere6dd8082015-12-16 16:07:01 -07006084VkBool32 ValidateMapImageLayouts(VkDevice device, VkDeviceMemory mem) {
Mark Youngb20a6a82016-01-07 15:41:43 -07006085 VkBool32 skip_call = VK_FALSE;
Michael Lentine7b236262015-10-23 12:41:44 -07006086 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6087 auto mem_data = dev_data->memImageMap.find(mem);
6088 if (mem_data != dev_data->memImageMap.end()) {
6089 auto image_data = dev_data->imageLayoutMap.find(mem_data->second);
6090 if (image_data != dev_data->imageLayoutMap.end()) {
6091 if (image_data->second->layout != VK_IMAGE_LAYOUT_PREINITIALIZED && image_data->second->layout != VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07006092 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT) 0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentine7b236262015-10-23 12:41:44 -07006093 "Cannot map an image with layout %d. Only GENERAL or PREINITIALIZED are supported.", image_data->second->layout);
6094 }
6095 }
6096 }
6097 return skip_call;
6098}
6099
Chia-I Wu9ab61502015-11-06 06:42:02 +08006100VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkMapMemory(
Michael Lentine7b236262015-10-23 12:41:44 -07006101 VkDevice device,
6102 VkDeviceMemory mem,
6103 VkDeviceSize offset,
6104 VkDeviceSize size,
6105 VkFlags flags,
6106 void **ppData)
6107{
6108 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinski31e5f282015-11-30 16:48:53 -07006109
Courtney Goeltzenleuchtere6dd8082015-12-16 16:07:01 -07006110 VkBool32 skip_call = VK_FALSE;
Mark Lobodzinski31e5f282015-11-30 16:48:53 -07006111#ifndef DISABLE_IMAGE_LAYOUT_VALIDATION
6112 skip_call = ValidateMapImageLayouts(device, mem);
6113#endif // DISABLE_IMAGE_LAYOUT_VALIDATION
6114
Michael Lentine7b236262015-10-23 12:41:44 -07006115 if (VK_FALSE == skip_call) {
6116 return dev_data->device_dispatch_table->MapMemory(device, mem, offset, size, flags, ppData);
6117 }
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07006118 return VK_ERROR_VALIDATION_FAILED_EXT;
Michael Lentine7b236262015-10-23 12:41:44 -07006119}
6120
Chia-I Wu9ab61502015-11-06 06:42:02 +08006121VKAPI_ATTR VkResult VKAPI_CALL vkBindImageMemory(
Michael Lentine7b236262015-10-23 12:41:44 -07006122 VkDevice device,
6123 VkImage image,
6124 VkDeviceMemory mem,
6125 VkDeviceSize memOffset)
6126{
6127 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6128 VkResult result = dev_data->device_dispatch_table->BindImageMemory(device, image, mem, memOffset);
6129 loader_platform_thread_lock_mutex(&globalLock);
6130 dev_data->memImageMap[mem] = image;
6131 loader_platform_thread_unlock_mutex(&globalLock);
6132 return result;
6133}
6134
Michael Lentineb887b0a2015-12-29 14:12:11 -06006135
6136VKAPI_ATTR VkResult VKAPI_CALL vkSetEvent(VkDevice device, VkEvent event) {
6137 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6138 dev_data->eventMap[event].needsSignaled = false;
6139 VkResult result = dev_data->device_dispatch_table->SetEvent(device, event);
6140 return result;
6141}
6142
Michael Lentine15a47882016-01-06 10:05:48 -06006143VKAPI_ATTR VkResult VKAPI_CALL vkQueueBindSparse(
6144 VkQueue queue,
6145 uint32_t bindInfoCount,
6146 const VkBindSparseInfo* pBindInfo,
6147 VkFence fence)
6148{
6149 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
Mark Youngb20a6a82016-01-07 15:41:43 -07006150 VkBool32 skip_call = VK_FALSE;
Michael Lentine15a47882016-01-06 10:05:48 -06006151
6152 for (uint32_t bindIdx=0; bindIdx < bindInfoCount; ++bindIdx) {
6153 const VkBindSparseInfo& bindInfo = pBindInfo[bindIdx];
6154 for (uint32_t i=0; i < bindInfo.waitSemaphoreCount; ++i) {
6155 if (dev_data->semaphoreSignaledMap[bindInfo.pWaitSemaphores[i]]) {
6156 dev_data->semaphoreSignaledMap[bindInfo.pWaitSemaphores[i]] = 0;
6157 } else {
6158 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
6159 "Queue %#" PRIx64 " is waiting on semaphore %#" PRIx64 " that has no way to be signaled.",
Mark Youngee3f3a22016-01-25 12:18:32 -07006160 (uint64_t)(queue), (uint64_t)(bindInfo.pWaitSemaphores[i]));
Michael Lentine15a47882016-01-06 10:05:48 -06006161 }
6162 }
6163 for (uint32_t i=0; i < bindInfo.signalSemaphoreCount; ++i) {
6164 dev_data->semaphoreSignaledMap[bindInfo.pSignalSemaphores[i]] = 1;
6165 }
6166 }
6167
Mark Youngb20a6a82016-01-07 15:41:43 -07006168 if (VK_FALSE == skip_call)
Michael Lentine15a47882016-01-06 10:05:48 -06006169 return dev_data->device_dispatch_table->QueueBindSparse(queue, bindInfoCount, pBindInfo, fence);
Mark Youngb20a6a82016-01-07 15:41:43 -07006170 else
6171 return VK_ERROR_VALIDATION_FAILED_EXT;
Michael Lentine15a47882016-01-06 10:05:48 -06006172}
6173
6174VKAPI_ATTR VkResult VKAPI_CALL vkCreateSemaphore(
6175 VkDevice device,
6176 const VkSemaphoreCreateInfo* pCreateInfo,
6177 const VkAllocationCallbacks* pAllocator,
6178 VkSemaphore* pSemaphore)
6179{
6180 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6181 VkResult result = dev_data->device_dispatch_table->CreateSemaphore(device, pCreateInfo, pAllocator, pSemaphore);
6182 if (result == VK_SUCCESS) {
6183 dev_data->semaphoreSignaledMap[*pSemaphore] = 0;
6184 }
Tobin Ehlis51b78af2016-01-06 10:27:47 -07006185 return result;
Michael Lentine15a47882016-01-06 10:05:48 -06006186}
6187
Chia-I Wu9ab61502015-11-06 06:42:02 +08006188VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR(
Michael Lentineabc5e922015-10-12 11:30:14 -05006189 VkDevice device,
6190 const VkSwapchainCreateInfoKHR *pCreateInfo,
Ian Elliott05846062015-11-20 14:13:17 -07006191 const VkAllocationCallbacks *pAllocator,
Michael Lentineabc5e922015-10-12 11:30:14 -05006192 VkSwapchainKHR *pSwapchain)
6193{
6194 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Ian Elliott05846062015-11-20 14:13:17 -07006195 VkResult result = dev_data->device_dispatch_table->CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
Michael Lentineabc5e922015-10-12 11:30:14 -05006196
6197 if (VK_SUCCESS == result) {
Tobin Ehlis75cd1c52016-01-21 10:21:04 -07006198 SWAPCHAIN_NODE *swapchain_data = new SWAPCHAIN_NODE(pCreateInfo);
Michael Lentineabc5e922015-10-12 11:30:14 -05006199 loader_platform_thread_lock_mutex(&globalLock);
6200 dev_data->device_extensions.swapchainMap[*pSwapchain] = swapchain_data;
6201 loader_platform_thread_unlock_mutex(&globalLock);
6202 }
6203
6204 return result;
6205}
6206
Ian Elliott05846062015-11-20 14:13:17 -07006207VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroySwapchainKHR(
Michael Lentineabc5e922015-10-12 11:30:14 -05006208 VkDevice device,
Ian Elliott05846062015-11-20 14:13:17 -07006209 VkSwapchainKHR swapchain,
6210 const VkAllocationCallbacks *pAllocator)
Michael Lentineabc5e922015-10-12 11:30:14 -05006211{
6212 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Michael Lentineabc5e922015-10-12 11:30:14 -05006213
6214 loader_platform_thread_lock_mutex(&globalLock);
6215 auto swapchain_data = dev_data->device_extensions.swapchainMap.find(swapchain);
6216 if (swapchain_data != dev_data->device_extensions.swapchainMap.end()) {
6217 if (swapchain_data->second->images.size() > 0) {
6218 for (auto swapchain_image : swapchain_data->second->images) {
6219 auto image_item = dev_data->imageLayoutMap.find(swapchain_image);
6220 if (image_item != dev_data->imageLayoutMap.end())
6221 dev_data->imageLayoutMap.erase(image_item);
6222 }
6223 }
6224 delete swapchain_data->second;
6225 dev_data->device_extensions.swapchainMap.erase(swapchain);
6226 }
6227 loader_platform_thread_unlock_mutex(&globalLock);
Ian Elliott05846062015-11-20 14:13:17 -07006228 return dev_data->device_dispatch_table->DestroySwapchainKHR(device, swapchain, pAllocator);
Michael Lentineabc5e922015-10-12 11:30:14 -05006229}
6230
Chia-I Wu9ab61502015-11-06 06:42:02 +08006231VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainImagesKHR(
Michael Lentineabc5e922015-10-12 11:30:14 -05006232 VkDevice device,
6233 VkSwapchainKHR swapchain,
6234 uint32_t* pCount,
6235 VkImage* pSwapchainImages)
6236{
6237 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6238 VkResult result = dev_data->device_dispatch_table->GetSwapchainImagesKHR(device, swapchain, pCount, pSwapchainImages);
6239
6240 if (result == VK_SUCCESS && pSwapchainImages != NULL) {
6241 // This should never happen and is checked by param checker.
6242 if (!pCount) return result;
6243 for (uint32_t i = 0; i < *pCount; ++i) {
6244 IMAGE_NODE* image_node = new IMAGE_NODE;
6245 image_node->layout = VK_IMAGE_LAYOUT_UNDEFINED;
6246 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis75cd1c52016-01-21 10:21:04 -07006247 auto swapchain_node = dev_data->device_extensions.swapchainMap[swapchain];
6248 image_node->format = swapchain_node->createInfo.imageFormat;
6249 swapchain_node->images.push_back(pSwapchainImages[i]);
Michael Lentineabc5e922015-10-12 11:30:14 -05006250 dev_data->imageLayoutMap[pSwapchainImages[i]] = image_node;
6251 loader_platform_thread_unlock_mutex(&globalLock);
6252 }
6253 }
6254 return result;
6255}
6256
Ian Elliott05846062015-11-20 14:13:17 -07006257VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo)
Michael Lentineabc5e922015-10-12 11:30:14 -05006258{
6259 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
Mark Youngb20a6a82016-01-07 15:41:43 -07006260 VkBool32 skip_call = VK_FALSE;
Michael Lentineabc5e922015-10-12 11:30:14 -05006261
Mark Lobodzinski31e5f282015-11-30 16:48:53 -07006262#ifndef DISABLE_IMAGE_LAYOUT_VALIDATION
Michael Lentineabc5e922015-10-12 11:30:14 -05006263 if (pPresentInfo) {
Michael Lentine15a47882016-01-06 10:05:48 -06006264 for (uint32_t i=0; i < pPresentInfo->waitSemaphoreCount; ++i) {
6265 if (dev_data->semaphoreSignaledMap[pPresentInfo->pWaitSemaphores[i]]) {
6266 dev_data->semaphoreSignaledMap[pPresentInfo->pWaitSemaphores[i]] = 0;
6267 } else {
6268 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
6269 "Queue %#" PRIx64 " is waiting on semaphore %#" PRIx64 " that has no way to be signaled.",
Mark Youngee3f3a22016-01-25 12:18:32 -07006270 (uint64_t)(queue), (uint64_t)(pPresentInfo->pWaitSemaphores[i]));
Michael Lentine15a47882016-01-06 10:05:48 -06006271 }
6272 }
Michael Lentineabc5e922015-10-12 11:30:14 -05006273 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
Ian Elliott05846062015-11-20 14:13:17 -07006274 auto swapchain_data = dev_data->device_extensions.swapchainMap.find(pPresentInfo->pSwapchains[i]);
6275 if (swapchain_data != dev_data->device_extensions.swapchainMap.end() && pPresentInfo->pImageIndices[i] < swapchain_data->second->images.size()) {
6276 VkImage image = swapchain_data->second->images[pPresentInfo->pImageIndices[i]];
Michael Lentineabc5e922015-10-12 11:30:14 -05006277 auto image_data = dev_data->imageLayoutMap.find(image);
6278 if (image_data != dev_data->imageLayoutMap.end()) {
Ian Elliott05846062015-11-20 14:13:17 -07006279 if (image_data->second->layout != VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07006280 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT, (uint64_t)queue, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Michael Lentineabc5e922015-10-12 11:30:14 -05006281 "Images passed to present must be in layout PRESENT_SOURCE_KHR but is in %d", image_data->second->layout);
6282 }
6283 }
6284 }
6285 }
6286 }
Mark Lobodzinski31e5f282015-11-30 16:48:53 -07006287#endif // DISABLE_IMAGE_LAYOUT_VALIDATION
Michael Lentineabc5e922015-10-12 11:30:14 -05006288
6289 if (VK_FALSE == skip_call)
6290 return dev_data->device_dispatch_table->QueuePresentKHR(queue, pPresentInfo);
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07006291 return VK_ERROR_VALIDATION_FAILED_EXT;
Michael Lentineabc5e922015-10-12 11:30:14 -05006292}
6293
Michael Lentine15a47882016-01-06 10:05:48 -06006294VKAPI_ATTR VkResult VKAPI_CALL vkAcquireNextImageKHR(
6295 VkDevice device,
6296 VkSwapchainKHR swapchain,
6297 uint64_t timeout,
6298 VkSemaphore semaphore,
6299 VkFence fence,
6300 uint32_t* pImageIndex)
6301{
6302 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6303 VkResult result = dev_data->device_dispatch_table->AcquireNextImageKHR(device, swapchain, timeout, semaphore, fence, pImageIndex);
6304 dev_data->semaphoreSignaledMap[semaphore] = 1;
Tobin Ehlis51b78af2016-01-06 10:27:47 -07006305 return result;
Michael Lentine15a47882016-01-06 10:05:48 -06006306}
6307
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07006308VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDebugReportCallbackEXT(
6309 VkInstance instance,
6310 const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,
6311 const VkAllocationCallbacks* pAllocator,
6312 VkDebugReportCallbackEXT* pMsgCallback)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006313{
Tobin Ehlis0b632332015-10-07 09:38:40 -06006314 layer_data* my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -07006315 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07006316 VkResult res = pTable->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback);
Tobin Ehlisc16c3e92015-06-16 09:04:30 -06006317 if (VK_SUCCESS == res) {
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -07006318 res = layer_create_msg_callback(my_data->report_data, pCreateInfo, pAllocator, pMsgCallback);
Tobin Ehlisc16c3e92015-06-16 09:04:30 -06006319 }
6320 return res;
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006321}
6322
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07006323VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDebugReportCallbackEXT(
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -07006324 VkInstance instance,
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07006325 VkDebugReportCallbackEXT msgCallback,
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -07006326 const VkAllocationCallbacks* pAllocator)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006327{
Tobin Ehlis0b632332015-10-07 09:38:40 -06006328 layer_data* my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -07006329 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07006330 pTable->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -07006331 layer_destroy_msg_callback(my_data->report_data, msgCallback, pAllocator);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006332}
6333
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07006334VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDebugReportMessageEXT(
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -07006335 VkInstance instance,
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07006336 VkDebugReportFlagsEXT flags,
6337 VkDebugReportObjectTypeEXT objType,
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -07006338 uint64_t object,
6339 size_t location,
6340 int32_t msgCode,
6341 const char* pLayerPrefix,
6342 const char* pMsg)
6343{
6344 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07006345 my_data->instance_dispatch_table->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -07006346}
6347
Chia-I Wu9ab61502015-11-06 06:42:02 +08006348VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDbgMarkerBegin(VkCommandBuffer commandBuffer, const char* pMarker)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006349{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06006350 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006351 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
6352 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis0b632332015-10-07 09:38:40 -06006353 if (!dev_data->device_extensions.debug_marker_enabled) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07006354 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_EXTENSION, "DS",
Tobin Ehlisa16e8922015-06-16 15:50:44 -06006355 "Attempt to use CmdDbgMarkerBegin but extension disabled!");
Jon Ashburneab34492015-06-01 09:37:38 -06006356 return;
Tobin Ehlisce132d82015-06-19 15:07:05 -06006357 } else if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07006358 skipCall |= addCmd(dev_data, pCB, CMD_DBGMARKERBEGIN, "vkCmdDbgMarkerBegin()");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006359 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06006360 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006361 debug_marker_dispatch_table(commandBuffer)->CmdDbgMarkerBegin(commandBuffer, pMarker);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006362}
6363
Chia-I Wu9ab61502015-11-06 06:42:02 +08006364VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDbgMarkerEnd(VkCommandBuffer commandBuffer)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006365{
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06006366 VkBool32 skipCall = VK_FALSE;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006367 layer_data* dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
6368 GLOBAL_CB_NODE* pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis0b632332015-10-07 09:38:40 -06006369 if (!dev_data->device_extensions.debug_marker_enabled) {
Mark Lobodzinskib01451b2016-01-04 13:18:10 -07006370 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_EXTENSION, "DS",
Tobin Ehlisa16e8922015-06-16 15:50:44 -06006371 "Attempt to use CmdDbgMarkerEnd but extension disabled!");
Jon Ashburneab34492015-06-01 09:37:38 -06006372 return;
Tobin Ehlisce132d82015-06-19 15:07:05 -06006373 } else if (pCB) {
Tobin Ehlis61b36f32015-12-16 08:19:42 -07006374 skipCall |= addCmd(dev_data, pCB, CMD_DBGMARKEREND, "vkCmdDbgMarkerEnd()");
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006375 }
Tobin Ehlis1cfb30a2015-09-09 11:31:10 -06006376 if (VK_FALSE == skipCall)
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006377 debug_marker_dispatch_table(commandBuffer)->CmdDbgMarkerEnd(commandBuffer);
Jon Ashburneab34492015-06-01 09:37:38 -06006378}
6379
Chia-I Wu9ab61502015-11-06 06:42:02 +08006380VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006381{
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07006382 if (!strcmp(funcName, "vkGetDeviceProcAddr"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006383 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006384 if (!strcmp(funcName, "vkDestroyDevice"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006385 return (PFN_vkVoidFunction) vkDestroyDevice;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006386 if (!strcmp(funcName, "vkQueueSubmit"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006387 return (PFN_vkVoidFunction) vkQueueSubmit;
Michael Lentine700b0aa2015-10-30 17:57:32 -07006388 if (!strcmp(funcName, "vkWaitForFences"))
6389 return (PFN_vkVoidFunction) vkWaitForFences;
6390 if (!strcmp(funcName, "vkGetFenceStatus"))
6391 return (PFN_vkVoidFunction) vkGetFenceStatus;
Mark Lobodzinski8da0d1e2016-01-06 14:58:59 -07006392 if (!strcmp(funcName, "vkQueueWaitIdle"))
6393 return (PFN_vkVoidFunction) vkQueueWaitIdle;
6394 if (!strcmp(funcName, "vkDeviceWaitIdle"))
6395 return (PFN_vkVoidFunction) vkDeviceWaitIdle;
Michael Lentine700b0aa2015-10-30 17:57:32 -07006396 if (!strcmp(funcName, "vkGetDeviceQueue"))
6397 return (PFN_vkVoidFunction) vkGetDeviceQueue;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006398 if (!strcmp(funcName, "vkDestroyInstance"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006399 return (PFN_vkVoidFunction) vkDestroyInstance;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006400 if (!strcmp(funcName, "vkDestroyDevice"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006401 return (PFN_vkVoidFunction) vkDestroyDevice;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006402 if (!strcmp(funcName, "vkDestroyFence"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006403 return (PFN_vkVoidFunction) vkDestroyFence;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006404 if (!strcmp(funcName, "vkDestroySemaphore"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006405 return (PFN_vkVoidFunction) vkDestroySemaphore;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006406 if (!strcmp(funcName, "vkDestroyEvent"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006407 return (PFN_vkVoidFunction) vkDestroyEvent;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006408 if (!strcmp(funcName, "vkDestroyQueryPool"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006409 return (PFN_vkVoidFunction) vkDestroyQueryPool;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006410 if (!strcmp(funcName, "vkDestroyBuffer"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006411 return (PFN_vkVoidFunction) vkDestroyBuffer;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006412 if (!strcmp(funcName, "vkDestroyBufferView"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006413 return (PFN_vkVoidFunction) vkDestroyBufferView;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006414 if (!strcmp(funcName, "vkDestroyImage"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006415 return (PFN_vkVoidFunction) vkDestroyImage;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006416 if (!strcmp(funcName, "vkDestroyImageView"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006417 return (PFN_vkVoidFunction) vkDestroyImageView;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006418 if (!strcmp(funcName, "vkDestroyShaderModule"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006419 return (PFN_vkVoidFunction) vkDestroyShaderModule;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006420 if (!strcmp(funcName, "vkDestroyPipeline"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006421 return (PFN_vkVoidFunction) vkDestroyPipeline;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006422 if (!strcmp(funcName, "vkDestroyPipelineLayout"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006423 return (PFN_vkVoidFunction) vkDestroyPipelineLayout;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006424 if (!strcmp(funcName, "vkDestroySampler"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006425 return (PFN_vkVoidFunction) vkDestroySampler;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006426 if (!strcmp(funcName, "vkDestroyDescriptorSetLayout"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006427 return (PFN_vkVoidFunction) vkDestroyDescriptorSetLayout;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006428 if (!strcmp(funcName, "vkDestroyDescriptorPool"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006429 return (PFN_vkVoidFunction) vkDestroyDescriptorPool;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006430 if (!strcmp(funcName, "vkDestroyFramebuffer"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006431 return (PFN_vkVoidFunction) vkDestroyFramebuffer;
Tobin Ehlis60a9b4f2015-07-07 10:42:20 -06006432 if (!strcmp(funcName, "vkDestroyRenderPass"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006433 return (PFN_vkVoidFunction) vkDestroyRenderPass;
Tobin Ehlisa1c28562015-10-23 16:00:08 -06006434 if (!strcmp(funcName, "vkCreateBuffer"))
6435 return (PFN_vkVoidFunction) vkCreateBuffer;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006436 if (!strcmp(funcName, "vkCreateBufferView"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006437 return (PFN_vkVoidFunction) vkCreateBufferView;
Tobin Ehlisa1c28562015-10-23 16:00:08 -06006438 if (!strcmp(funcName, "vkCreateImage"))
6439 return (PFN_vkVoidFunction) vkCreateImage;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006440 if (!strcmp(funcName, "vkCreateImageView"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006441 return (PFN_vkVoidFunction) vkCreateImageView;
Jon Ashburnc669cc62015-07-09 15:02:25 -06006442 if (!strcmp(funcName, "CreatePipelineCache"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006443 return (PFN_vkVoidFunction) vkCreatePipelineCache;
Jon Ashburnc669cc62015-07-09 15:02:25 -06006444 if (!strcmp(funcName, "DestroyPipelineCache"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006445 return (PFN_vkVoidFunction) vkDestroyPipelineCache;
Jon Ashburnc669cc62015-07-09 15:02:25 -06006446 if (!strcmp(funcName, "GetPipelineCacheData"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006447 return (PFN_vkVoidFunction) vkGetPipelineCacheData;
Jon Ashburnc669cc62015-07-09 15:02:25 -06006448 if (!strcmp(funcName, "MergePipelineCaches"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006449 return (PFN_vkVoidFunction) vkMergePipelineCaches;
Jon Ashburnc669cc62015-07-09 15:02:25 -06006450 if (!strcmp(funcName, "vkCreateGraphicsPipelines"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006451 return (PFN_vkVoidFunction) vkCreateGraphicsPipelines;
Mark Lobodzinski475a2182015-11-10 15:25:01 -07006452 if (!strcmp(funcName, "vkCreateComputePipelines"))
6453 return (PFN_vkVoidFunction) vkCreateComputePipelines;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006454 if (!strcmp(funcName, "vkCreateSampler"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006455 return (PFN_vkVoidFunction) vkCreateSampler;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006456 if (!strcmp(funcName, "vkCreateDescriptorSetLayout"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006457 return (PFN_vkVoidFunction) vkCreateDescriptorSetLayout;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05006458 if (!strcmp(funcName, "vkCreatePipelineLayout"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006459 return (PFN_vkVoidFunction) vkCreatePipelineLayout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006460 if (!strcmp(funcName, "vkCreateDescriptorPool"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006461 return (PFN_vkVoidFunction) vkCreateDescriptorPool;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006462 if (!strcmp(funcName, "vkResetDescriptorPool"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006463 return (PFN_vkVoidFunction) vkResetDescriptorPool;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006464 if (!strcmp(funcName, "vkAllocateDescriptorSets"))
6465 return (PFN_vkVoidFunction) vkAllocateDescriptorSets;
Tobin Ehlise735c692015-10-08 13:13:50 -06006466 if (!strcmp(funcName, "vkFreeDescriptorSets"))
6467 return (PFN_vkVoidFunction) vkFreeDescriptorSets;
Chia-I Wu9d00ed72015-05-25 16:27:55 +08006468 if (!strcmp(funcName, "vkUpdateDescriptorSets"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006469 return (PFN_vkVoidFunction) vkUpdateDescriptorSets;
Mark Lobodzinski39298632015-11-18 08:38:27 -07006470 if (!strcmp(funcName, "vkCreateCommandPool"))
6471 return (PFN_vkVoidFunction) vkCreateCommandPool;
6472 if (!strcmp(funcName, "vkDestroyCommandPool"))
6473 return (PFN_vkVoidFunction) vkDestroyCommandPool;
Tobin Ehlisac0ef842015-12-14 13:46:38 -07006474 if (!strcmp(funcName, "vkResetCommandPool"))
6475 return (PFN_vkVoidFunction) vkResetCommandPool;
Chia-I Wu3432a0c2015-10-27 18:04:07 +08006476 if (!strcmp(funcName, "vkAllocateCommandBuffers"))
6477 return (PFN_vkVoidFunction) vkAllocateCommandBuffers;
Mark Lobodzinski39298632015-11-18 08:38:27 -07006478 if (!strcmp(funcName, "vkFreeCommandBuffers"))
6479 return (PFN_vkVoidFunction) vkFreeCommandBuffers;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006480 if (!strcmp(funcName, "vkBeginCommandBuffer"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006481 return (PFN_vkVoidFunction) vkBeginCommandBuffer;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006482 if (!strcmp(funcName, "vkEndCommandBuffer"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006483 return (PFN_vkVoidFunction) vkEndCommandBuffer;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006484 if (!strcmp(funcName, "vkResetCommandBuffer"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006485 return (PFN_vkVoidFunction) vkResetCommandBuffer;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006486 if (!strcmp(funcName, "vkCmdBindPipeline"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006487 return (PFN_vkVoidFunction) vkCmdBindPipeline;
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06006488 if (!strcmp(funcName, "vkCmdSetViewport"))
6489 return (PFN_vkVoidFunction) vkCmdSetViewport;
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -06006490 if (!strcmp(funcName, "vkCmdSetScissor"))
6491 return (PFN_vkVoidFunction) vkCmdSetScissor;
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06006492 if (!strcmp(funcName, "vkCmdSetLineWidth"))
6493 return (PFN_vkVoidFunction) vkCmdSetLineWidth;
6494 if (!strcmp(funcName, "vkCmdSetDepthBias"))
6495 return (PFN_vkVoidFunction) vkCmdSetDepthBias;
6496 if (!strcmp(funcName, "vkCmdSetBlendConstants"))
6497 return (PFN_vkVoidFunction) vkCmdSetBlendConstants;
6498 if (!strcmp(funcName, "vkCmdSetDepthBounds"))
6499 return (PFN_vkVoidFunction) vkCmdSetDepthBounds;
6500 if (!strcmp(funcName, "vkCmdSetStencilCompareMask"))
6501 return (PFN_vkVoidFunction) vkCmdSetStencilCompareMask;
6502 if (!strcmp(funcName, "vkCmdSetStencilWriteMask"))
6503 return (PFN_vkVoidFunction) vkCmdSetStencilWriteMask;
6504 if (!strcmp(funcName, "vkCmdSetStencilReference"))
6505 return (PFN_vkVoidFunction) vkCmdSetStencilReference;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006506 if (!strcmp(funcName, "vkCmdBindDescriptorSets"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006507 return (PFN_vkVoidFunction) vkCmdBindDescriptorSets;
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06006508 if (!strcmp(funcName, "vkCmdBindVertexBuffers"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006509 return (PFN_vkVoidFunction) vkCmdBindVertexBuffers;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006510 if (!strcmp(funcName, "vkCmdBindIndexBuffer"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006511 return (PFN_vkVoidFunction) vkCmdBindIndexBuffer;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006512 if (!strcmp(funcName, "vkCmdDraw"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006513 return (PFN_vkVoidFunction) vkCmdDraw;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006514 if (!strcmp(funcName, "vkCmdDrawIndexed"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006515 return (PFN_vkVoidFunction) vkCmdDrawIndexed;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006516 if (!strcmp(funcName, "vkCmdDrawIndirect"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006517 return (PFN_vkVoidFunction) vkCmdDrawIndirect;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006518 if (!strcmp(funcName, "vkCmdDrawIndexedIndirect"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006519 return (PFN_vkVoidFunction) vkCmdDrawIndexedIndirect;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006520 if (!strcmp(funcName, "vkCmdDispatch"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006521 return (PFN_vkVoidFunction) vkCmdDispatch;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006522 if (!strcmp(funcName, "vkCmdDispatchIndirect"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006523 return (PFN_vkVoidFunction) vkCmdDispatchIndirect;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006524 if (!strcmp(funcName, "vkCmdCopyBuffer"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006525 return (PFN_vkVoidFunction) vkCmdCopyBuffer;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006526 if (!strcmp(funcName, "vkCmdCopyImage"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006527 return (PFN_vkVoidFunction) vkCmdCopyImage;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006528 if (!strcmp(funcName, "vkCmdCopyBufferToImage"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006529 return (PFN_vkVoidFunction) vkCmdCopyBufferToImage;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006530 if (!strcmp(funcName, "vkCmdCopyImageToBuffer"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006531 return (PFN_vkVoidFunction) vkCmdCopyImageToBuffer;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006532 if (!strcmp(funcName, "vkCmdUpdateBuffer"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006533 return (PFN_vkVoidFunction) vkCmdUpdateBuffer;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006534 if (!strcmp(funcName, "vkCmdFillBuffer"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006535 return (PFN_vkVoidFunction) vkCmdFillBuffer;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006536 if (!strcmp(funcName, "vkCmdClearColorImage"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006537 return (PFN_vkVoidFunction) vkCmdClearColorImage;
Chris Forbesd9be82b2015-06-22 17:21:59 +12006538 if (!strcmp(funcName, "vkCmdClearDepthStencilImage"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006539 return (PFN_vkVoidFunction) vkCmdClearDepthStencilImage;
Courtney Goeltzenleuchterc9323e02015-10-15 16:51:05 -06006540 if (!strcmp(funcName, "vkCmdClearAttachments"))
6541 return (PFN_vkVoidFunction) vkCmdClearAttachments;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006542 if (!strcmp(funcName, "vkCmdResolveImage"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006543 return (PFN_vkVoidFunction) vkCmdResolveImage;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006544 if (!strcmp(funcName, "vkCmdSetEvent"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006545 return (PFN_vkVoidFunction) vkCmdSetEvent;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006546 if (!strcmp(funcName, "vkCmdResetEvent"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006547 return (PFN_vkVoidFunction) vkCmdResetEvent;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006548 if (!strcmp(funcName, "vkCmdWaitEvents"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006549 return (PFN_vkVoidFunction) vkCmdWaitEvents;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006550 if (!strcmp(funcName, "vkCmdPipelineBarrier"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006551 return (PFN_vkVoidFunction) vkCmdPipelineBarrier;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006552 if (!strcmp(funcName, "vkCmdBeginQuery"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006553 return (PFN_vkVoidFunction) vkCmdBeginQuery;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006554 if (!strcmp(funcName, "vkCmdEndQuery"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006555 return (PFN_vkVoidFunction) vkCmdEndQuery;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006556 if (!strcmp(funcName, "vkCmdResetQueryPool"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006557 return (PFN_vkVoidFunction) vkCmdResetQueryPool;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006558 if (!strcmp(funcName, "vkCmdWriteTimestamp"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006559 return (PFN_vkVoidFunction) vkCmdWriteTimestamp;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006560 if (!strcmp(funcName, "vkCreateFramebuffer"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006561 return (PFN_vkVoidFunction) vkCreateFramebuffer;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07006562 if (!strcmp(funcName, "vkCreateShaderModule"))
6563 return (PFN_vkVoidFunction) vkCreateShaderModule;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006564 if (!strcmp(funcName, "vkCreateRenderPass"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006565 return (PFN_vkVoidFunction) vkCreateRenderPass;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006566 if (!strcmp(funcName, "vkCmdBeginRenderPass"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006567 return (PFN_vkVoidFunction) vkCmdBeginRenderPass;
Chia-I Wu08accc62015-07-07 11:50:03 +08006568 if (!strcmp(funcName, "vkCmdNextSubpass"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006569 return (PFN_vkVoidFunction) vkCmdNextSubpass;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06006570 if (!strcmp(funcName, "vkCmdEndRenderPass"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006571 return (PFN_vkVoidFunction) vkCmdEndRenderPass;
Tobin Ehlis4b34ddc2015-09-17 14:18:16 -06006572 if (!strcmp(funcName, "vkCmdExecuteCommands"))
6573 return (PFN_vkVoidFunction) vkCmdExecuteCommands;
Michael Lentineb887b0a2015-12-29 14:12:11 -06006574 if (!strcmp(funcName, "vkSetEvent"))
6575 return (PFN_vkVoidFunction) vkSetEvent;
Michael Lentine7b236262015-10-23 12:41:44 -07006576 if (!strcmp(funcName, "vkMapMemory"))
6577 return (PFN_vkVoidFunction) vkMapMemory;
Michael Lentineb887b0a2015-12-29 14:12:11 -06006578 if (!strcmp(funcName, "vkGetQueryPoolResults"))
6579 return (PFN_vkVoidFunction) vkGetQueryPoolResults;
Michael Lentine15a47882016-01-06 10:05:48 -06006580 if (!strcmp(funcName, "vkBindImageMemory"))
6581 return (PFN_vkVoidFunction) vkBindImageMemory;
6582 if (!strcmp(funcName, "vkQueueBindSparse"))
6583 return (PFN_vkVoidFunction) vkQueueBindSparse;
6584 if (!strcmp(funcName, "vkCreateSemaphore"))
6585 return (PFN_vkVoidFunction) vkCreateSemaphore;
Jon Ashburneab34492015-06-01 09:37:38 -06006586
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07006587 if (dev == NULL)
6588 return NULL;
6589
6590 layer_data *dev_data;
6591 dev_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
6592
Michael Lentineabc5e922015-10-12 11:30:14 -05006593 if (dev_data->device_extensions.wsi_enabled)
6594 {
6595 if (!strcmp(funcName, "vkCreateSwapchainKHR"))
6596 return (PFN_vkVoidFunction) vkCreateSwapchainKHR;
6597 if (!strcmp(funcName, "vkDestroySwapchainKHR"))
6598 return (PFN_vkVoidFunction) vkDestroySwapchainKHR;
6599 if (!strcmp(funcName, "vkGetSwapchainImagesKHR"))
6600 return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR;
Michael Lentine15a47882016-01-06 10:05:48 -06006601 if (!strcmp(funcName, "vkAcquireNextImageKHR"))
6602 return (PFN_vkVoidFunction) vkAcquireNextImageKHR;
Michael Lentineabc5e922015-10-12 11:30:14 -05006603 if (!strcmp(funcName, "vkQueuePresentKHR"))
6604 return (PFN_vkVoidFunction) vkQueuePresentKHR;
6605 }
6606
Tobin Ehlis0b632332015-10-07 09:38:40 -06006607 VkLayerDispatchTable* pTable = dev_data->device_dispatch_table;
6608 if (dev_data->device_extensions.debug_marker_enabled)
Jon Ashburn8fd08252015-05-28 16:25:02 -06006609 {
Jon Ashburn747f2b62015-06-18 15:02:58 -06006610 if (!strcmp(funcName, "vkCmdDbgMarkerBegin"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006611 return (PFN_vkVoidFunction) vkCmdDbgMarkerBegin;
Jon Ashburn747f2b62015-06-18 15:02:58 -06006612 if (!strcmp(funcName, "vkCmdDbgMarkerEnd"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006613 return (PFN_vkVoidFunction) vkCmdDbgMarkerEnd;
Jon Ashburneab34492015-06-01 09:37:38 -06006614 }
6615 {
Jon Ashburn8fd08252015-05-28 16:25:02 -06006616 if (pTable->GetDeviceProcAddr == NULL)
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006617 return NULL;
Jon Ashburn8fd08252015-05-28 16:25:02 -06006618 return pTable->GetDeviceProcAddr(dev, funcName);
Jon Ashburnf6b33db2015-05-05 14:22:52 -06006619 }
6620}
6621
Chia-I Wu9ab61502015-11-06 06:42:02 +08006622VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06006623{
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07006624 if (!strcmp(funcName, "vkGetInstanceProcAddr"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006625 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07006626 if (!strcmp(funcName, "vkGetDeviceProcAddr"))
6627 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Courtney Goeltzenleuchterabc035e2015-06-01 14:29:58 -06006628 if (!strcmp(funcName, "vkCreateInstance"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006629 return (PFN_vkVoidFunction) vkCreateInstance;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07006630 if (!strcmp(funcName, "vkCreateDevice"))
6631 return (PFN_vkVoidFunction) vkCreateDevice;
Jon Ashburn3950e1b2015-05-20 09:00:28 -06006632 if (!strcmp(funcName, "vkDestroyInstance"))
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06006633 return (PFN_vkVoidFunction) vkDestroyInstance;
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06006634 if (!strcmp(funcName, "vkEnumerateInstanceLayerProperties"))
6635 return (PFN_vkVoidFunction) vkEnumerateInstanceLayerProperties;
6636 if (!strcmp(funcName, "vkEnumerateInstanceExtensionProperties"))
6637 return (PFN_vkVoidFunction) vkEnumerateInstanceExtensionProperties;
6638 if (!strcmp(funcName, "vkEnumerateDeviceLayerProperties"))
6639 return (PFN_vkVoidFunction) vkEnumerateDeviceLayerProperties;
6640 if (!strcmp(funcName, "vkEnumerateDeviceExtensionProperties"))
6641 return (PFN_vkVoidFunction) vkEnumerateDeviceExtensionProperties;
Courtney Goeltzenleuchteree562872015-06-01 14:33:14 -06006642
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07006643 if (instance == NULL)
6644 return NULL;
6645
6646 PFN_vkVoidFunction fptr;
6647
6648 layer_data* my_data;
Tobin Ehlis7e2ad752015-12-01 09:48:58 -07006649 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlisa16e8922015-06-16 15:50:44 -06006650 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchteree562872015-06-01 14:33:14 -06006651 if (fptr)
6652 return fptr;
6653
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -07006654 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
6655 if (pTable->GetInstanceProcAddr == NULL)
6656 return NULL;
6657 return pTable->GetInstanceProcAddr(instance, funcName);
Tobin Ehlis10ae8c12015-03-17 16:24:32 -06006658}