blob: b4e95ace24f1f3a4260a125908d9daed32d106ab [file] [log] [blame]
Dave Houlton59a20702017-02-02 17:26:23 -07001/* Copyright (c) 2015-2017 The Khronos Group Inc.
2 * Copyright (c) 2015-2017 Valve Corporation
3 * Copyright (c) 2015-2017 LunarG, Inc.
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -06004 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -06008 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060010 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060016 *
Jon Ashburne922f712015-11-03 13:41:23 -070017 * Author: Mark Lobodzinski <mark@lunarg.com>
18 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Dave Houlton59a20702017-02-02 17:26:23 -070019 * Author: Dave Houlton <daveh@lunarg.com>
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070020 */
21
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060022#pragma once
23#include <stdbool.h>
John Zulauf965d88d2018-04-12 15:47:26 -060024#include <string>
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060025#include <vector>
Dave Houlton3c9fca72017-03-27 17:25:54 -060026#include "vk_format_utils.h"
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060027#include "vk_layer_logging.h"
28
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -060029#ifndef WIN32
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070030#include <strings.h> // For ffs()
Courtney Goeltzenleuchter3698c622015-10-27 11:23:21 -060031#else
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070032#include <intrin.h> // For __lzcnt()
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -060033#endif
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060034
35#ifdef __cplusplus
John Zulauf965d88d2018-04-12 15:47:26 -060036// Traits objects to allow string_join to operate on collections of const char *
37template <typename String>
38struct StringJoinSizeTrait {
39 static size_t size(const String &str) { return str.size(); }
40};
41
42template <>
43struct StringJoinSizeTrait<const char *> {
44 static size_t size(const char *str) {
45 if (!str) return 0;
46 return strlen(str);
47 }
48};
49// Similar to perl/python join
50// * String must support size, reserve, append, and be default constructable
51// * StringCollection must support size, const forward iteration, and store
52// strings compatible with String::append
53// * Accessor trait can be set if default accessors (compatible with string
54// and const char *) don't support size(StringCollection::value_type &)
55//
56// Return type based on sep type
57template <typename String = std::string, typename StringCollection = std::vector<String>,
58 typename Accessor = StringJoinSizeTrait<typename StringCollection::value_type>>
59static inline String string_join(const String &sep, const StringCollection &strings) {
60 String joined;
61 const size_t count = strings.size();
62 if (!count) return joined;
63
64 // Prereserved storage, s.t. we will execute in linear time (avoids reallocation copies)
65 size_t reserve = (count - 1) * sep.size();
66 for (const auto &str : strings) {
67 reserve += Accessor::size(str); // abstracted to allow const char * type in StringCollection
68 }
69 joined.reserve(reserve + 1);
70
71 // Seps only occur *between* strings entries, so first is special
72 auto current = strings.cbegin();
73 joined.append(*current);
74 ++current;
75 for (; current != strings.cend(); ++current) {
76 joined.append(sep);
77 joined.append(*current);
78 }
79 return joined;
80}
81
82// Requires StringCollection::value_type has a const char * constructor and is compatible the string_join::String above
83template <typename StringCollection = std::vector<std::string>, typename SepString = std::string>
84static inline SepString string_join(const char *sep, const StringCollection &strings) {
85 return string_join<SepString, StringCollection>(SepString(sep), strings);
86}
87
John Zulaufdf851b12018-06-12 14:49:04 -060088// Perl/Python style join operation for general types using stream semantics
89// Note: won't be as fast as string_join above, but simpler to use (and code)
90// Note: Modifiable reference doesn't match the google style but does match std style for stream handling and algorithms
91template <typename Stream, typename String, typename ForwardIt>
92Stream &stream_join(Stream &stream, const String &sep, ForwardIt first, ForwardIt last) {
93 if (first != last) {
94 stream << *first;
95 ++first;
96 while (first != last) {
97 stream << sep << *first;
98 ++first;
99 }
100 }
101 return stream;
102}
103
104// stream_join For whole collections with forward iterators
105template <typename Stream, typename String, typename Collection>
106Stream &stream_join(Stream &stream, const String &sep, const Collection &values) {
107 return stream_join(stream, sep, values.cbegin(), values.cend());
108}
109
Mark Lobodzinskic1b5b882018-06-25 14:54:04 -0600110typedef void *dispatch_key;
111static inline dispatch_key get_dispatch_key(const void *object) { return (dispatch_key) * (VkLayerDispatchTable **)object; }
112
113VK_LAYER_EXPORT VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func);
114VK_LAYER_EXPORT VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo, VkLayerFunction func);
115
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -0600116extern "C" {
117#endif
118
Jon Ashburnd883d812016-03-24 08:32:09 -0600119#define VK_LAYER_API_VERSION VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION)
Mark Lobodzinskiadaac9d2016-01-08 11:07:56 -0700120
Mark Lobodzinskia9f33492016-01-11 14:17:05 -0700121typedef enum VkStringErrorFlagBits {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700122 VK_STRING_ERROR_NONE = 0x00000000,
123 VK_STRING_ERROR_LENGTH = 0x00000001,
124 VK_STRING_ERROR_BAD_DATA = 0x00000002,
Mark Lobodzinskia9f33492016-01-11 14:17:05 -0700125} VkStringErrorFlagBits;
126typedef VkFlags VkStringErrorFlags;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700127
Mark Young6ba8abe2017-11-09 10:37:04 -0700128VK_LAYER_EXPORT void layer_debug_report_actions(debug_report_data *report_data,
129 std::vector<VkDebugReportCallbackEXT> &logging_callback,
130 const VkAllocationCallbacks *pAllocator, const char *layer_identifier);
131
132VK_LAYER_EXPORT void layer_debug_messenger_actions(debug_report_data *report_data,
133 std::vector<VkDebugUtilsMessengerEXT> &logging_messenger,
134 const VkAllocationCallbacks *pAllocator, const char *layer_identifier);
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600135
Mike Stroyana551bc02016-09-28 09:42:28 -0600136VK_LAYER_EXPORT VkStringErrorFlags vk_string_validate(const int max_length, const char *char_array);
137VK_LAYER_EXPORT bool white_list(const char *item, const char *whitelist);
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -0600138
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700139static inline int u_ffs(int val) {
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600140#ifdef WIN32
Mark Lobodzinski5ddf6c32015-12-16 17:47:28 -0700141 unsigned long bit_pos = 0;
Mike Stroyandebb9842016-01-07 10:05:21 -0700142 if (_BitScanForward(&bit_pos, val) != 0) {
Mark Lobodzinski5ddf6c32015-12-16 17:47:28 -0700143 bit_pos += 1;
144 }
145 return bit_pos;
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600146#else
Mark Lobodzinski5ddf6c32015-12-16 17:47:28 -0700147 return ffs(val);
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600148#endif
149}
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -0600150
151#ifdef __cplusplus
152}
153#endif