blob: 3d1c69eb6f35c981c157f24ba597bdcedd5b8edd [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
103bool validate_and_capture_pipeline_shader_state(layer_data *dev_data, PIPELINE_STATE *pPipeline);
104bool validate_compute_pipeline(layer_data *dev_data, PIPELINE_STATE *pPipeline);
105typedef std::pair<unsigned, unsigned> descriptor_slot_t;
106
107#endif //VULKAN_SHADER_VALIDATION_H