blob: 679bc322e9091dfe2795492d1dc7db067d641af4 [file] [log] [blame]
Chris Forbes47567b72017-06-09 12:09:45 -07001/* Copyright (c) 2015-2017 The Khronos Group Inc.
2 * Copyright (c) 2015-2017 Valve Corporation
3 * Copyright (c) 2015-2017 LunarG, Inc.
4 * Copyright (C) 2015-2017 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Chris Forbes <chrisf@ijw.co.nz>
19 */
20#ifndef VULKAN_SHADER_VALIDATION_H
21#define VULKAN_SHADER_VALIDATION_H
22
Cort Strattona81851c2017-11-06 19:13:53 -080023#include <spirv_tools_commit_id.h>
24
Chris Forbes47567b72017-06-09 12:09:45 -070025// 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.
27struct 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
70struct 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;
Mark Young4e919b22018-05-21 15:53:59 -060077 VkShaderModule vk_shader_module;
Chris Forbes47567b72017-06-09 12:09:45 -070078
Mark Young4e919b22018-05-21 15:53:59 -060079 shader_module(VkShaderModuleCreateInfo const *pCreateInfo, VkShaderModule shaderModule)
Chris Forbes47567b72017-06-09 12:09:45 -070080 : words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
81 def_index(),
Mark Young4e919b22018-05-21 15:53:59 -060082 has_valid_spirv(true),
83 vk_shader_module(shaderModule) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060084 BuildDefIndex();
Chris Forbes47567b72017-06-09 12:09:45 -070085 }
86
Mark Young4e919b22018-05-21 15:53:59 -060087 shader_module() : has_valid_spirv(false), vk_shader_module(VK_NULL_HANDLE) {}
Chris Forbes47567b72017-06-09 12:09:45 -070088
89 // Expose begin() / end() to enable range-based for
90 spirv_inst_iter begin() const { return spirv_inst_iter(words.begin(), words.begin() + 5); } // First insn
91 spirv_inst_iter end() const { return spirv_inst_iter(words.begin(), words.end()); } // Just past last insn
92 // Given an offset into the module, produce an iterator there.
93 spirv_inst_iter at(unsigned offset) const { return spirv_inst_iter(words.begin(), words.begin() + offset); }
94
95 // Gets an iterator to the definition of an id
96 spirv_inst_iter get_def(unsigned id) const {
97 auto it = def_index.find(id);
98 if (it == def_index.end()) {
99 return end();
100 }
101 return at(it->second);
102 }
103
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600104 void BuildDefIndex();
Chris Forbes47567b72017-06-09 12:09:45 -0700105};
106
Chris Forbes9a61e082017-07-24 15:35:29 -0700107class ValidationCache {
108 // hashes of shaders that have passed validation before, and can be skipped.
109 // we don't store negative results, as we would have to also store what was
110 // wrong with them; also, we expect they will get fixed, so we're less
111 // likely to see them again.
112 std::unordered_set<uint32_t> good_shader_hashes;
113 ValidationCache() {}
114
Dave Houltona9df0ce2018-02-07 10:51:23 -0700115 public:
Chris Forbes9a61e082017-07-24 15:35:29 -0700116 static VkValidationCacheEXT Create(VkValidationCacheCreateInfoEXT const *pCreateInfo) {
117 auto cache = new ValidationCache();
118 cache->Load(pCreateInfo);
119 return VkValidationCacheEXT(cache);
120 }
121
122 void Load(VkValidationCacheCreateInfoEXT const *pCreateInfo) {
Cort Stratton77955d82018-02-01 23:14:50 -0800123 const auto headerSize = 2 * sizeof(uint32_t) + VK_UUID_SIZE;
124 auto size = headerSize;
Dave Houltona9df0ce2018-02-07 10:51:23 -0700125 if (!pCreateInfo->pInitialData || pCreateInfo->initialDataSize < size) return;
Chris Forbes9a61e082017-07-24 15:35:29 -0700126
127 uint32_t const *data = (uint32_t const *)pCreateInfo->pInitialData;
Dave Houltona9df0ce2018-02-07 10:51:23 -0700128 if (data[0] != size) return;
129 if (data[1] != VK_VALIDATION_CACHE_HEADER_VERSION_ONE_EXT) return;
Cort Stratton77955d82018-02-01 23:14:50 -0800130 uint8_t expected_uuid[VK_UUID_SIZE];
131 Sha1ToVkUuid(SPIRV_TOOLS_COMMIT_ID, expected_uuid);
Dave Houltona9df0ce2018-02-07 10:51:23 -0700132 if (memcmp(&data[2], expected_uuid, VK_UUID_SIZE) != 0) return; // different version
Chris Forbes9a61e082017-07-24 15:35:29 -0700133
Dave Houltona9df0ce2018-02-07 10:51:23 -0700134 data = (uint32_t const *)(reinterpret_cast<uint8_t const *>(data) + headerSize);
Chris Forbes9a61e082017-07-24 15:35:29 -0700135
Dave Houltona9df0ce2018-02-07 10:51:23 -0700136 for (; size < pCreateInfo->initialDataSize; data++, size += sizeof(uint32_t)) {
Chris Forbes9a61e082017-07-24 15:35:29 -0700137 good_shader_hashes.insert(*data);
138 }
139 }
140
141 void Write(size_t *pDataSize, void *pData) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700142 const auto headerSize = 2 * sizeof(uint32_t) + VK_UUID_SIZE; // 4 bytes for header size + 4 bytes for version number + UUID
Chris Forbes9a61e082017-07-24 15:35:29 -0700143 if (!pData) {
144 *pDataSize = headerSize + good_shader_hashes.size() * sizeof(uint32_t);
145 return;
146 }
147
148 if (*pDataSize < headerSize) {
149 *pDataSize = 0;
Dave Houltona9df0ce2018-02-07 10:51:23 -0700150 return; // Too small for even the header!
Chris Forbes9a61e082017-07-24 15:35:29 -0700151 }
152
153 uint32_t *out = (uint32_t *)pData;
154 size_t actualSize = headerSize;
155
156 // Write the header
157 *out++ = headerSize;
158 *out++ = VK_VALIDATION_CACHE_HEADER_VERSION_ONE_EXT;
Dave Houltona9df0ce2018-02-07 10:51:23 -0700159 Sha1ToVkUuid(SPIRV_TOOLS_COMMIT_ID, reinterpret_cast<uint8_t *>(out));
160 out = (uint32_t *)(reinterpret_cast<uint8_t *>(out) + VK_UUID_SIZE);
Chris Forbes9a61e082017-07-24 15:35:29 -0700161
Dave Houltona9df0ce2018-02-07 10:51:23 -0700162 for (auto it = good_shader_hashes.begin(); it != good_shader_hashes.end() && actualSize < *pDataSize;
Chris Forbes9a61e082017-07-24 15:35:29 -0700163 it++, out++, actualSize += sizeof(uint32_t)) {
164 *out = *it;
165 }
166
167 *pDataSize = actualSize;
168 }
169
170 void Merge(ValidationCache const *other) {
Chris Forbesf73483b2017-11-22 16:54:46 -0800171 good_shader_hashes.reserve(good_shader_hashes.size() + other->good_shader_hashes.size());
Dave Houltona9df0ce2018-02-07 10:51:23 -0700172 for (auto h : other->good_shader_hashes) good_shader_hashes.insert(h);
Chris Forbes9a61e082017-07-24 15:35:29 -0700173 }
174
175 static uint32_t MakeShaderHash(VkShaderModuleCreateInfo const *smci);
176
Dave Houltona9df0ce2018-02-07 10:51:23 -0700177 bool Contains(uint32_t hash) { return good_shader_hashes.count(hash) != 0; }
Chris Forbes9a61e082017-07-24 15:35:29 -0700178
Dave Houltona9df0ce2018-02-07 10:51:23 -0700179 void Insert(uint32_t hash) { good_shader_hashes.insert(hash); }
180
181 private:
182 void Sha1ToVkUuid(const char *sha1_str, uint8_t uuid[VK_UUID_SIZE]) {
183 // Convert sha1_str from a hex string to binary. We only need VK_UUID_BYTES of
184 // output, so pad with zeroes if the input string is shorter than that, and truncate
185 // if it's longer.
186 char padded_sha1_str[2 * VK_UUID_SIZE + 1] = {};
187 strncpy(padded_sha1_str, sha1_str, 2 * VK_UUID_SIZE + 1);
188 char byte_str[3] = {};
189 for (uint32_t i = 0; i < VK_UUID_SIZE; ++i) {
190 byte_str[0] = padded_sha1_str[2 * i + 0];
191 byte_str[1] = padded_sha1_str[2 * i + 1];
192 uuid[i] = static_cast<uint8_t>(strtol(byte_str, NULL, 16));
193 }
Cort Strattonb614d332017-11-22 16:05:49 -0800194 }
Chris Forbes9a61e082017-07-24 15:35:29 -0700195};
196
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600197bool ValidateAndCapturePipelineShaderState(layer_data *dev_data, PIPELINE_STATE *pPipeline);
198bool ValidateComputePipeline(layer_data *dev_data, PIPELINE_STATE *pPipeline);
Eric Werness30127fd2018-10-31 21:01:03 -0700199bool ValidateRayTracingPipelineNV(layer_data *dev_data, PIPELINE_STATE *pipeline);
Chris Forbes47567b72017-06-09 12:09:45 -0700200typedef std::pair<unsigned, unsigned> descriptor_slot_t;
Chris Forbes4ae55b32017-06-09 14:42:56 -0700201bool PreCallValidateCreateShaderModule(layer_data *dev_data, VkShaderModuleCreateInfo const *pCreateInfo, bool *spirv_valid);
Chris Forbes47567b72017-06-09 12:09:45 -0700202
Dave Houltona9df0ce2018-02-07 10:51:23 -0700203#endif // VULKAN_SHADER_VALIDATION_H