2 * Copyright © 2015 Intel Corporation
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:
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
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
24 * Jason Ekstrand (jason@jlekstrand.net)
29 #include "nir_spirv.h"
30 #include "nir_builder.h"
34 struct vtn_decoration
;
37 vtn_value_type_invalid
= 0,
39 vtn_value_type_string
,
40 vtn_value_type_decoration_group
,
42 vtn_value_type_constant
,
44 vtn_value_type_function
,
47 vtn_value_type_extension
,
48 vtn_value_type_image_pointer
,
49 vtn_value_type_sampled_image
,
53 /* Merge opcode if this block contains a merge; SpvOpNop otherwise. */
55 uint32_t merge_block_id
;
56 const uint32_t *label
;
57 const uint32_t *branch
;
62 struct exec_node node
;
64 nir_function_impl
*impl
;
65 struct vtn_block
*start_block
;
70 typedef bool (*vtn_instruction_handler
)(struct vtn_builder
*, uint32_t,
71 const uint32_t *, unsigned);
73 struct vtn_ssa_value
{
76 struct vtn_ssa_value
**elems
;
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.
83 struct vtn_ssa_value
*transposed
;
85 const struct glsl_type
*type
;
89 const struct glsl_type
*type
;
91 /* for matrices, whether the matrix is stored row-major */
94 /* for structs, the offset of each member */
97 /* for structs, whether it was decorated as a "non-SSBO-like" block */
100 /* for structs, whether it was decorated as an "SSBO-like" block */
103 /* for structs with block == true, whether this is a builtin block (i.e. a
104 * block that contains only builtins).
108 /* Image format for image_load_store type images */
109 unsigned image_format
;
111 /* for arrays and matrices, the array stride */
114 /* for arrays, the vtn_type for the elements of the array */
115 struct vtn_type
*array_element
;
117 /* for structures, the vtn_type for each member */
118 struct vtn_type
**members
;
120 /* Whether this type, or a parent type, has been decorated as a builtin */
126 struct vtn_image_pointer
{
127 nir_deref_var
*deref
;
132 struct vtn_sampled_image
{
133 nir_deref_var
*image
; /* Image or array of images */
134 nir_deref_var
*sampler
; /* Sampler */
138 enum vtn_value_type value_type
;
140 struct vtn_decoration
*decoration
;
144 struct vtn_type
*type
;
146 nir_constant
*constant
;
147 const struct glsl_type
*const_type
;
150 nir_deref_var
*deref
;
151 struct vtn_type
*deref_type
;
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
;
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
;
174 nir_function_impl
*impl
;
175 struct vtn_block
*block
;
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.
183 struct hash_table
*const_table
;
186 * Map from nir_block to the vtn_block which ends with it -- used for
187 * handling phi nodes.
189 struct hash_table
*block_table
;
192 * NIR variable for each SPIR-V builtin.
197 } builtins
[42]; /* XXX need symbolic constant from SPIR-V header */
199 unsigned value_id_bound
;
200 struct vtn_value
*values
;
202 SpvExecutionModel execution_model
;
203 bool origin_upper_left
;
204 struct vtn_value
*entry_point
;
206 struct vtn_function
*func
;
207 struct exec_list functions
;
209 /* Current function parameter index */
210 unsigned func_param_idx
;
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
)
217 assert(value_id
< b
->value_id_bound
);
218 assert(b
->values
[value_id
].value_type
== vtn_value_type_invalid
);
220 b
->values
[value_id
].value_type
= value_type
;
222 return &b
->values
[value_id
];
225 static inline struct vtn_value
*
226 vtn_untyped_value(struct vtn_builder
*b
, uint32_t value_id
)
228 assert(value_id
< b
->value_id_bound
);
229 return &b
->values
[value_id
];
232 static inline struct vtn_value
*
233 vtn_value(struct vtn_builder
*b
, uint32_t value_id
,
234 enum vtn_value_type value_type
)
236 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
237 assert(val
->value_type
== value_type
);
241 struct vtn_ssa_value
*vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
);
243 typedef void (*vtn_decoration_foreach_cb
)(struct vtn_builder
*,
246 const struct vtn_decoration
*,
249 void vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
250 vtn_decoration_foreach_cb cb
, void *data
);
252 bool vtn_handle_glsl450_instruction(struct vtn_builder
*b
, uint32_t ext_opcode
,
253 const uint32_t *words
, unsigned count
);