2 * Copyright 2018 Collabora Ltd.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #ifndef SPIRV_BUILDER_H
25 #define SPIRV_BUILDER_H
27 #include "compiler/spirv/spirv.h"
28 #include "compiler/spirv/GLSL.std.450.h"
38 size_t num_words
, room
;
41 struct spirv_builder
{
44 struct spirv_buffer capabilities
;
45 struct spirv_buffer extensions
;
46 struct spirv_buffer imports
;
47 struct spirv_buffer memory_model
;
48 struct spirv_buffer entry_points
;
49 struct spirv_buffer exec_modes
;
50 struct spirv_buffer debug_names
;
51 struct spirv_buffer decorations
;
53 struct spirv_buffer types_const_defs
;
54 struct hash_table
*types
;
55 struct hash_table
*consts
;
57 struct spirv_buffer instructions
;
62 spirv_builder_new_id(struct spirv_builder
*b
)
68 spirv_builder_emit_cap(struct spirv_builder
*b
, SpvCapability cap
);
71 spirv_builder_emit_extension(struct spirv_builder
*b
, const char *ext
);
74 spirv_builder_emit_source(struct spirv_builder
*b
, SpvSourceLanguage lang
,
78 spirv_builder_emit_mem_model(struct spirv_builder
*b
,
79 SpvAddressingModel addr_model
,
80 SpvMemoryModel mem_model
);
83 spirv_builder_emit_name(struct spirv_builder
*b
, SpvId target
,
87 spirv_builder_emit_decoration(struct spirv_builder
*b
, SpvId target
,
88 SpvDecoration decoration
);
91 spirv_builder_emit_location(struct spirv_builder
*b
, SpvId target
,
95 spirv_builder_emit_component(struct spirv_builder
*b
, SpvId target
,
99 spirv_builder_emit_builtin(struct spirv_builder
*b
, SpvId target
,
103 spirv_builder_emit_index(struct spirv_builder
*b
, SpvId target
, int index
);
106 spirv_builder_emit_descriptor_set(struct spirv_builder
*b
, SpvId target
,
107 uint32_t descriptor_set
);
110 spirv_builder_emit_binding(struct spirv_builder
*b
, SpvId target
,
114 spirv_builder_emit_array_stride(struct spirv_builder
*b
, SpvId target
,
118 spirv_builder_emit_offset(struct spirv_builder
*b
, SpvId target
,
122 spirv_builder_emit_xfb_buffer(struct spirv_builder
*b
, SpvId target
,
126 spirv_builder_emit_xfb_stride(struct spirv_builder
*b
, SpvId target
,
130 spirv_builder_emit_member_offset(struct spirv_builder
*b
, SpvId target
,
131 uint32_t member
, uint32_t offset
);
134 spirv_builder_emit_entry_point(struct spirv_builder
*b
,
135 SpvExecutionModel exec_model
, SpvId entry_point
,
136 const char *name
, const SpvId interfaces
[],
137 size_t num_interfaces
);
140 spirv_builder_emit_exec_mode(struct spirv_builder
*b
, SpvId entry_point
,
141 SpvExecutionMode exec_mode
);
144 spirv_builder_function(struct spirv_builder
*b
, SpvId result
,
146 SpvFunctionControlMask function_control
,
147 SpvId function_type
);
150 spirv_builder_function_end(struct spirv_builder
*b
);
153 spirv_builder_label(struct spirv_builder
*b
, SpvId label
);
156 spirv_builder_return(struct spirv_builder
*b
);
159 spirv_builder_emit_undef(struct spirv_builder
*b
, SpvId result_type
);
162 spirv_builder_emit_load(struct spirv_builder
*b
, SpvId result_type
,
166 spirv_builder_emit_store(struct spirv_builder
*b
, SpvId pointer
, SpvId object
);
169 spirv_builder_emit_access_chain(struct spirv_builder
*b
, SpvId result_type
,
170 SpvId base
, const SpvId indexes
[],
174 spirv_builder_emit_unop(struct spirv_builder
*b
, SpvOp op
, SpvId result_type
,
178 spirv_builder_emit_binop(struct spirv_builder
*b
, SpvOp op
, SpvId result_type
,
179 SpvId operand0
, SpvId operand1
);
182 spirv_builder_emit_triop(struct spirv_builder
*b
, SpvOp op
, SpvId result_type
,
183 SpvId operand0
, SpvId operand1
, SpvId operand2
);
186 spirv_builder_emit_composite_extract(struct spirv_builder
*b
, SpvId result_type
,
187 SpvId composite
, const uint32_t indexes
[],
191 spirv_builder_emit_composite_construct(struct spirv_builder
*b
,
193 const SpvId constituents
[],
194 size_t num_constituents
);
197 spirv_builder_emit_vector_shuffle(struct spirv_builder
*b
, SpvId result_type
,
198 SpvId vector_1
, SpvId vector_2
,
199 const uint32_t components
[],
200 size_t num_components
);
202 spirv_builder_emit_vector_extract(struct spirv_builder
*b
, SpvId result_type
,
206 spirv_builder_emit_vector_insert(struct spirv_builder
*b
, SpvId result_type
,
211 spirv_builder_emit_branch(struct spirv_builder
*b
, SpvId label
);
214 spirv_builder_emit_selection_merge(struct spirv_builder
*b
, SpvId merge_block
,
215 SpvSelectionControlMask selection_control
);
218 spirv_builder_loop_merge(struct spirv_builder
*b
, SpvId merge_block
,
219 SpvId cont_target
, SpvLoopControlMask loop_control
);
222 spirv_builder_emit_branch_conditional(struct spirv_builder
*b
, SpvId condition
,
223 SpvId true_label
, SpvId false_label
);
226 spirv_builder_emit_phi(struct spirv_builder
*b
, SpvId result_type
,
227 size_t num_vars
, size_t *position
);
230 spirv_builder_set_phi_operand(struct spirv_builder
*b
, size_t position
,
231 size_t index
, SpvId variable
, SpvId parent
);
234 spirv_builder_emit_kill(struct spirv_builder
*b
);
238 spirv_builder_emit_image_sample(struct spirv_builder
*b
,
251 spirv_builder_emit_image(struct spirv_builder
*b
, SpvId result_type
,
252 SpvId sampled_image
);
255 spirv_builder_emit_image_fetch(struct spirv_builder
*b
,
263 spirv_builder_emit_image_query_size(struct spirv_builder
*b
,
269 spirv_builder_emit_ext_inst(struct spirv_builder
*b
, SpvId result_type
,
270 SpvId set
, uint32_t instruction
,
271 const SpvId args
[], size_t num_args
);
274 spirv_builder_type_void(struct spirv_builder
*b
);
277 spirv_builder_type_bool(struct spirv_builder
*b
);
280 spirv_builder_type_int(struct spirv_builder
*b
, unsigned width
);
283 spirv_builder_type_uint(struct spirv_builder
*b
, unsigned width
);
286 spirv_builder_type_float(struct spirv_builder
*b
, unsigned width
);
289 spirv_builder_type_image(struct spirv_builder
*b
, SpvId sampled_type
,
290 SpvDim dim
, bool depth
, bool arrayed
, bool ms
,
291 unsigned sampled
, SpvImageFormat image_format
);
294 spirv_builder_type_sampled_image(struct spirv_builder
*b
, SpvId image_type
);
297 spirv_builder_type_pointer(struct spirv_builder
*b
,
298 SpvStorageClass storage_class
, SpvId type
);
301 spirv_builder_type_vector(struct spirv_builder
*b
, SpvId component_type
,
302 unsigned component_count
);
305 spirv_builder_type_matrix(struct spirv_builder
*b
, SpvId component_type
,
306 unsigned component_count
);
309 spirv_builder_type_array(struct spirv_builder
*b
, SpvId component_type
,
313 spirv_builder_type_struct(struct spirv_builder
*b
, const SpvId member_types
[],
314 size_t num_member_types
);
317 spirv_builder_type_function(struct spirv_builder
*b
, SpvId return_type
,
318 const SpvId parameter_types
[],
319 size_t num_parameter_types
);
322 spirv_builder_const_bool(struct spirv_builder
*b
, bool val
);
325 spirv_builder_const_int(struct spirv_builder
*b
, int width
, int32_t val
);
328 spirv_builder_const_uint(struct spirv_builder
*b
, int width
, uint32_t val
);
331 spirv_builder_const_float(struct spirv_builder
*b
, int width
, float val
);
334 spirv_builder_const_composite(struct spirv_builder
*b
, SpvId result_type
,
335 const SpvId constituents
[],
336 size_t num_constituents
);
339 spirv_builder_emit_var(struct spirv_builder
*b
, SpvId type
,
340 SpvStorageClass storage_class
);
343 spirv_builder_import(struct spirv_builder
*b
, const char *name
);
346 spirv_builder_get_num_words(struct spirv_builder
*b
);
349 spirv_builder_get_words(struct spirv_builder
*b
, uint32_t *words
,