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 #include "spirv_builder.h"
26 #include "util/macros.h"
27 #include "util/u_bitcast.h"
28 #include "util/u_memory.h"
29 #include "util/hash_table.h"
36 spirv_buffer_grow(struct spirv_buffer
*b
, size_t needed
)
38 size_t new_room
= MAX3(64, (b
->room
* 3) / 2, needed
);
40 uint32_t *new_words
= realloc(b
->words
, new_room
* sizeof(uint32_t));
50 spirv_buffer_prepare(struct spirv_buffer
*b
, size_t needed
)
52 needed
+= b
->num_words
;
53 if (b
->room
>= b
->num_words
+ needed
)
56 return spirv_buffer_grow(b
, needed
);
60 spirv_buffer_emit_word(struct spirv_buffer
*b
, uint32_t word
)
62 assert(b
->num_words
< b
->room
);
63 b
->words
[b
->num_words
++] = word
;
67 spirv_buffer_emit_string(struct spirv_buffer
*b
, const char *str
)
71 while (str
[pos
] != '\0') {
72 word
|= str
[pos
] << (8 * (pos
% 4));
74 spirv_buffer_prepare(b
, 1);
75 spirv_buffer_emit_word(b
, word
);
80 spirv_buffer_prepare(b
, 1);
81 spirv_buffer_emit_word(b
, word
);
87 spirv_builder_emit_cap(struct spirv_builder
*b
, SpvCapability cap
)
89 spirv_buffer_prepare(&b
->capabilities
, 2);
90 spirv_buffer_emit_word(&b
->capabilities
, SpvOpCapability
| (2 << 16));
91 spirv_buffer_emit_word(&b
->capabilities
, cap
);
95 spirv_builder_emit_source(struct spirv_builder
*b
, SpvSourceLanguage lang
,
98 spirv_buffer_prepare(&b
->debug_names
, 3);
99 spirv_buffer_emit_word(&b
->debug_names
, SpvOpSource
| (3 << 16));
100 spirv_buffer_emit_word(&b
->debug_names
, lang
);
101 spirv_buffer_emit_word(&b
->debug_names
, version
);
105 spirv_builder_emit_mem_model(struct spirv_builder
*b
,
106 SpvAddressingModel addr_model
,
107 SpvMemoryModel mem_model
)
109 spirv_buffer_prepare(&b
->memory_model
, 3);
110 spirv_buffer_emit_word(&b
->memory_model
, SpvOpMemoryModel
| (3 << 16));
111 spirv_buffer_emit_word(&b
->memory_model
, addr_model
);
112 spirv_buffer_emit_word(&b
->memory_model
, mem_model
);
116 spirv_builder_emit_entry_point(struct spirv_builder
*b
,
117 SpvExecutionModel exec_model
, SpvId entry_point
,
118 const char *name
, const SpvId interfaces
[],
119 size_t num_interfaces
)
121 size_t pos
= b
->entry_points
.num_words
;
122 spirv_buffer_prepare(&b
->entry_points
, 3);
123 spirv_buffer_emit_word(&b
->entry_points
, SpvOpEntryPoint
);
124 spirv_buffer_emit_word(&b
->entry_points
, exec_model
);
125 spirv_buffer_emit_word(&b
->entry_points
, entry_point
);
126 int len
= spirv_buffer_emit_string(&b
->entry_points
, name
);
127 b
->entry_points
.words
[pos
] |= (3 + len
+ num_interfaces
) << 16;
128 spirv_buffer_prepare(&b
->entry_points
, num_interfaces
);
129 for (int i
= 0; i
< num_interfaces
; ++i
)
130 spirv_buffer_emit_word(&b
->entry_points
, interfaces
[i
]);
134 spirv_builder_emit_exec_mode(struct spirv_builder
*b
, SpvId entry_point
,
135 SpvExecutionMode exec_mode
)
137 spirv_buffer_prepare(&b
->exec_modes
, 3);
138 spirv_buffer_emit_word(&b
->exec_modes
, SpvOpExecutionMode
| (3 << 16));
139 spirv_buffer_emit_word(&b
->exec_modes
, entry_point
);
140 spirv_buffer_emit_word(&b
->exec_modes
, exec_mode
);
144 spirv_builder_emit_name(struct spirv_builder
*b
, SpvId target
,
147 size_t pos
= b
->debug_names
.num_words
;
148 spirv_buffer_prepare(&b
->debug_names
, 2);
149 spirv_buffer_emit_word(&b
->debug_names
, SpvOpName
);
150 spirv_buffer_emit_word(&b
->debug_names
, target
);
151 int len
= spirv_buffer_emit_string(&b
->debug_names
, name
);
152 b
->debug_names
.words
[pos
] |= (2 + len
) << 16;
156 emit_decoration(struct spirv_builder
*b
, SpvId target
,
157 SpvDecoration decoration
, const uint32_t extra_operands
[],
158 size_t num_extra_operands
)
160 int words
= 3 + num_extra_operands
;
161 spirv_buffer_prepare(&b
->decorations
, words
);
162 spirv_buffer_emit_word(&b
->decorations
, SpvOpDecorate
| (words
<< 16));
163 spirv_buffer_emit_word(&b
->decorations
, target
);
164 spirv_buffer_emit_word(&b
->decorations
, decoration
);
165 for (int i
= 0; i
< num_extra_operands
; ++i
)
166 spirv_buffer_emit_word(&b
->decorations
, extra_operands
[i
]);
170 spirv_builder_emit_decoration(struct spirv_builder
*b
, SpvId target
,
171 SpvDecoration decoration
)
173 emit_decoration(b
, target
, decoration
, NULL
, 0);
177 spirv_builder_emit_location(struct spirv_builder
*b
, SpvId target
,
180 uint32_t args
[] = { location
};
181 emit_decoration(b
, target
, SpvDecorationLocation
, args
, ARRAY_SIZE(args
));
185 spirv_builder_emit_component(struct spirv_builder
*b
, SpvId target
,
188 uint32_t args
[] = { component
};
189 emit_decoration(b
, target
, SpvDecorationComponent
, args
, ARRAY_SIZE(args
));
193 spirv_builder_emit_builtin(struct spirv_builder
*b
, SpvId target
,
196 uint32_t args
[] = { builtin
};
197 emit_decoration(b
, target
, SpvDecorationBuiltIn
, args
, ARRAY_SIZE(args
));
201 spirv_builder_emit_descriptor_set(struct spirv_builder
*b
, SpvId target
,
202 uint32_t descriptor_set
)
204 uint32_t args
[] = { descriptor_set
};
205 emit_decoration(b
, target
, SpvDecorationDescriptorSet
, args
,
210 spirv_builder_emit_binding(struct spirv_builder
*b
, SpvId target
,
213 uint32_t args
[] = { binding
};
214 emit_decoration(b
, target
, SpvDecorationBinding
, args
, ARRAY_SIZE(args
));
218 spirv_builder_emit_array_stride(struct spirv_builder
*b
, SpvId target
,
221 uint32_t args
[] = { stride
};
222 emit_decoration(b
, target
, SpvDecorationArrayStride
, args
, ARRAY_SIZE(args
));
226 emit_member_decoration(struct spirv_builder
*b
, SpvId target
, uint32_t member
,
227 SpvDecoration decoration
, const uint32_t extra_operands
[],
228 size_t num_extra_operands
)
230 int words
= 4 + num_extra_operands
;
231 spirv_buffer_prepare(&b
->decorations
, words
);
232 spirv_buffer_emit_word(&b
->decorations
,
233 SpvOpMemberDecorate
| (words
<< 16));
234 spirv_buffer_emit_word(&b
->decorations
, target
);
235 spirv_buffer_emit_word(&b
->decorations
, member
);
236 spirv_buffer_emit_word(&b
->decorations
, decoration
);
237 for (int i
= 0; i
< num_extra_operands
; ++i
)
238 spirv_buffer_emit_word(&b
->decorations
, extra_operands
[i
]);
242 spirv_builder_emit_member_offset(struct spirv_builder
*b
, SpvId target
,
243 uint32_t member
, uint32_t offset
)
245 uint32_t args
[] = { offset
};
246 emit_member_decoration(b
, target
, member
, SpvDecorationOffset
,
247 args
, ARRAY_SIZE(args
));
251 spirv_builder_emit_undef(struct spirv_builder
*b
, SpvId result_type
)
253 SpvId result
= spirv_builder_new_id(b
);
254 spirv_buffer_prepare(&b
->instructions
, 3);
255 spirv_buffer_emit_word(&b
->instructions
, SpvOpUndef
| (3 << 16));
256 spirv_buffer_emit_word(&b
->instructions
, result_type
);
257 spirv_buffer_emit_word(&b
->instructions
, result
);
262 spirv_builder_function(struct spirv_builder
*b
, SpvId result
,
264 SpvFunctionControlMask function_control
,
267 spirv_buffer_prepare(&b
->instructions
, 5);
268 spirv_buffer_emit_word(&b
->instructions
, SpvOpFunction
| (5 << 16));
269 spirv_buffer_emit_word(&b
->instructions
, return_type
);
270 spirv_buffer_emit_word(&b
->instructions
, result
);
271 spirv_buffer_emit_word(&b
->instructions
, function_control
);
272 spirv_buffer_emit_word(&b
->instructions
, function_type
);
276 spirv_builder_function_end(struct spirv_builder
*b
)
278 spirv_buffer_prepare(&b
->instructions
, 1);
279 spirv_buffer_emit_word(&b
->instructions
, SpvOpFunctionEnd
| (1 << 16));
283 spirv_builder_label(struct spirv_builder
*b
, SpvId label
)
285 spirv_buffer_prepare(&b
->instructions
, 2);
286 spirv_buffer_emit_word(&b
->instructions
, SpvOpLabel
| (2 << 16));
287 spirv_buffer_emit_word(&b
->instructions
, label
);
291 spirv_builder_return(struct spirv_builder
*b
)
293 spirv_buffer_prepare(&b
->instructions
, 1);
294 spirv_buffer_emit_word(&b
->instructions
, SpvOpReturn
| (1 << 16));
298 spirv_builder_emit_load(struct spirv_builder
*b
, SpvId result_type
,
301 return spirv_builder_emit_unop(b
, SpvOpLoad
, result_type
, pointer
);
305 spirv_builder_emit_store(struct spirv_builder
*b
, SpvId pointer
, SpvId object
)
307 spirv_buffer_prepare(&b
->instructions
, 3);
308 spirv_buffer_emit_word(&b
->instructions
, SpvOpStore
| (3 << 16));
309 spirv_buffer_emit_word(&b
->instructions
, pointer
);
310 spirv_buffer_emit_word(&b
->instructions
, object
);
314 spirv_builder_emit_access_chain(struct spirv_builder
*b
, SpvId result_type
,
315 SpvId base
, const SpvId indexes
[],
318 SpvId result
= spirv_builder_new_id(b
);
320 int words
= 4 + num_indexes
;
321 spirv_buffer_prepare(&b
->instructions
, words
);
322 spirv_buffer_emit_word(&b
->instructions
, SpvOpAccessChain
| (words
<< 16));
323 spirv_buffer_emit_word(&b
->instructions
, result_type
);
324 spirv_buffer_emit_word(&b
->instructions
, result
);
325 spirv_buffer_emit_word(&b
->instructions
, base
);
326 for (int i
= 0; i
< num_indexes
; ++i
)
327 spirv_buffer_emit_word(&b
->instructions
, indexes
[i
]);
333 spirv_builder_emit_unop(struct spirv_builder
*b
, SpvOp op
, SpvId result_type
,
336 SpvId result
= spirv_builder_new_id(b
);
337 spirv_buffer_prepare(&b
->instructions
, 4);
338 spirv_buffer_emit_word(&b
->instructions
, op
| (4 << 16));
339 spirv_buffer_emit_word(&b
->instructions
, result_type
);
340 spirv_buffer_emit_word(&b
->instructions
, result
);
341 spirv_buffer_emit_word(&b
->instructions
, operand
);
346 spirv_builder_emit_binop(struct spirv_builder
*b
, SpvOp op
, SpvId result_type
,
347 SpvId operand0
, SpvId operand1
)
349 SpvId result
= spirv_builder_new_id(b
);
350 spirv_buffer_prepare(&b
->instructions
, 5);
351 spirv_buffer_emit_word(&b
->instructions
, op
| (5 << 16));
352 spirv_buffer_emit_word(&b
->instructions
, result_type
);
353 spirv_buffer_emit_word(&b
->instructions
, result
);
354 spirv_buffer_emit_word(&b
->instructions
, operand0
);
355 spirv_buffer_emit_word(&b
->instructions
, operand1
);
360 spirv_builder_emit_triop(struct spirv_builder
*b
, SpvOp op
, SpvId result_type
,
361 SpvId operand0
, SpvId operand1
, SpvId operand2
)
363 SpvId result
= spirv_builder_new_id(b
);
364 spirv_buffer_prepare(&b
->instructions
, 6);
365 spirv_buffer_emit_word(&b
->instructions
, op
| (6 << 16));
366 spirv_buffer_emit_word(&b
->instructions
, result_type
);
367 spirv_buffer_emit_word(&b
->instructions
, result
);
368 spirv_buffer_emit_word(&b
->instructions
, operand0
);
369 spirv_buffer_emit_word(&b
->instructions
, operand1
);
370 spirv_buffer_emit_word(&b
->instructions
, operand2
);
375 spirv_builder_emit_composite_extract(struct spirv_builder
*b
, SpvId result_type
,
376 SpvId composite
, const uint32_t indexes
[],
379 SpvId result
= spirv_builder_new_id(b
);
381 assert(num_indexes
> 0);
382 int words
= 4 + num_indexes
;
383 spirv_buffer_prepare(&b
->instructions
, words
);
384 spirv_buffer_emit_word(&b
->instructions
,
385 SpvOpCompositeExtract
| (words
<< 16));
386 spirv_buffer_emit_word(&b
->instructions
, result_type
);
387 spirv_buffer_emit_word(&b
->instructions
, result
);
388 spirv_buffer_emit_word(&b
->instructions
, composite
);
389 for (int i
= 0; i
< num_indexes
; ++i
)
390 spirv_buffer_emit_word(&b
->instructions
, indexes
[i
]);
395 spirv_builder_emit_composite_construct(struct spirv_builder
*b
,
397 const SpvId constituents
[],
398 size_t num_constituents
)
400 SpvId result
= spirv_builder_new_id(b
);
402 assert(num_constituents
> 0);
403 int words
= 3 + num_constituents
;
404 spirv_buffer_prepare(&b
->instructions
, words
);
405 spirv_buffer_emit_word(&b
->instructions
,
406 SpvOpCompositeConstruct
| (words
<< 16));
407 spirv_buffer_emit_word(&b
->instructions
, result_type
);
408 spirv_buffer_emit_word(&b
->instructions
, result
);
409 for (int i
= 0; i
< num_constituents
; ++i
)
410 spirv_buffer_emit_word(&b
->instructions
, constituents
[i
]);
415 spirv_builder_emit_vector_shuffle(struct spirv_builder
*b
, SpvId result_type
,
416 SpvId vector_1
, SpvId vector_2
,
417 const uint32_t components
[],
418 size_t num_components
)
420 SpvId result
= spirv_builder_new_id(b
);
422 assert(num_components
> 0);
423 int words
= 5 + num_components
;
424 spirv_buffer_prepare(&b
->instructions
, words
);
425 spirv_buffer_emit_word(&b
->instructions
, SpvOpVectorShuffle
| (words
<< 16));
426 spirv_buffer_emit_word(&b
->instructions
, result_type
);
427 spirv_buffer_emit_word(&b
->instructions
, result
);
428 spirv_buffer_emit_word(&b
->instructions
, vector_1
);
429 spirv_buffer_emit_word(&b
->instructions
, vector_2
);
430 for (int i
= 0; i
< num_components
; ++i
)
431 spirv_buffer_emit_word(&b
->instructions
, components
[i
]);
436 spirv_builder_emit_image_sample_implicit_lod(struct spirv_builder
*b
,
441 SpvId result
= spirv_builder_new_id(b
);
442 spirv_buffer_prepare(&b
->instructions
, 5);
443 spirv_buffer_emit_word(&b
->instructions
, SpvOpImageSampleImplicitLod
| (5 << 16));
444 spirv_buffer_emit_word(&b
->instructions
, result_type
);
445 spirv_buffer_emit_word(&b
->instructions
, result
);
446 spirv_buffer_emit_word(&b
->instructions
, sampled_image
);
447 spirv_buffer_emit_word(&b
->instructions
, coordinate
);
452 spirv_builder_emit_image_sample_proj_implicit_lod(struct spirv_builder
*b
,
457 SpvId result
= spirv_builder_new_id(b
);
458 spirv_buffer_prepare(&b
->instructions
, 5);
459 spirv_buffer_emit_word(&b
->instructions
, SpvOpImageSampleProjImplicitLod
| (5 << 16));
460 spirv_buffer_emit_word(&b
->instructions
, result_type
);
461 spirv_buffer_emit_word(&b
->instructions
, result
);
462 spirv_buffer_emit_word(&b
->instructions
, sampled_image
);
463 spirv_buffer_emit_word(&b
->instructions
, coordinate
);
468 spirv_builder_emit_ext_inst(struct spirv_builder
*b
, SpvId result_type
,
469 SpvId set
, uint32_t instruction
,
470 const SpvId
*args
, size_t num_args
)
472 SpvId result
= spirv_builder_new_id(b
);
474 int words
= 5 + num_args
;
475 spirv_buffer_prepare(&b
->instructions
, words
);
476 spirv_buffer_emit_word(&b
->instructions
, SpvOpExtInst
| (words
<< 16));
477 spirv_buffer_emit_word(&b
->instructions
, result_type
);
478 spirv_buffer_emit_word(&b
->instructions
, result
);
479 spirv_buffer_emit_word(&b
->instructions
, set
);
480 spirv_buffer_emit_word(&b
->instructions
, instruction
);
481 for (int i
= 0; i
< num_args
; ++i
)
482 spirv_buffer_emit_word(&b
->instructions
, args
[i
]);
495 non_aggregate_type_hash(const void *arg
)
497 const struct spirv_type
*type
= arg
;
499 uint32_t hash
= _mesa_fnv32_1a_offset_bias
;
500 hash
= _mesa_fnv32_1a_accumulate(hash
, type
->op
);
501 hash
= _mesa_fnv32_1a_accumulate_block(hash
, type
->args
, sizeof(uint32_t) *
507 non_aggregate_type_equals(const void *a
, const void *b
)
509 const struct spirv_type
*ta
= a
, *tb
= b
;
511 if (ta
->op
!= tb
->op
)
514 assert(ta
->num_args
== tb
->num_args
);
515 return memcmp(ta
->args
, tb
->args
, sizeof(uint32_t) * ta
->num_args
) == 0;
519 get_type_def(struct spirv_builder
*b
, SpvOp op
, const uint32_t args
[],
522 /* According to the SPIR-V specification:
524 * "Two different type <id>s form, by definition, two different types. It
525 * is valid to declare multiple aggregate type <id>s having the same
526 * opcode and operands. This is to allow multiple instances of aggregate
527 * types with the same structure to be decorated differently. (Different
528 * decorations are not required; two different aggregate type <id>s are
529 * allowed to have identical declarations and decorations, and will still
530 * be two different types.) Non-aggregate types are different: It is
531 * invalid to declare multiple type <id>s for the same scalar, vector, or
532 * matrix type. That is, non-aggregate type declarations must all have
533 * different opcodes or operands. (Note that non-aggregate types cannot
534 * be decorated in ways that affect their type.)"
536 * ..so, we need to prevent the same non-aggregate type to be re-defined
537 * with a new <id>. We do this by putting the definitions in a hash-map, so
538 * we can easily look up and reuse them.
541 struct spirv_type key
;
542 assert(num_args
< ARRAY_SIZE(key
.args
));
544 memcpy(&key
.args
, args
, sizeof(uint32_t) * num_args
);
545 key
.num_args
= num_args
;
547 struct hash_entry
*entry
;
549 entry
= _mesa_hash_table_search(b
->types
, &key
);
551 return ((struct spirv_type
*)entry
->data
)->type
;
553 b
->types
= _mesa_hash_table_create(NULL
, non_aggregate_type_hash
,
554 non_aggregate_type_equals
);
558 struct spirv_type
*type
= CALLOC_STRUCT(spirv_type
);
563 memcpy(&type
->args
, args
, sizeof(uint32_t) * num_args
);
564 type
->num_args
= num_args
;
566 type
->type
= spirv_builder_new_id(b
);
567 spirv_buffer_prepare(&b
->types_const_defs
, 2 + num_args
);
568 spirv_buffer_emit_word(&b
->types_const_defs
, op
| ((2 + num_args
) << 16));
569 spirv_buffer_emit_word(&b
->types_const_defs
, type
->type
);
570 for (int i
= 0; i
< num_args
; ++i
)
571 spirv_buffer_emit_word(&b
->types_const_defs
, args
[i
]);
573 entry
= _mesa_hash_table_insert(b
->types
, type
, type
);
576 return ((struct spirv_type
*)entry
->data
)->type
;
580 spirv_builder_type_void(struct spirv_builder
*b
)
582 return get_type_def(b
, SpvOpTypeVoid
, NULL
, 0);
586 spirv_builder_type_bool(struct spirv_builder
*b
)
588 return get_type_def(b
, SpvOpTypeBool
, NULL
, 0);
592 spirv_builder_type_int(struct spirv_builder
*b
, unsigned width
)
594 uint32_t args
[] = { width
, 1 };
595 return get_type_def(b
, SpvOpTypeInt
, args
, ARRAY_SIZE(args
));
599 spirv_builder_type_uint(struct spirv_builder
*b
, unsigned width
)
601 uint32_t args
[] = { width
, 0 };
602 return get_type_def(b
, SpvOpTypeInt
, args
, ARRAY_SIZE(args
));
606 spirv_builder_type_float(struct spirv_builder
*b
, unsigned width
)
608 uint32_t args
[] = { width
};
609 return get_type_def(b
, SpvOpTypeFloat
, args
, ARRAY_SIZE(args
));
613 spirv_builder_type_image(struct spirv_builder
*b
, SpvId sampled_type
,
614 SpvDim dim
, bool depth
, bool arrayed
, bool ms
,
615 unsigned sampled
, SpvImageFormat image_format
)
619 sampled_type
, dim
, depth
? 1 : 0, arrayed
? 1 : 0, ms
? 1 : 0, sampled
,
622 return get_type_def(b
, SpvOpTypeImage
, args
, ARRAY_SIZE(args
));
626 spirv_builder_type_sampled_image(struct spirv_builder
*b
, SpvId image_type
)
628 uint32_t args
[] = { image_type
};
629 return get_type_def(b
, SpvOpTypeSampledImage
, args
, ARRAY_SIZE(args
));
633 spirv_builder_type_pointer(struct spirv_builder
*b
,
634 SpvStorageClass storage_class
, SpvId type
)
636 uint32_t args
[] = { storage_class
, type
};
637 return get_type_def(b
, SpvOpTypePointer
, args
, ARRAY_SIZE(args
));
641 spirv_builder_type_vector(struct spirv_builder
*b
, SpvId component_type
,
642 unsigned component_count
)
644 assert(component_count
> 1);
645 uint32_t args
[] = { component_type
, component_count
};
646 return get_type_def(b
, SpvOpTypeVector
, args
, ARRAY_SIZE(args
));
650 spirv_builder_type_array(struct spirv_builder
*b
, SpvId component_type
,
653 SpvId type
= spirv_builder_new_id(b
);
654 spirv_buffer_prepare(&b
->types_const_defs
, 4);
655 spirv_buffer_emit_word(&b
->types_const_defs
, SpvOpTypeArray
| (4 << 16));
656 spirv_buffer_emit_word(&b
->types_const_defs
, type
);
657 spirv_buffer_emit_word(&b
->types_const_defs
, component_type
);
658 spirv_buffer_emit_word(&b
->types_const_defs
, length
);
663 spirv_builder_type_struct(struct spirv_builder
*b
, const SpvId member_types
[],
664 size_t num_member_types
)
666 int words
= 2 + num_member_types
;
667 SpvId type
= spirv_builder_new_id(b
);
668 spirv_buffer_prepare(&b
->types_const_defs
, words
);
669 spirv_buffer_emit_word(&b
->types_const_defs
, SpvOpTypeStruct
| (words
<< 16));
670 spirv_buffer_emit_word(&b
->types_const_defs
, type
);
671 for (int i
= 0; i
< num_member_types
; ++i
)
672 spirv_buffer_emit_word(&b
->types_const_defs
, member_types
[i
]);
677 spirv_builder_type_function(struct spirv_builder
*b
, SpvId return_type
,
678 const SpvId parameter_types
[],
679 size_t num_parameter_types
)
681 int words
= 3 + num_parameter_types
;
682 SpvId type
= spirv_builder_new_id(b
);
683 spirv_buffer_prepare(&b
->types_const_defs
, words
);
684 spirv_buffer_emit_word(&b
->types_const_defs
, SpvOpTypeFunction
| (words
<< 16));
685 spirv_buffer_emit_word(&b
->types_const_defs
, type
);
686 spirv_buffer_emit_word(&b
->types_const_defs
, return_type
);
687 for (int i
= 0; i
< num_parameter_types
; ++i
)
688 spirv_buffer_emit_word(&b
->types_const_defs
, parameter_types
[i
]);
693 get_const_def(struct spirv_builder
*b
, SpvOp op
, SpvId type
,
694 const uint32_t args
[], size_t num_args
)
696 /* TODO: reuse constants */
697 SpvId result
= spirv_builder_new_id(b
);
698 spirv_buffer_prepare(&b
->types_const_defs
, 3 + num_args
);
699 spirv_buffer_emit_word(&b
->types_const_defs
, op
| ((3 + num_args
) << 16));
700 spirv_buffer_emit_word(&b
->types_const_defs
, type
);
701 spirv_buffer_emit_word(&b
->types_const_defs
, result
);
702 for (int i
= 0; i
< num_args
; ++i
)
703 spirv_buffer_emit_word(&b
->types_const_defs
, args
[i
]);
708 spirv_builder_const_bool(struct spirv_builder
*b
, bool val
)
710 return get_const_def(b
, val
? SpvOpConstantTrue
: SpvOpConstantFalse
,
711 spirv_builder_type_bool(b
), NULL
, 0);
715 spirv_builder_const_int(struct spirv_builder
*b
, int width
, int32_t val
)
718 uint32_t args
[] = { val
};
719 return get_const_def(b
, SpvOpConstant
, spirv_builder_type_int(b
, width
),
720 args
, ARRAY_SIZE(args
));
724 spirv_builder_const_uint(struct spirv_builder
*b
, int width
, uint32_t val
)
727 uint32_t args
[] = { val
};
728 return get_const_def(b
, SpvOpConstant
, spirv_builder_type_uint(b
, width
),
729 args
, ARRAY_SIZE(args
));
733 spirv_builder_const_float(struct spirv_builder
*b
, int width
, float val
)
736 uint32_t args
[] = { u_bitcast_f2u(val
) };
737 return get_const_def(b
, SpvOpConstant
, spirv_builder_type_float(b
, width
),
738 args
, ARRAY_SIZE(args
));
742 spirv_builder_const_composite(struct spirv_builder
*b
, SpvId result_type
,
743 const SpvId constituents
[],
744 size_t num_constituents
)
746 return get_const_def(b
, SpvOpConstantComposite
, result_type
,
747 (const uint32_t *)constituents
,
752 spirv_builder_emit_var(struct spirv_builder
*b
, SpvId type
,
753 SpvStorageClass storage_class
)
755 assert(storage_class
!= SpvStorageClassGeneric
);
756 struct spirv_buffer
*buf
= storage_class
!= SpvStorageClassFunction
?
757 &b
->types_const_defs
: &b
->instructions
;
759 SpvId ret
= spirv_builder_new_id(b
);
760 spirv_buffer_prepare(buf
, 4);
761 spirv_buffer_emit_word(buf
, SpvOpVariable
| (4 << 16));
762 spirv_buffer_emit_word(buf
, type
);
763 spirv_buffer_emit_word(buf
, ret
);
764 spirv_buffer_emit_word(buf
, storage_class
);
769 spirv_builder_import(struct spirv_builder
*b
, const char *name
)
771 SpvId result
= spirv_builder_new_id(b
);
772 size_t pos
= b
->imports
.num_words
;
773 spirv_buffer_prepare(&b
->imports
, 2);
774 spirv_buffer_emit_word(&b
->imports
, SpvOpExtInstImport
);
775 spirv_buffer_emit_word(&b
->imports
, result
);
776 int len
= spirv_buffer_emit_string(&b
->imports
, name
);
777 b
->imports
.words
[pos
] |= (2 + len
) << 16;
782 spirv_builder_get_num_words(struct spirv_builder
*b
)
784 const size_t header_size
= 5;
786 b
->capabilities
.num_words
+
787 b
->imports
.num_words
+
788 b
->memory_model
.num_words
+
789 b
->entry_points
.num_words
+
790 b
->exec_modes
.num_words
+
791 b
->debug_names
.num_words
+
792 b
->decorations
.num_words
+
793 b
->types_const_defs
.num_words
+
794 b
->instructions
.num_words
;
798 spirv_builder_get_words(struct spirv_builder
*b
, uint32_t *words
,
801 assert(num_words
>= spirv_builder_get_num_words(b
));
804 words
[written
++] = SpvMagicNumber
;
805 words
[written
++] = 0x00010000;
806 words
[written
++] = 0;
807 words
[written
++] = b
->prev_id
+ 1;
808 words
[written
++] = 0;
810 const struct spirv_buffer
*buffers
[] = {
818 &b
->types_const_defs
,
822 for (int i
= 0; i
< ARRAY_SIZE(buffers
); ++i
) {
823 const struct spirv_buffer
*buffer
= buffers
[i
];
824 for (int j
= 0; j
< buffer
->num_words
; ++j
)
825 words
[written
++] = buffer
->words
[j
];
828 assert(written
== spirv_builder_get_num_words(b
));