blob: 99a784caec6678745b6a8038156335afea529094 [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
23// A forward iterator over spirv instructions. Provides easy access to len, opcode, and content words
24// without the caller needing to care too much about the physical SPIRV module layout.
25struct spirv_inst_iter {
26 std::vector<uint32_t>::const_iterator zero;
27 std::vector<uint32_t>::const_iterator it;
28
29 uint32_t len() {
30 auto result = *it >> 16;
31 assert(result > 0);
32 return result;
33 }
34
35 uint32_t opcode() { return *it & 0x0ffffu; }
36
37 uint32_t const &word(unsigned n) {
38 assert(n < len());
39 return it[n];
40 }
41
42 uint32_t offset() { return (uint32_t)(it - zero); }
43
44 spirv_inst_iter() {}
45
46 spirv_inst_iter(std::vector<uint32_t>::const_iterator zero, std::vector<uint32_t>::const_iterator it) : zero(zero), it(it) {}
47
48 bool operator==(spirv_inst_iter const &other) { return it == other.it; }
49
50 bool operator!=(spirv_inst_iter const &other) { return it != other.it; }
51
52 spirv_inst_iter operator++(int) { // x++
53 spirv_inst_iter ii = *this;
54 it += len();
55 return ii;
56 }
57
58 spirv_inst_iter operator++() { // ++x;
59 it += len();
60 return *this;
61 }
62
63 // The iterator and the value are the same thing.
64 spirv_inst_iter &operator*() { return *this; }
65 spirv_inst_iter const &operator*() const { return *this; }
66};
67
68struct shader_module {
69 // The spirv image itself
70 std::vector<uint32_t> words;
71 // A mapping of <id> to the first word of its def. this is useful because walking type
72 // trees, constant expressions, etc requires jumping all over the instruction stream.
73 std::unordered_map<unsigned, unsigned> def_index;
74 bool has_valid_spirv;
75
76 shader_module(VkShaderModuleCreateInfo const *pCreateInfo)
77 : words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
78 def_index(),
79 has_valid_spirv(true) {
80 build_def_index();
81 }
82
83 shader_module() : has_valid_spirv(false) {}
84
85 // Expose begin() / end() to enable range-based for
86 spirv_inst_iter begin() const { return spirv_inst_iter(words.begin(), words.begin() + 5); } // First insn
87 spirv_inst_iter end() const { return spirv_inst_iter(words.begin(), words.end()); } // Just past last insn
88 // Given an offset into the module, produce an iterator there.
89 spirv_inst_iter at(unsigned offset) const { return spirv_inst_iter(words.begin(), words.begin() + offset); }
90
91 // Gets an iterator to the definition of an id
92 spirv_inst_iter get_def(unsigned id) const {
93 auto it = def_index.find(id);
94 if (it == def_index.end()) {
95 return end();
96 }
97 return at(it->second);
98 }
99
100 void build_def_index();
101};
102
Chris Forbes9a61e082017-07-24 15:35:29 -0700103// TODO: Wire this up to SPIRV-Tools commit hash
104#define VALIDATION_CACHE_VERSION 1
105
106class ValidationCache {
107 // hashes of shaders that have passed validation before, and can be skipped.
108 // we don't store negative results, as we would have to also store what was
109 // wrong with them; also, we expect they will get fixed, so we're less
110 // likely to see them again.
111 std::unordered_set<uint32_t> good_shader_hashes;
112 ValidationCache() {}
113
114public:
115 static VkValidationCacheEXT Create(VkValidationCacheCreateInfoEXT const *pCreateInfo) {
116 auto cache = new ValidationCache();
117 cache->Load(pCreateInfo);
118 return VkValidationCacheEXT(cache);
119 }
120
121 void Load(VkValidationCacheCreateInfoEXT const *pCreateInfo) {
122 auto size = 8u + VK_UUID_SIZE;
123 if (!pCreateInfo->pInitialData || pCreateInfo->initialDataSize < size)
124 return;
125
126 uint32_t const *data = (uint32_t const *)pCreateInfo->pInitialData;
127 if (data[0] != size)
128 return;
129 if (data[1] != VK_VALIDATION_CACHE_HEADER_VERSION_ONE_EXT)
130 return;
131 if (data[2] != VALIDATION_CACHE_VERSION || data[3] || data[4] || data[5])
132 return; // different version
133
134 data += 6;
135
136 for (;size < pCreateInfo->initialDataSize;
137 data++, size += sizeof(uint32_t)) {
138 good_shader_hashes.insert(*data);
139 }
140 }
141
142 void Write(size_t *pDataSize, void *pData) {
143 auto headerSize = 8u + VK_UUID_SIZE;
144 if (!pData) {
145 *pDataSize = headerSize + good_shader_hashes.size() * sizeof(uint32_t);
146 return;
147 }
148
149 if (*pDataSize < headerSize) {
150 *pDataSize = 0;
151 return; // Too small for even the header!
152 }
153
154 uint32_t *out = (uint32_t *)pData;
155 size_t actualSize = headerSize;
156
157 // Write the header
158 *out++ = headerSize;
159 *out++ = VK_VALIDATION_CACHE_HEADER_VERSION_ONE_EXT;
160 *out++ = VALIDATION_CACHE_VERSION;
161 *out++ = 0;
162 *out++ = 0;
163 *out++ = 0;
164
165 for (auto it = good_shader_hashes.begin();
166 it != good_shader_hashes.end() && actualSize < *pDataSize;
167 it++, out++, actualSize += sizeof(uint32_t)) {
168 *out = *it;
169 }
170
171 *pDataSize = actualSize;
172 }
173
174 void Merge(ValidationCache const *other) {
175 for (auto h : other->good_shader_hashes)
176 good_shader_hashes.insert(h);
177 }
178
179 static uint32_t MakeShaderHash(VkShaderModuleCreateInfo const *smci);
180
181 bool Contains(uint32_t hash) {
182 return good_shader_hashes.count(hash) != 0;
183 }
184
185 void Insert(uint32_t hash) {
186 good_shader_hashes.insert(hash);
187 }
188};
189
Chris Forbes47567b72017-06-09 12:09:45 -0700190bool validate_and_capture_pipeline_shader_state(layer_data *dev_data, PIPELINE_STATE *pPipeline);
191bool validate_compute_pipeline(layer_data *dev_data, PIPELINE_STATE *pPipeline);
192typedef std::pair<unsigned, unsigned> descriptor_slot_t;
Chris Forbes4ae55b32017-06-09 14:42:56 -0700193bool PreCallValidateCreateShaderModule(layer_data *dev_data, VkShaderModuleCreateInfo const *pCreateInfo, bool *spirv_valid);
Chris Forbes47567b72017-06-09 12:09:45 -0700194
195#endif //VULKAN_SHADER_VALIDATION_H