Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015-2017 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2017 Valve Corporation |
| 3 | * Copyright (c) 2015-2017 LunarG, Inc. |
| 4 | * Copyright (C) 2015-2017 Google Inc. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * Author: Chris Forbes <chrisf@ijw.co.nz> |
| 19 | */ |
| 20 | #ifndef VULKAN_SHADER_VALIDATION_H |
| 21 | #define VULKAN_SHADER_VALIDATION_H |
| 22 | |
Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 23 | #include <spirv_tools_commit_id.h> |
| 24 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 25 | // A forward iterator over spirv instructions. Provides easy access to len, opcode, and content words |
| 26 | // without the caller needing to care too much about the physical SPIRV module layout. |
| 27 | struct spirv_inst_iter { |
| 28 | std::vector<uint32_t>::const_iterator zero; |
| 29 | std::vector<uint32_t>::const_iterator it; |
| 30 | |
| 31 | uint32_t len() { |
| 32 | auto result = *it >> 16; |
| 33 | assert(result > 0); |
| 34 | return result; |
| 35 | } |
| 36 | |
| 37 | uint32_t opcode() { return *it & 0x0ffffu; } |
| 38 | |
| 39 | uint32_t const &word(unsigned n) { |
| 40 | assert(n < len()); |
| 41 | return it[n]; |
| 42 | } |
| 43 | |
| 44 | uint32_t offset() { return (uint32_t)(it - zero); } |
| 45 | |
| 46 | spirv_inst_iter() {} |
| 47 | |
| 48 | spirv_inst_iter(std::vector<uint32_t>::const_iterator zero, std::vector<uint32_t>::const_iterator it) : zero(zero), it(it) {} |
| 49 | |
| 50 | bool operator==(spirv_inst_iter const &other) { return it == other.it; } |
| 51 | |
| 52 | bool operator!=(spirv_inst_iter const &other) { return it != other.it; } |
| 53 | |
| 54 | spirv_inst_iter operator++(int) { // x++ |
| 55 | spirv_inst_iter ii = *this; |
| 56 | it += len(); |
| 57 | return ii; |
| 58 | } |
| 59 | |
| 60 | spirv_inst_iter operator++() { // ++x; |
| 61 | it += len(); |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | // The iterator and the value are the same thing. |
| 66 | spirv_inst_iter &operator*() { return *this; } |
| 67 | spirv_inst_iter const &operator*() const { return *this; } |
| 68 | }; |
| 69 | |
| 70 | struct shader_module { |
| 71 | // The spirv image itself |
| 72 | std::vector<uint32_t> words; |
| 73 | // A mapping of <id> to the first word of its def. this is useful because walking type |
| 74 | // trees, constant expressions, etc requires jumping all over the instruction stream. |
| 75 | std::unordered_map<unsigned, unsigned> def_index; |
| 76 | bool has_valid_spirv; |
| 77 | |
| 78 | shader_module(VkShaderModuleCreateInfo const *pCreateInfo) |
| 79 | : words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)), |
| 80 | def_index(), |
| 81 | has_valid_spirv(true) { |
| 82 | build_def_index(); |
| 83 | } |
| 84 | |
| 85 | shader_module() : has_valid_spirv(false) {} |
| 86 | |
| 87 | // Expose begin() / end() to enable range-based for |
| 88 | spirv_inst_iter begin() const { return spirv_inst_iter(words.begin(), words.begin() + 5); } // First insn |
| 89 | spirv_inst_iter end() const { return spirv_inst_iter(words.begin(), words.end()); } // Just past last insn |
| 90 | // Given an offset into the module, produce an iterator there. |
| 91 | spirv_inst_iter at(unsigned offset) const { return spirv_inst_iter(words.begin(), words.begin() + offset); } |
| 92 | |
| 93 | // Gets an iterator to the definition of an id |
| 94 | spirv_inst_iter get_def(unsigned id) const { |
| 95 | auto it = def_index.find(id); |
| 96 | if (it == def_index.end()) { |
| 97 | return end(); |
| 98 | } |
| 99 | return at(it->second); |
| 100 | } |
| 101 | |
| 102 | void build_def_index(); |
| 103 | }; |
| 104 | |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 105 | class ValidationCache { |
| 106 | // hashes of shaders that have passed validation before, and can be skipped. |
| 107 | // we don't store negative results, as we would have to also store what was |
| 108 | // wrong with them; also, we expect they will get fixed, so we're less |
| 109 | // likely to see them again. |
| 110 | std::unordered_set<uint32_t> good_shader_hashes; |
| 111 | ValidationCache() {} |
| 112 | |
| 113 | public: |
| 114 | static VkValidationCacheEXT Create(VkValidationCacheCreateInfoEXT const *pCreateInfo) { |
| 115 | auto cache = new ValidationCache(); |
| 116 | cache->Load(pCreateInfo); |
| 117 | return VkValidationCacheEXT(cache); |
| 118 | } |
| 119 | |
| 120 | void Load(VkValidationCacheCreateInfoEXT const *pCreateInfo) { |
| 121 | auto size = 8u + VK_UUID_SIZE; |
| 122 | if (!pCreateInfo->pInitialData || pCreateInfo->initialDataSize < size) |
| 123 | return; |
| 124 | |
| 125 | uint32_t const *data = (uint32_t const *)pCreateInfo->pInitialData; |
| 126 | if (data[0] != size) |
| 127 | return; |
| 128 | if (data[1] != VK_VALIDATION_CACHE_HEADER_VERSION_ONE_EXT) |
| 129 | return; |
Cort Stratton | b614d33 | 2017-11-22 16:05:49 -0800 | [diff] [blame^] | 130 | uint8_t expected_uuid[16]; |
| 131 | CommitIdToUuid(SPIRV_TOOLS_COMMIT_ID, expected_uuid); |
| 132 | if (memcmp(&data[2], expected_uuid, 16) != 0) |
Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 133 | return; // different version |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 134 | |
| 135 | data += 6; |
| 136 | |
| 137 | for (;size < pCreateInfo->initialDataSize; |
| 138 | data++, size += sizeof(uint32_t)) { |
| 139 | good_shader_hashes.insert(*data); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void Write(size_t *pDataSize, void *pData) { |
| 144 | auto headerSize = 8u + VK_UUID_SIZE; |
| 145 | if (!pData) { |
| 146 | *pDataSize = headerSize + good_shader_hashes.size() * sizeof(uint32_t); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | if (*pDataSize < headerSize) { |
| 151 | *pDataSize = 0; |
| 152 | return; // Too small for even the header! |
| 153 | } |
| 154 | |
| 155 | uint32_t *out = (uint32_t *)pData; |
| 156 | size_t actualSize = headerSize; |
| 157 | |
| 158 | // Write the header |
| 159 | *out++ = headerSize; |
| 160 | *out++ = VK_VALIDATION_CACHE_HEADER_VERSION_ONE_EXT; |
Cort Stratton | b614d33 | 2017-11-22 16:05:49 -0800 | [diff] [blame^] | 161 | // Convert SPIRV-Tools commit ID (as a string of hexadecimal digits) into bytes, |
| 162 | // and write as many bytes as will fit into the header as a UUID. |
| 163 | CommitIdToUuid(SPIRV_TOOLS_COMMIT_ID, reinterpret_cast<uint8_t*>(out)); |
Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 164 | out += 4; |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 165 | |
| 166 | for (auto it = good_shader_hashes.begin(); |
| 167 | it != good_shader_hashes.end() && actualSize < *pDataSize; |
| 168 | it++, out++, actualSize += sizeof(uint32_t)) { |
| 169 | *out = *it; |
| 170 | } |
| 171 | |
| 172 | *pDataSize = actualSize; |
| 173 | } |
| 174 | |
| 175 | void Merge(ValidationCache const *other) { |
| 176 | for (auto h : other->good_shader_hashes) |
| 177 | good_shader_hashes.insert(h); |
| 178 | } |
| 179 | |
| 180 | static uint32_t MakeShaderHash(VkShaderModuleCreateInfo const *smci); |
| 181 | |
| 182 | bool Contains(uint32_t hash) { |
| 183 | return good_shader_hashes.count(hash) != 0; |
| 184 | } |
| 185 | |
| 186 | void Insert(uint32_t hash) { |
| 187 | good_shader_hashes.insert(hash); |
| 188 | } |
Cort Stratton | b614d33 | 2017-11-22 16:05:49 -0800 | [diff] [blame^] | 189 | private: |
| 190 | void CommitIdToUuid(const char* commitId, uint8_t uuid[VK_UUID_SIZE]) { |
| 191 | size_t commitIdLen = strlen(commitId); |
| 192 | assert(commitIdLen/2 >= VK_UUID_SIZE); |
| 193 | char str[3] = {}; |
| 194 | for (uint32_t i = 0; i < VK_UUID_SIZE; ++i) { |
| 195 | str[0] = commitId[2 * i + 0]; |
| 196 | str[1] = commitId[2 * i + 1]; |
| 197 | uuid[i] = static_cast<uint8_t>(strtol(str, NULL, 16)); |
| 198 | } |
| 199 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 202 | bool validate_and_capture_pipeline_shader_state(layer_data *dev_data, PIPELINE_STATE *pPipeline); |
| 203 | bool validate_compute_pipeline(layer_data *dev_data, PIPELINE_STATE *pPipeline); |
| 204 | typedef std::pair<unsigned, unsigned> descriptor_slot_t; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 205 | bool PreCallValidateCreateShaderModule(layer_data *dev_data, VkShaderModuleCreateInfo const *pCreateInfo, bool *spirv_valid); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 206 | |
| 207 | #endif //VULKAN_SHADER_VALIDATION_H |