nir/gather_info: Handle multi-slot variables in io bitfields
[mesa.git] / src / glsl / nir / spirv_to_nir_private.h
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "nir.h"
29 #include "nir_spirv.h"
30 #include "nir_builder.h"
31 #include "spirv.h"
32
33 struct vtn_builder;
34 struct vtn_decoration;
35
36 enum vtn_value_type {
37 vtn_value_type_invalid = 0,
38 vtn_value_type_undef,
39 vtn_value_type_string,
40 vtn_value_type_decoration_group,
41 vtn_value_type_type,
42 vtn_value_type_constant,
43 vtn_value_type_deref,
44 vtn_value_type_function,
45 vtn_value_type_block,
46 vtn_value_type_ssa,
47 vtn_value_type_extension,
48 vtn_value_type_image_pointer,
49 vtn_value_type_sampled_image,
50 };
51
52 struct vtn_block {
53 /* Merge opcode if this block contains a merge; SpvOpNop otherwise. */
54 SpvOp merge_op;
55 uint32_t merge_block_id;
56 const uint32_t *label;
57 const uint32_t *branch;
58 nir_block *block;
59 };
60
61 struct vtn_function {
62 struct exec_node node;
63
64 nir_function_impl *impl;
65 struct vtn_block *start_block;
66
67 const uint32_t *end;
68 };
69
70 typedef bool (*vtn_instruction_handler)(struct vtn_builder *, uint32_t,
71 const uint32_t *, unsigned);
72
73 struct vtn_ssa_value {
74 union {
75 nir_ssa_def *def;
76 struct vtn_ssa_value **elems;
77 };
78
79 /* For matrices, if this is non-NULL, then this value is actually the
80 * transpose of some other value. The value that `transposed` points to
81 * always dominates this value.
82 */
83 struct vtn_ssa_value *transposed;
84
85 const struct glsl_type *type;
86 };
87
88 struct vtn_type {
89 const struct glsl_type *type;
90
91 /* for matrices, whether the matrix is stored row-major */
92 bool row_major;
93
94 /* for structs, the offset of each member */
95 unsigned *offsets;
96
97 /* for structs, whether it was decorated as a "non-SSBO-like" block */
98 bool block;
99
100 /* for structs, whether it was decorated as an "SSBO-like" block */
101 bool buffer_block;
102
103 /* for structs with block == true, whether this is a builtin block (i.e. a
104 * block that contains only builtins).
105 */
106 bool builtin_block;
107
108 /* Image format for image_load_store type images */
109 unsigned image_format;
110
111 /* for arrays and matrices, the array stride */
112 unsigned stride;
113
114 /* for arrays, the vtn_type for the elements of the array */
115 struct vtn_type *array_element;
116
117 /* for structures, the vtn_type for each member */
118 struct vtn_type **members;
119
120 /* Whether this type, or a parent type, has been decorated as a builtin */
121 bool is_builtin;
122
123 SpvBuiltIn builtin;
124 };
125
126 struct vtn_image_pointer {
127 nir_deref_var *deref;
128 nir_ssa_def *coord;
129 nir_ssa_def *sample;
130 };
131
132 struct vtn_sampled_image {
133 nir_deref_var *image; /* Image or array of images */
134 nir_deref_var *sampler; /* Sampler */
135 };
136
137 struct vtn_value {
138 enum vtn_value_type value_type;
139 const char *name;
140 struct vtn_decoration *decoration;
141 union {
142 void *ptr;
143 char *str;
144 struct vtn_type *type;
145 struct {
146 nir_constant *constant;
147 const struct glsl_type *const_type;
148 };
149 struct {
150 nir_deref_var *deref;
151 struct vtn_type *deref_type;
152 };
153 struct vtn_image_pointer *image;
154 struct vtn_sampled_image *sampled_image;
155 struct vtn_function *func;
156 struct vtn_block *block;
157 struct vtn_ssa_value *ssa;
158 vtn_instruction_handler ext_handler;
159 };
160 };
161
162 struct vtn_decoration {
163 struct vtn_decoration *next;
164 int member; /* -1 if not a member decoration */
165 const uint32_t *literals;
166 struct vtn_value *group;
167 SpvDecoration decoration;
168 };
169
170 struct vtn_builder {
171 nir_builder nb;
172
173 nir_shader *shader;
174 nir_function_impl *impl;
175 struct vtn_block *block;
176
177 /*
178 * In SPIR-V, constants are global, whereas in NIR, the load_const
179 * instruction we use is per-function. So while we parse each function, we
180 * keep a hash table of constants we've resolved to nir_ssa_value's so
181 * far, and we lazily resolve them when we see them used in a function.
182 */
183 struct hash_table *const_table;
184
185 /*
186 * Map from nir_block to the vtn_block which ends with it -- used for
187 * handling phi nodes.
188 */
189 struct hash_table *block_table;
190
191 /*
192 * NIR variable for each SPIR-V builtin.
193 */
194 struct {
195 nir_variable *in;
196 nir_variable *out;
197 } builtins[42]; /* XXX need symbolic constant from SPIR-V header */
198
199 unsigned value_id_bound;
200 struct vtn_value *values;
201
202 SpvExecutionModel execution_model;
203 bool origin_upper_left;
204 struct vtn_value *entry_point;
205
206 struct vtn_function *func;
207 struct exec_list functions;
208
209 /* Current function parameter index */
210 unsigned func_param_idx;
211 };
212
213 static inline struct vtn_value *
214 vtn_push_value(struct vtn_builder *b, uint32_t value_id,
215 enum vtn_value_type value_type)
216 {
217 assert(value_id < b->value_id_bound);
218 assert(b->values[value_id].value_type == vtn_value_type_invalid);
219
220 b->values[value_id].value_type = value_type;
221
222 return &b->values[value_id];
223 }
224
225 static inline struct vtn_value *
226 vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
227 {
228 assert(value_id < b->value_id_bound);
229 return &b->values[value_id];
230 }
231
232 static inline struct vtn_value *
233 vtn_value(struct vtn_builder *b, uint32_t value_id,
234 enum vtn_value_type value_type)
235 {
236 struct vtn_value *val = vtn_untyped_value(b, value_id);
237 assert(val->value_type == value_type);
238 return val;
239 }
240
241 struct vtn_ssa_value *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);
242
243 typedef void (*vtn_decoration_foreach_cb)(struct vtn_builder *,
244 struct vtn_value *,
245 int member,
246 const struct vtn_decoration *,
247 void *);
248
249 void vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
250 vtn_decoration_foreach_cb cb, void *data);
251
252 bool vtn_handle_glsl450_instruction(struct vtn_builder *b, uint32_t ext_opcode,
253 const uint32_t *words, unsigned count);