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"
30 #define XXH_INLINE_ALL
31 #include "util/xxhash.h"
38 spirv_buffer_grow(struct spirv_buffer
*b
, size_t needed
)
40 size_t new_room
= MAX3(64, (b
->room
* 3) / 2, needed
);
42 uint32_t *new_words
= realloc(b
->words
, new_room
* sizeof(uint32_t));
52 spirv_buffer_prepare(struct spirv_buffer
*b
, size_t needed
)
54 needed
+= b
->num_words
;
55 if (b
->room
>= b
->num_words
+ needed
)
58 return spirv_buffer_grow(b
, needed
);
62 spirv_buffer_emit_word(struct spirv_buffer
*b
, uint32_t word
)
64 assert(b
->num_words
< b
->room
);
65 b
->words
[b
->num_words
++] = word
;
69 spirv_buffer_emit_string(struct spirv_buffer
*b
, const char *str
)
73 while (str
[pos
] != '\0') {
74 word
|= str
[pos
] << (8 * (pos
% 4));
76 spirv_buffer_prepare(b
, 1);
77 spirv_buffer_emit_word(b
, word
);
82 spirv_buffer_prepare(b
, 1);
83 spirv_buffer_emit_word(b
, word
);
89 spirv_builder_emit_cap(struct spirv_builder
*b
, SpvCapability cap
)
91 spirv_buffer_prepare(&b
->capabilities
, 2);
92 spirv_buffer_emit_word(&b
->capabilities
, SpvOpCapability
| (2 << 16));
93 spirv_buffer_emit_word(&b
->capabilities
, cap
);
97 spirv_builder_emit_source(struct spirv_builder
*b
, SpvSourceLanguage lang
,
100 spirv_buffer_prepare(&b
->debug_names
, 3);
101 spirv_buffer_emit_word(&b
->debug_names
, SpvOpSource
| (3 << 16));
102 spirv_buffer_emit_word(&b
->debug_names
, lang
);
103 spirv_buffer_emit_word(&b
->debug_names
, version
);
107 spirv_builder_emit_mem_model(struct spirv_builder
*b
,
108 SpvAddressingModel addr_model
,
109 SpvMemoryModel mem_model
)
111 spirv_buffer_prepare(&b
->memory_model
, 3);
112 spirv_buffer_emit_word(&b
->memory_model
, SpvOpMemoryModel
| (3 << 16));
113 spirv_buffer_emit_word(&b
->memory_model
, addr_model
);
114 spirv_buffer_emit_word(&b
->memory_model
, mem_model
);
118 spirv_builder_emit_entry_point(struct spirv_builder
*b
,
119 SpvExecutionModel exec_model
, SpvId entry_point
,
120 const char *name
, const SpvId interfaces
[],
121 size_t num_interfaces
)
123 size_t pos
= b
->entry_points
.num_words
;
124 spirv_buffer_prepare(&b
->entry_points
, 3);
125 spirv_buffer_emit_word(&b
->entry_points
, SpvOpEntryPoint
);
126 spirv_buffer_emit_word(&b
->entry_points
, exec_model
);
127 spirv_buffer_emit_word(&b
->entry_points
, entry_point
);
128 int len
= spirv_buffer_emit_string(&b
->entry_points
, name
);
129 b
->entry_points
.words
[pos
] |= (3 + len
+ num_interfaces
) << 16;
130 spirv_buffer_prepare(&b
->entry_points
, num_interfaces
);
131 for (int i
= 0; i
< num_interfaces
; ++i
)
132 spirv_buffer_emit_word(&b
->entry_points
, interfaces
[i
]);
136 spirv_builder_emit_exec_mode(struct spirv_builder
*b
, SpvId entry_point
,
137 SpvExecutionMode exec_mode
)
139 spirv_buffer_prepare(&b
->exec_modes
, 3);
140 spirv_buffer_emit_word(&b
->exec_modes
, SpvOpExecutionMode
| (3 << 16));
141 spirv_buffer_emit_word(&b
->exec_modes
, entry_point
);
142 spirv_buffer_emit_word(&b
->exec_modes
, exec_mode
);
146 spirv_builder_emit_name(struct spirv_builder
*b
, SpvId target
,
149 size_t pos
= b
->debug_names
.num_words
;
150 spirv_buffer_prepare(&b
->debug_names
, 2);
151 spirv_buffer_emit_word(&b
->debug_names
, SpvOpName
);
152 spirv_buffer_emit_word(&b
->debug_names
, target
);
153 int len
= spirv_buffer_emit_string(&b
->debug_names
, name
);
154 b
->debug_names
.words
[pos
] |= (2 + len
) << 16;
158 emit_decoration(struct spirv_builder
*b
, SpvId target
,
159 SpvDecoration decoration
, const uint32_t extra_operands
[],
160 size_t num_extra_operands
)
162 int words
= 3 + num_extra_operands
;
163 spirv_buffer_prepare(&b
->decorations
, words
);
164 spirv_buffer_emit_word(&b
->decorations
, SpvOpDecorate
| (words
<< 16));
165 spirv_buffer_emit_word(&b
->decorations
, target
);
166 spirv_buffer_emit_word(&b
->decorations
, decoration
);
167 for (int i
= 0; i
< num_extra_operands
; ++i
)
168 spirv_buffer_emit_word(&b
->decorations
, extra_operands
[i
]);
172 spirv_builder_emit_decoration(struct spirv_builder
*b
, SpvId target
,
173 SpvDecoration decoration
)
175 emit_decoration(b
, target
, decoration
, NULL
, 0);
179 spirv_builder_emit_location(struct spirv_builder
*b
, SpvId target
,
182 uint32_t args
[] = { location
};
183 emit_decoration(b
, target
, SpvDecorationLocation
, args
, ARRAY_SIZE(args
));
187 spirv_builder_emit_component(struct spirv_builder
*b
, SpvId target
,
190 uint32_t args
[] = { component
};
191 emit_decoration(b
, target
, SpvDecorationComponent
, args
, ARRAY_SIZE(args
));
195 spirv_builder_emit_builtin(struct spirv_builder
*b
, SpvId target
,
198 uint32_t args
[] = { builtin
};
199 emit_decoration(b
, target
, SpvDecorationBuiltIn
, args
, ARRAY_SIZE(args
));
203 spirv_builder_emit_descriptor_set(struct spirv_builder
*b
, SpvId target
,
204 uint32_t descriptor_set
)
206 uint32_t args
[] = { descriptor_set
};
207 emit_decoration(b
, target
, SpvDecorationDescriptorSet
, args
,
212 spirv_builder_emit_binding(struct spirv_builder
*b
, SpvId target
,
215 uint32_t args
[] = { binding
};
216 emit_decoration(b
, target
, SpvDecorationBinding
, args
, ARRAY_SIZE(args
));
220 spirv_builder_emit_array_stride(struct spirv_builder
*b
, SpvId target
,
223 uint32_t args
[] = { stride
};
224 emit_decoration(b
, target
, SpvDecorationArrayStride
, args
, ARRAY_SIZE(args
));
228 spirv_builder_emit_offset(struct spirv_builder
*b
, SpvId target
,
231 uint32_t args
[] = { offset
};
232 emit_decoration(b
, target
, SpvDecorationOffset
, args
, ARRAY_SIZE(args
));
236 spirv_builder_emit_xfb_buffer(struct spirv_builder
*b
, SpvId target
,
239 uint32_t args
[] = { buffer
};
240 emit_decoration(b
, target
, SpvDecorationXfbBuffer
, args
, ARRAY_SIZE(args
));
244 spirv_builder_emit_xfb_stride(struct spirv_builder
*b
, SpvId target
,
247 uint32_t args
[] = { stride
};
248 emit_decoration(b
, target
, SpvDecorationXfbStride
, args
, ARRAY_SIZE(args
));
252 spirv_builder_emit_index(struct spirv_builder
*b
, SpvId target
, int index
)
254 uint32_t args
[] = { index
};
255 emit_decoration(b
, target
, SpvDecorationIndex
, args
, ARRAY_SIZE(args
));
259 emit_member_decoration(struct spirv_builder
*b
, SpvId target
, uint32_t member
,
260 SpvDecoration decoration
, const uint32_t extra_operands
[],
261 size_t num_extra_operands
)
263 int words
= 4 + num_extra_operands
;
264 spirv_buffer_prepare(&b
->decorations
, words
);
265 spirv_buffer_emit_word(&b
->decorations
,
266 SpvOpMemberDecorate
| (words
<< 16));
267 spirv_buffer_emit_word(&b
->decorations
, target
);
268 spirv_buffer_emit_word(&b
->decorations
, member
);
269 spirv_buffer_emit_word(&b
->decorations
, decoration
);
270 for (int i
= 0; i
< num_extra_operands
; ++i
)
271 spirv_buffer_emit_word(&b
->decorations
, extra_operands
[i
]);
275 spirv_builder_emit_member_offset(struct spirv_builder
*b
, SpvId target
,
276 uint32_t member
, uint32_t offset
)
278 uint32_t args
[] = { offset
};
279 emit_member_decoration(b
, target
, member
, SpvDecorationOffset
,
280 args
, ARRAY_SIZE(args
));
284 spirv_builder_emit_undef(struct spirv_builder
*b
, SpvId result_type
)
286 SpvId result
= spirv_builder_new_id(b
);
287 spirv_buffer_prepare(&b
->instructions
, 3);
288 spirv_buffer_emit_word(&b
->instructions
, SpvOpUndef
| (3 << 16));
289 spirv_buffer_emit_word(&b
->instructions
, result_type
);
290 spirv_buffer_emit_word(&b
->instructions
, result
);
295 spirv_builder_function(struct spirv_builder
*b
, SpvId result
,
297 SpvFunctionControlMask function_control
,
300 spirv_buffer_prepare(&b
->instructions
, 5);
301 spirv_buffer_emit_word(&b
->instructions
, SpvOpFunction
| (5 << 16));
302 spirv_buffer_emit_word(&b
->instructions
, return_type
);
303 spirv_buffer_emit_word(&b
->instructions
, result
);
304 spirv_buffer_emit_word(&b
->instructions
, function_control
);
305 spirv_buffer_emit_word(&b
->instructions
, function_type
);
309 spirv_builder_function_end(struct spirv_builder
*b
)
311 spirv_buffer_prepare(&b
->instructions
, 1);
312 spirv_buffer_emit_word(&b
->instructions
, SpvOpFunctionEnd
| (1 << 16));
316 spirv_builder_label(struct spirv_builder
*b
, SpvId label
)
318 spirv_buffer_prepare(&b
->instructions
, 2);
319 spirv_buffer_emit_word(&b
->instructions
, SpvOpLabel
| (2 << 16));
320 spirv_buffer_emit_word(&b
->instructions
, label
);
324 spirv_builder_return(struct spirv_builder
*b
)
326 spirv_buffer_prepare(&b
->instructions
, 1);
327 spirv_buffer_emit_word(&b
->instructions
, SpvOpReturn
| (1 << 16));
331 spirv_builder_emit_load(struct spirv_builder
*b
, SpvId result_type
,
334 return spirv_builder_emit_unop(b
, SpvOpLoad
, result_type
, pointer
);
338 spirv_builder_emit_store(struct spirv_builder
*b
, SpvId pointer
, SpvId object
)
340 spirv_buffer_prepare(&b
->instructions
, 3);
341 spirv_buffer_emit_word(&b
->instructions
, SpvOpStore
| (3 << 16));
342 spirv_buffer_emit_word(&b
->instructions
, pointer
);
343 spirv_buffer_emit_word(&b
->instructions
, object
);
347 spirv_builder_emit_access_chain(struct spirv_builder
*b
, SpvId result_type
,
348 SpvId base
, const SpvId indexes
[],
351 SpvId result
= spirv_builder_new_id(b
);
353 int words
= 4 + num_indexes
;
354 spirv_buffer_prepare(&b
->instructions
, words
);
355 spirv_buffer_emit_word(&b
->instructions
, SpvOpAccessChain
| (words
<< 16));
356 spirv_buffer_emit_word(&b
->instructions
, result_type
);
357 spirv_buffer_emit_word(&b
->instructions
, result
);
358 spirv_buffer_emit_word(&b
->instructions
, base
);
359 for (int i
= 0; i
< num_indexes
; ++i
)
360 spirv_buffer_emit_word(&b
->instructions
, indexes
[i
]);
366 spirv_builder_emit_unop(struct spirv_builder
*b
, SpvOp op
, SpvId result_type
,
369 SpvId result
= spirv_builder_new_id(b
);
370 spirv_buffer_prepare(&b
->instructions
, 4);
371 spirv_buffer_emit_word(&b
->instructions
, op
| (4 << 16));
372 spirv_buffer_emit_word(&b
->instructions
, result_type
);
373 spirv_buffer_emit_word(&b
->instructions
, result
);
374 spirv_buffer_emit_word(&b
->instructions
, operand
);
379 spirv_builder_emit_binop(struct spirv_builder
*b
, SpvOp op
, SpvId result_type
,
380 SpvId operand0
, SpvId operand1
)
382 SpvId result
= spirv_builder_new_id(b
);
383 spirv_buffer_prepare(&b
->instructions
, 5);
384 spirv_buffer_emit_word(&b
->instructions
, op
| (5 << 16));
385 spirv_buffer_emit_word(&b
->instructions
, result_type
);
386 spirv_buffer_emit_word(&b
->instructions
, result
);
387 spirv_buffer_emit_word(&b
->instructions
, operand0
);
388 spirv_buffer_emit_word(&b
->instructions
, operand1
);
393 spirv_builder_emit_triop(struct spirv_builder
*b
, SpvOp op
, SpvId result_type
,
394 SpvId operand0
, SpvId operand1
, SpvId operand2
)
396 SpvId result
= spirv_builder_new_id(b
);
397 spirv_buffer_prepare(&b
->instructions
, 6);
398 spirv_buffer_emit_word(&b
->instructions
, op
| (6 << 16));
399 spirv_buffer_emit_word(&b
->instructions
, result_type
);
400 spirv_buffer_emit_word(&b
->instructions
, result
);
401 spirv_buffer_emit_word(&b
->instructions
, operand0
);
402 spirv_buffer_emit_word(&b
->instructions
, operand1
);
403 spirv_buffer_emit_word(&b
->instructions
, operand2
);
408 spirv_builder_emit_composite_extract(struct spirv_builder
*b
, SpvId result_type
,
409 SpvId composite
, const uint32_t indexes
[],
412 SpvId result
= spirv_builder_new_id(b
);
414 assert(num_indexes
> 0);
415 int words
= 4 + num_indexes
;
416 spirv_buffer_prepare(&b
->instructions
, words
);
417 spirv_buffer_emit_word(&b
->instructions
,
418 SpvOpCompositeExtract
| (words
<< 16));
419 spirv_buffer_emit_word(&b
->instructions
, result_type
);
420 spirv_buffer_emit_word(&b
->instructions
, result
);
421 spirv_buffer_emit_word(&b
->instructions
, composite
);
422 for (int i
= 0; i
< num_indexes
; ++i
)
423 spirv_buffer_emit_word(&b
->instructions
, indexes
[i
]);
428 spirv_builder_emit_composite_construct(struct spirv_builder
*b
,
430 const SpvId constituents
[],
431 size_t num_constituents
)
433 SpvId result
= spirv_builder_new_id(b
);
435 assert(num_constituents
> 0);
436 int words
= 3 + num_constituents
;
437 spirv_buffer_prepare(&b
->instructions
, words
);
438 spirv_buffer_emit_word(&b
->instructions
,
439 SpvOpCompositeConstruct
| (words
<< 16));
440 spirv_buffer_emit_word(&b
->instructions
, result_type
);
441 spirv_buffer_emit_word(&b
->instructions
, result
);
442 for (int i
= 0; i
< num_constituents
; ++i
)
443 spirv_buffer_emit_word(&b
->instructions
, constituents
[i
]);
448 spirv_builder_emit_vector_shuffle(struct spirv_builder
*b
, SpvId result_type
,
449 SpvId vector_1
, SpvId vector_2
,
450 const uint32_t components
[],
451 size_t num_components
)
453 SpvId result
= spirv_builder_new_id(b
);
455 assert(num_components
> 0);
456 int words
= 5 + num_components
;
457 spirv_buffer_prepare(&b
->instructions
, words
);
458 spirv_buffer_emit_word(&b
->instructions
, SpvOpVectorShuffle
| (words
<< 16));
459 spirv_buffer_emit_word(&b
->instructions
, result_type
);
460 spirv_buffer_emit_word(&b
->instructions
, result
);
461 spirv_buffer_emit_word(&b
->instructions
, vector_1
);
462 spirv_buffer_emit_word(&b
->instructions
, vector_2
);
463 for (int i
= 0; i
< num_components
; ++i
)
464 spirv_buffer_emit_word(&b
->instructions
, components
[i
]);
469 spirv_builder_emit_vector_extract(struct spirv_builder
*b
, SpvId result_type
,
473 SpvId result
= spirv_builder_new_id(b
);
476 spirv_buffer_prepare(&b
->instructions
, words
);
477 spirv_buffer_emit_word(&b
->instructions
, SpvOpVectorExtractDynamic
| (words
<< 16));
478 spirv_buffer_emit_word(&b
->instructions
, result_type
);
479 spirv_buffer_emit_word(&b
->instructions
, result
);
480 spirv_buffer_emit_word(&b
->instructions
, vector_1
);
481 spirv_buffer_emit_word(&b
->instructions
, spirv_builder_const_uint(b
, 32, component
));
486 spirv_builder_emit_vector_insert(struct spirv_builder
*b
, SpvId result_type
,
491 SpvId result
= spirv_builder_new_id(b
);
494 spirv_buffer_prepare(&b
->instructions
, words
);
495 spirv_buffer_emit_word(&b
->instructions
, SpvOpVectorInsertDynamic
| (words
<< 16));
496 spirv_buffer_emit_word(&b
->instructions
, result_type
);
497 spirv_buffer_emit_word(&b
->instructions
, result
);
498 spirv_buffer_emit_word(&b
->instructions
, vector_1
);
499 spirv_buffer_emit_word(&b
->instructions
, component
);
500 spirv_buffer_emit_word(&b
->instructions
, spirv_builder_const_uint(b
, 32, index
));
505 spirv_builder_emit_branch(struct spirv_builder
*b
, SpvId label
)
507 spirv_buffer_prepare(&b
->instructions
, 2);
508 spirv_buffer_emit_word(&b
->instructions
, SpvOpBranch
| (2 << 16));
509 spirv_buffer_emit_word(&b
->instructions
, label
);
513 spirv_builder_emit_selection_merge(struct spirv_builder
*b
, SpvId merge_block
,
514 SpvSelectionControlMask selection_control
)
516 spirv_buffer_prepare(&b
->instructions
, 3);
517 spirv_buffer_emit_word(&b
->instructions
, SpvOpSelectionMerge
| (3 << 16));
518 spirv_buffer_emit_word(&b
->instructions
, merge_block
);
519 spirv_buffer_emit_word(&b
->instructions
, selection_control
);
523 spirv_builder_loop_merge(struct spirv_builder
*b
, SpvId merge_block
,
524 SpvId cont_target
, SpvLoopControlMask loop_control
)
526 spirv_buffer_prepare(&b
->instructions
, 4);
527 spirv_buffer_emit_word(&b
->instructions
, SpvOpLoopMerge
| (4 << 16));
528 spirv_buffer_emit_word(&b
->instructions
, merge_block
);
529 spirv_buffer_emit_word(&b
->instructions
, cont_target
);
530 spirv_buffer_emit_word(&b
->instructions
, loop_control
);
534 spirv_builder_emit_branch_conditional(struct spirv_builder
*b
, SpvId condition
,
535 SpvId true_label
, SpvId false_label
)
537 spirv_buffer_prepare(&b
->instructions
, 4);
538 spirv_buffer_emit_word(&b
->instructions
, SpvOpBranchConditional
| (4 << 16));
539 spirv_buffer_emit_word(&b
->instructions
, condition
);
540 spirv_buffer_emit_word(&b
->instructions
, true_label
);
541 spirv_buffer_emit_word(&b
->instructions
, false_label
);
545 spirv_builder_emit_phi(struct spirv_builder
*b
, SpvId result_type
,
546 size_t num_vars
, size_t *position
)
548 SpvId result
= spirv_builder_new_id(b
);
550 assert(num_vars
> 0);
551 int words
= 3 + 2 * num_vars
;
552 spirv_buffer_prepare(&b
->instructions
, words
);
553 spirv_buffer_emit_word(&b
->instructions
, SpvOpPhi
| (words
<< 16));
554 spirv_buffer_emit_word(&b
->instructions
, result_type
);
555 spirv_buffer_emit_word(&b
->instructions
, result
);
556 *position
= b
->instructions
.num_words
;
557 for (int i
= 0; i
< 2 * num_vars
; ++i
)
558 spirv_buffer_emit_word(&b
->instructions
, 0);
563 spirv_builder_set_phi_operand(struct spirv_builder
*b
, size_t position
,
564 size_t index
, SpvId variable
, SpvId parent
)
566 b
->instructions
.words
[position
+ index
* 2 + 0] = variable
;
567 b
->instructions
.words
[position
+ index
* 2 + 1] = parent
;
571 spirv_builder_emit_kill(struct spirv_builder
*b
)
573 spirv_buffer_prepare(&b
->instructions
, 1);
574 spirv_buffer_emit_word(&b
->instructions
, SpvOpKill
| (1 << 16));
578 spirv_builder_emit_image_sample(struct spirv_builder
*b
,
590 SpvId result
= spirv_builder_new_id(b
);
592 int opcode
= SpvOpImageSampleImplicitLod
;
595 opcode
+= SpvOpImageSampleProjImplicitLod
- SpvOpImageSampleImplicitLod
;
596 if (lod
|| (dx
&& dy
))
597 opcode
+= SpvOpImageSampleExplicitLod
- SpvOpImageSampleImplicitLod
;
599 opcode
+= SpvOpImageSampleDrefImplicitLod
- SpvOpImageSampleImplicitLod
;
603 SpvImageOperandsMask operand_mask
= SpvImageOperandsMaskNone
;
604 SpvId extra_operands
[5];
605 int num_extra_operands
= 0;
607 extra_operands
[++num_extra_operands
] = bias
;
608 operand_mask
|= SpvImageOperandsBiasMask
;
611 extra_operands
[++num_extra_operands
] = lod
;
612 operand_mask
|= SpvImageOperandsLodMask
;
613 } else if (dx
&& dy
) {
614 extra_operands
[++num_extra_operands
] = dx
;
615 extra_operands
[++num_extra_operands
] = dy
;
616 operand_mask
|= SpvImageOperandsGradMask
;
619 extra_operands
[++num_extra_operands
] = offset
;
620 operand_mask
|= SpvImageOperandsOffsetMask
;
623 /* finalize num_extra_operands / extra_operands */
624 if (num_extra_operands
> 0) {
625 extra_operands
[0] = operand_mask
;
626 num_extra_operands
++;
629 spirv_buffer_prepare(&b
->instructions
, operands
+ num_extra_operands
);
630 spirv_buffer_emit_word(&b
->instructions
, opcode
| ((operands
+ num_extra_operands
) << 16));
631 spirv_buffer_emit_word(&b
->instructions
, result_type
);
632 spirv_buffer_emit_word(&b
->instructions
, result
);
633 spirv_buffer_emit_word(&b
->instructions
, sampled_image
);
634 spirv_buffer_emit_word(&b
->instructions
, coordinate
);
636 spirv_buffer_emit_word(&b
->instructions
, dref
);
637 for (int i
= 0; i
< num_extra_operands
; ++i
)
638 spirv_buffer_emit_word(&b
->instructions
, extra_operands
[i
]);
643 spirv_builder_emit_image(struct spirv_builder
*b
, SpvId result_type
,
646 SpvId result
= spirv_builder_new_id(b
);
647 spirv_buffer_prepare(&b
->instructions
, 4);
648 spirv_buffer_emit_word(&b
->instructions
, SpvOpImage
| (4 << 16));
649 spirv_buffer_emit_word(&b
->instructions
, result_type
);
650 spirv_buffer_emit_word(&b
->instructions
, result
);
651 spirv_buffer_emit_word(&b
->instructions
, sampled_image
);
656 spirv_builder_emit_image_fetch(struct spirv_builder
*b
,
663 SpvId result
= spirv_builder_new_id(b
);
665 SpvImageOperandsMask operand_mask
= SpvImageOperandsMaskNone
;
666 SpvId extra_operands
[3];
667 int num_extra_operands
= 0;
669 extra_operands
[++num_extra_operands
] = lod
;
670 operand_mask
|= SpvImageOperandsLodMask
;
673 extra_operands
[++num_extra_operands
] = sample
;
674 operand_mask
|= SpvImageOperandsSampleMask
;
677 /* finalize num_extra_operands / extra_operands */
678 if (num_extra_operands
> 0) {
679 extra_operands
[0] = operand_mask
;
680 num_extra_operands
++;
683 spirv_buffer_prepare(&b
->instructions
, 5 + num_extra_operands
);
684 spirv_buffer_emit_word(&b
->instructions
, SpvOpImageFetch
|
685 ((5 + num_extra_operands
) << 16));
686 spirv_buffer_emit_word(&b
->instructions
, result_type
);
687 spirv_buffer_emit_word(&b
->instructions
, result
);
688 spirv_buffer_emit_word(&b
->instructions
, image
);
689 spirv_buffer_emit_word(&b
->instructions
, coordinate
);
690 for (int i
= 0; i
< num_extra_operands
; ++i
)
691 spirv_buffer_emit_word(&b
->instructions
, extra_operands
[i
]);
696 spirv_builder_emit_image_query_size(struct spirv_builder
*b
,
701 int opcode
= SpvOpImageQuerySize
;
705 opcode
= SpvOpImageQuerySizeLod
;
708 SpvId result
= spirv_builder_new_id(b
);
709 spirv_buffer_prepare(&b
->instructions
, words
);
710 spirv_buffer_emit_word(&b
->instructions
, opcode
| (words
<< 16));
711 spirv_buffer_emit_word(&b
->instructions
, result_type
);
712 spirv_buffer_emit_word(&b
->instructions
, result
);
713 spirv_buffer_emit_word(&b
->instructions
, image
);
716 spirv_buffer_emit_word(&b
->instructions
, lod
);
722 spirv_builder_emit_ext_inst(struct spirv_builder
*b
, SpvId result_type
,
723 SpvId set
, uint32_t instruction
,
724 const SpvId
*args
, size_t num_args
)
726 SpvId result
= spirv_builder_new_id(b
);
728 int words
= 5 + num_args
;
729 spirv_buffer_prepare(&b
->instructions
, words
);
730 spirv_buffer_emit_word(&b
->instructions
, SpvOpExtInst
| (words
<< 16));
731 spirv_buffer_emit_word(&b
->instructions
, result_type
);
732 spirv_buffer_emit_word(&b
->instructions
, result
);
733 spirv_buffer_emit_word(&b
->instructions
, set
);
734 spirv_buffer_emit_word(&b
->instructions
, instruction
);
735 for (int i
= 0; i
< num_args
; ++i
)
736 spirv_buffer_emit_word(&b
->instructions
, args
[i
]);
749 non_aggregate_type_hash(const void *arg
)
751 const struct spirv_type
*type
= arg
;
754 hash
= XXH32(&type
->op
, sizeof(type
->op
), hash
);
755 hash
= XXH32(type
->args
, sizeof(uint32_t) * type
->num_args
, hash
);
760 non_aggregate_type_equals(const void *a
, const void *b
)
762 const struct spirv_type
*ta
= a
, *tb
= b
;
764 if (ta
->op
!= tb
->op
)
767 assert(ta
->num_args
== tb
->num_args
);
768 return memcmp(ta
->args
, tb
->args
, sizeof(uint32_t) * ta
->num_args
) == 0;
772 get_type_def(struct spirv_builder
*b
, SpvOp op
, const uint32_t args
[],
775 /* According to the SPIR-V specification:
777 * "Two different type <id>s form, by definition, two different types. It
778 * is valid to declare multiple aggregate type <id>s having the same
779 * opcode and operands. This is to allow multiple instances of aggregate
780 * types with the same structure to be decorated differently. (Different
781 * decorations are not required; two different aggregate type <id>s are
782 * allowed to have identical declarations and decorations, and will still
783 * be two different types.) Non-aggregate types are different: It is
784 * invalid to declare multiple type <id>s for the same scalar, vector, or
785 * matrix type. That is, non-aggregate type declarations must all have
786 * different opcodes or operands. (Note that non-aggregate types cannot
787 * be decorated in ways that affect their type.)"
789 * ..so, we need to prevent the same non-aggregate type to be re-defined
790 * with a new <id>. We do this by putting the definitions in a hash-map, so
791 * we can easily look up and reuse them.
794 struct spirv_type key
;
795 assert(num_args
<= ARRAY_SIZE(key
.args
));
797 memcpy(&key
.args
, args
, sizeof(uint32_t) * num_args
);
798 key
.num_args
= num_args
;
800 struct hash_entry
*entry
;
802 entry
= _mesa_hash_table_search(b
->types
, &key
);
804 return ((struct spirv_type
*)entry
->data
)->type
;
806 b
->types
= _mesa_hash_table_create(NULL
, non_aggregate_type_hash
,
807 non_aggregate_type_equals
);
811 struct spirv_type
*type
= CALLOC_STRUCT(spirv_type
);
816 memcpy(&type
->args
, args
, sizeof(uint32_t) * num_args
);
817 type
->num_args
= num_args
;
819 type
->type
= spirv_builder_new_id(b
);
820 spirv_buffer_prepare(&b
->types_const_defs
, 2 + num_args
);
821 spirv_buffer_emit_word(&b
->types_const_defs
, op
| ((2 + num_args
) << 16));
822 spirv_buffer_emit_word(&b
->types_const_defs
, type
->type
);
823 for (int i
= 0; i
< num_args
; ++i
)
824 spirv_buffer_emit_word(&b
->types_const_defs
, args
[i
]);
826 entry
= _mesa_hash_table_insert(b
->types
, type
, type
);
829 return ((struct spirv_type
*)entry
->data
)->type
;
833 spirv_builder_type_void(struct spirv_builder
*b
)
835 return get_type_def(b
, SpvOpTypeVoid
, NULL
, 0);
839 spirv_builder_type_bool(struct spirv_builder
*b
)
841 return get_type_def(b
, SpvOpTypeBool
, NULL
, 0);
845 spirv_builder_type_int(struct spirv_builder
*b
, unsigned width
)
847 uint32_t args
[] = { width
, 1 };
848 return get_type_def(b
, SpvOpTypeInt
, args
, ARRAY_SIZE(args
));
852 spirv_builder_type_uint(struct spirv_builder
*b
, unsigned width
)
854 uint32_t args
[] = { width
, 0 };
855 return get_type_def(b
, SpvOpTypeInt
, args
, ARRAY_SIZE(args
));
859 spirv_builder_type_float(struct spirv_builder
*b
, unsigned width
)
861 uint32_t args
[] = { width
};
862 return get_type_def(b
, SpvOpTypeFloat
, args
, ARRAY_SIZE(args
));
866 spirv_builder_type_image(struct spirv_builder
*b
, SpvId sampled_type
,
867 SpvDim dim
, bool depth
, bool arrayed
, bool ms
,
868 unsigned sampled
, SpvImageFormat image_format
)
872 sampled_type
, dim
, depth
? 1 : 0, arrayed
? 1 : 0, ms
? 1 : 0, sampled
,
875 return get_type_def(b
, SpvOpTypeImage
, args
, ARRAY_SIZE(args
));
879 spirv_builder_type_sampled_image(struct spirv_builder
*b
, SpvId image_type
)
881 uint32_t args
[] = { image_type
};
882 return get_type_def(b
, SpvOpTypeSampledImage
, args
, ARRAY_SIZE(args
));
886 spirv_builder_type_pointer(struct spirv_builder
*b
,
887 SpvStorageClass storage_class
, SpvId type
)
889 uint32_t args
[] = { storage_class
, type
};
890 return get_type_def(b
, SpvOpTypePointer
, args
, ARRAY_SIZE(args
));
894 spirv_builder_type_vector(struct spirv_builder
*b
, SpvId component_type
,
895 unsigned component_count
)
897 assert(component_count
> 1);
898 uint32_t args
[] = { component_type
, component_count
};
899 return get_type_def(b
, SpvOpTypeVector
, args
, ARRAY_SIZE(args
));
903 spirv_builder_type_array(struct spirv_builder
*b
, SpvId component_type
,
906 SpvId type
= spirv_builder_new_id(b
);
907 spirv_buffer_prepare(&b
->types_const_defs
, 4);
908 spirv_buffer_emit_word(&b
->types_const_defs
, SpvOpTypeArray
| (4 << 16));
909 spirv_buffer_emit_word(&b
->types_const_defs
, type
);
910 spirv_buffer_emit_word(&b
->types_const_defs
, component_type
);
911 spirv_buffer_emit_word(&b
->types_const_defs
, length
);
916 spirv_builder_type_struct(struct spirv_builder
*b
, const SpvId member_types
[],
917 size_t num_member_types
)
919 int words
= 2 + num_member_types
;
920 SpvId type
= spirv_builder_new_id(b
);
921 spirv_buffer_prepare(&b
->types_const_defs
, words
);
922 spirv_buffer_emit_word(&b
->types_const_defs
, SpvOpTypeStruct
| (words
<< 16));
923 spirv_buffer_emit_word(&b
->types_const_defs
, type
);
924 for (int i
= 0; i
< num_member_types
; ++i
)
925 spirv_buffer_emit_word(&b
->types_const_defs
, member_types
[i
]);
930 spirv_builder_type_function(struct spirv_builder
*b
, SpvId return_type
,
931 const SpvId parameter_types
[],
932 size_t num_parameter_types
)
934 int words
= 3 + num_parameter_types
;
935 SpvId type
= spirv_builder_new_id(b
);
936 spirv_buffer_prepare(&b
->types_const_defs
, words
);
937 spirv_buffer_emit_word(&b
->types_const_defs
, SpvOpTypeFunction
| (words
<< 16));
938 spirv_buffer_emit_word(&b
->types_const_defs
, type
);
939 spirv_buffer_emit_word(&b
->types_const_defs
, return_type
);
940 for (int i
= 0; i
< num_parameter_types
; ++i
)
941 spirv_buffer_emit_word(&b
->types_const_defs
, parameter_types
[i
]);
954 const_hash(const void *arg
)
956 const struct spirv_const
*key
= arg
;
959 hash
= XXH32(&key
->op
, sizeof(key
->op
), hash
);
960 hash
= XXH32(&key
->type
, sizeof(key
->type
), hash
);
961 hash
= XXH32(key
->args
, sizeof(uint32_t) * key
->num_args
, hash
);
966 const_equals(const void *a
, const void *b
)
968 const struct spirv_const
*ca
= a
, *cb
= b
;
970 if (ca
->op
!= cb
->op
||
971 ca
->type
!= cb
->type
)
974 assert(ca
->num_args
== cb
->num_args
);
975 return memcmp(ca
->args
, cb
->args
, sizeof(uint32_t) * ca
->num_args
) == 0;
979 get_const_def(struct spirv_builder
*b
, SpvOp op
, SpvId type
,
980 const uint32_t args
[], size_t num_args
)
982 struct spirv_const key
;
983 assert(num_args
<= ARRAY_SIZE(key
.args
));
986 memcpy(&key
.args
, args
, sizeof(uint32_t) * num_args
);
987 key
.num_args
= num_args
;
989 struct hash_entry
*entry
;
991 entry
= _mesa_hash_table_search(b
->consts
, &key
);
993 return ((struct spirv_const
*)entry
->data
)->result
;
995 b
->consts
= _mesa_hash_table_create(NULL
, const_hash
, const_equals
);
999 struct spirv_const
*cnst
= CALLOC_STRUCT(spirv_const
);
1005 memcpy(&cnst
->args
, args
, sizeof(uint32_t) * num_args
);
1006 cnst
->num_args
= num_args
;
1008 cnst
->result
= spirv_builder_new_id(b
);
1009 spirv_buffer_prepare(&b
->types_const_defs
, 3 + num_args
);
1010 spirv_buffer_emit_word(&b
->types_const_defs
, op
| ((3 + num_args
) << 16));
1011 spirv_buffer_emit_word(&b
->types_const_defs
, type
);
1012 spirv_buffer_emit_word(&b
->types_const_defs
, cnst
->result
);
1013 for (int i
= 0; i
< num_args
; ++i
)
1014 spirv_buffer_emit_word(&b
->types_const_defs
, args
[i
]);
1016 entry
= _mesa_hash_table_insert(b
->consts
, cnst
, cnst
);
1019 return ((struct spirv_const
*)entry
->data
)->result
;
1023 spirv_builder_const_bool(struct spirv_builder
*b
, bool val
)
1025 return get_const_def(b
, val
? SpvOpConstantTrue
: SpvOpConstantFalse
,
1026 spirv_builder_type_bool(b
), NULL
, 0);
1030 spirv_builder_const_int(struct spirv_builder
*b
, int width
, int32_t val
)
1032 assert(width
<= 32);
1033 uint32_t args
[] = { val
};
1034 return get_const_def(b
, SpvOpConstant
, spirv_builder_type_int(b
, width
),
1035 args
, ARRAY_SIZE(args
));
1039 spirv_builder_const_uint(struct spirv_builder
*b
, int width
, uint32_t val
)
1041 assert(width
<= 32);
1042 uint32_t args
[] = { val
};
1043 return get_const_def(b
, SpvOpConstant
, spirv_builder_type_uint(b
, width
),
1044 args
, ARRAY_SIZE(args
));
1048 spirv_builder_const_float(struct spirv_builder
*b
, int width
, float val
)
1050 assert(width
<= 32);
1051 uint32_t args
[] = { u_bitcast_f2u(val
) };
1052 return get_const_def(b
, SpvOpConstant
, spirv_builder_type_float(b
, width
),
1053 args
, ARRAY_SIZE(args
));
1057 spirv_builder_const_composite(struct spirv_builder
*b
, SpvId result_type
,
1058 const SpvId constituents
[],
1059 size_t num_constituents
)
1061 return get_const_def(b
, SpvOpConstantComposite
, result_type
,
1062 (const uint32_t *)constituents
,
1067 spirv_builder_emit_var(struct spirv_builder
*b
, SpvId type
,
1068 SpvStorageClass storage_class
)
1070 assert(storage_class
!= SpvStorageClassGeneric
);
1071 struct spirv_buffer
*buf
= storage_class
!= SpvStorageClassFunction
?
1072 &b
->types_const_defs
: &b
->instructions
;
1074 SpvId ret
= spirv_builder_new_id(b
);
1075 spirv_buffer_prepare(buf
, 4);
1076 spirv_buffer_emit_word(buf
, SpvOpVariable
| (4 << 16));
1077 spirv_buffer_emit_word(buf
, type
);
1078 spirv_buffer_emit_word(buf
, ret
);
1079 spirv_buffer_emit_word(buf
, storage_class
);
1084 spirv_builder_import(struct spirv_builder
*b
, const char *name
)
1086 SpvId result
= spirv_builder_new_id(b
);
1087 size_t pos
= b
->imports
.num_words
;
1088 spirv_buffer_prepare(&b
->imports
, 2);
1089 spirv_buffer_emit_word(&b
->imports
, SpvOpExtInstImport
);
1090 spirv_buffer_emit_word(&b
->imports
, result
);
1091 int len
= spirv_buffer_emit_string(&b
->imports
, name
);
1092 b
->imports
.words
[pos
] |= (2 + len
) << 16;
1097 spirv_builder_get_num_words(struct spirv_builder
*b
)
1099 const size_t header_size
= 5;
1100 return header_size
+
1101 b
->capabilities
.num_words
+
1102 b
->imports
.num_words
+
1103 b
->memory_model
.num_words
+
1104 b
->entry_points
.num_words
+
1105 b
->exec_modes
.num_words
+
1106 b
->debug_names
.num_words
+
1107 b
->decorations
.num_words
+
1108 b
->types_const_defs
.num_words
+
1109 b
->instructions
.num_words
;
1113 spirv_builder_get_words(struct spirv_builder
*b
, uint32_t *words
,
1116 assert(num_words
>= spirv_builder_get_num_words(b
));
1119 words
[written
++] = SpvMagicNumber
;
1120 words
[written
++] = 0x00010000;
1121 words
[written
++] = 0;
1122 words
[written
++] = b
->prev_id
+ 1;
1123 words
[written
++] = 0;
1125 const struct spirv_buffer
*buffers
[] = {
1133 &b
->types_const_defs
,
1137 for (int i
= 0; i
< ARRAY_SIZE(buffers
); ++i
) {
1138 const struct spirv_buffer
*buffer
= buffers
[i
];
1139 for (int j
= 0; j
< buffer
->num_words
; ++j
)
1140 words
[written
++] = buffer
->words
[j
];
1143 assert(written
== spirv_builder_get_num_words(b
));