2 * Copyright © 2017 Connor Abbott
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 #include "nir_serialize.h"
25 #include "nir_control_flow.h"
26 #include "util/u_dynarray.h"
27 #include "util/u_math.h"
29 #define NIR_SERIALIZE_FUNC_HAS_IMPL ((void *)(intptr_t)1)
30 #define MAX_OBJECT_IDS (1 << 20)
39 const nir_shader
*nir
;
43 /* maps pointer to index */
44 struct hash_table
*remap_table
;
46 /* the next index to assign to a NIR in-memory object */
49 /* Array of write_phi_fixup structs representing phi sources that need to
50 * be resolved in the second pass.
52 struct util_dynarray phi_fixups
;
54 /* The last serialized type. */
55 const struct glsl_type
*last_type
;
56 const struct glsl_type
*last_interface_type
;
57 struct nir_variable_data last_var_data
;
59 /* For skipping equal ALU headers (typical after scalarization). */
60 nir_instr_type last_instr_type
;
61 uintptr_t last_alu_header_offset
;
63 /* Don't write optional data such as variable names. */
70 struct blob_reader
*blob
;
72 /* the next index to assign to a NIR in-memory object */
75 /* The length of the index -> object table */
76 uint32_t idx_table_len
;
78 /* map from index to deserialized pointer */
81 /* List of phi sources. */
82 struct list_head phi_srcs
;
84 /* The last deserialized type. */
85 const struct glsl_type
*last_type
;
86 const struct glsl_type
*last_interface_type
;
87 struct nir_variable_data last_var_data
;
91 write_add_object(write_ctx
*ctx
, const void *obj
)
93 uint32_t index
= ctx
->next_idx
++;
94 assert(index
!= MAX_OBJECT_IDS
);
95 _mesa_hash_table_insert(ctx
->remap_table
, obj
, (void *)(uintptr_t) index
);
99 write_lookup_object(write_ctx
*ctx
, const void *obj
)
101 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->remap_table
, obj
);
103 return (uint32_t)(uintptr_t) entry
->data
;
107 read_add_object(read_ctx
*ctx
, void *obj
)
109 assert(ctx
->next_idx
< ctx
->idx_table_len
);
110 ctx
->idx_table
[ctx
->next_idx
++] = obj
;
114 read_lookup_object(read_ctx
*ctx
, uint32_t idx
)
116 assert(idx
< ctx
->idx_table_len
);
117 return ctx
->idx_table
[idx
];
121 read_object(read_ctx
*ctx
)
123 return read_lookup_object(ctx
, blob_read_uint32(ctx
->blob
));
127 encode_bit_size_3bits(uint8_t bit_size
)
129 /* Encode values of 0, 1, 2, 4, 8, 16, 32, 64 in 3 bits. */
130 assert(bit_size
<= 64 && util_is_power_of_two_or_zero(bit_size
));
132 return util_logbase2(bit_size
) + 1;
137 decode_bit_size_3bits(uint8_t bit_size
)
140 return 1 << (bit_size
- 1);
144 #define NUM_COMPONENTS_IS_SEPARATE_7 7
147 encode_num_components_in_3bits(uint8_t num_components
)
149 if (num_components
<= 4)
150 return num_components
;
151 if (num_components
== 8)
153 if (num_components
== 16)
156 /* special value indicating that num_components is in the next uint32 */
157 return NUM_COMPONENTS_IS_SEPARATE_7
;
161 decode_num_components_in_3bits(uint8_t value
)
170 unreachable("invalid num_components encoding");
175 write_constant(write_ctx
*ctx
, const nir_constant
*c
)
177 blob_write_bytes(ctx
->blob
, c
->values
, sizeof(c
->values
));
178 blob_write_uint32(ctx
->blob
, c
->num_elements
);
179 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
180 write_constant(ctx
, c
->elements
[i
]);
183 static nir_constant
*
184 read_constant(read_ctx
*ctx
, nir_variable
*nvar
)
186 nir_constant
*c
= ralloc(nvar
, nir_constant
);
188 blob_copy_bytes(ctx
->blob
, (uint8_t *)c
->values
, sizeof(c
->values
));
189 c
->num_elements
= blob_read_uint32(ctx
->blob
);
190 c
->elements
= ralloc_array(nvar
, nir_constant
*, c
->num_elements
);
191 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
192 c
->elements
[i
] = read_constant(ctx
, nvar
);
197 enum var_data_encoding
{
199 var_encode_shader_temp
,
200 var_encode_function_temp
,
201 var_encode_location_diff
,
208 unsigned has_constant_initializer
:1;
209 unsigned has_pointer_initializer
:1;
210 unsigned has_interface_type
:1;
211 unsigned num_state_slots
:7;
212 unsigned data_encoding
:2;
213 unsigned type_same_as_last
:1;
214 unsigned interface_type_same_as_last
:1;
216 unsigned num_members
:16;
220 union packed_var_data_diff
{
225 int driver_location
:16;
230 write_variable(write_ctx
*ctx
, const nir_variable
*var
)
232 write_add_object(ctx
, var
);
234 assert(var
->num_state_slots
< (1 << 7));
236 STATIC_ASSERT(sizeof(union packed_var
) == 4);
237 union packed_var flags
;
240 flags
.u
.has_name
= !ctx
->strip
&& var
->name
;
241 flags
.u
.has_constant_initializer
= !!(var
->constant_initializer
);
242 flags
.u
.has_pointer_initializer
= !!(var
->pointer_initializer
);
243 flags
.u
.has_interface_type
= !!(var
->interface_type
);
244 flags
.u
.type_same_as_last
= var
->type
== ctx
->last_type
;
245 flags
.u
.interface_type_same_as_last
=
246 var
->interface_type
&& var
->interface_type
== ctx
->last_interface_type
;
247 flags
.u
.num_state_slots
= var
->num_state_slots
;
248 flags
.u
.num_members
= var
->num_members
;
250 struct nir_variable_data data
= var
->data
;
252 /* When stripping, we expect that the location is no longer needed,
253 * which is typically after shaders are linked.
256 data
.mode
!= nir_var_system_value
&&
257 data
.mode
!= nir_var_shader_in
&&
258 data
.mode
!= nir_var_shader_out
)
261 /* Temporary variables don't serialize var->data. */
262 if (data
.mode
== nir_var_shader_temp
)
263 flags
.u
.data_encoding
= var_encode_shader_temp
;
264 else if (data
.mode
== nir_var_function_temp
)
265 flags
.u
.data_encoding
= var_encode_function_temp
;
267 struct nir_variable_data tmp
= data
;
269 tmp
.location
= ctx
->last_var_data
.location
;
270 tmp
.location_frac
= ctx
->last_var_data
.location_frac
;
271 tmp
.driver_location
= ctx
->last_var_data
.driver_location
;
273 /* See if we can encode only the difference in locations from the last
276 if (memcmp(&ctx
->last_var_data
, &tmp
, sizeof(tmp
)) == 0 &&
277 abs((int)data
.location
-
278 (int)ctx
->last_var_data
.location
) < (1 << 12) &&
279 abs((int)data
.driver_location
-
280 (int)ctx
->last_var_data
.driver_location
) < (1 << 15))
281 flags
.u
.data_encoding
= var_encode_location_diff
;
283 flags
.u
.data_encoding
= var_encode_full
;
286 blob_write_uint32(ctx
->blob
, flags
.u32
);
288 if (!flags
.u
.type_same_as_last
) {
289 encode_type_to_blob(ctx
->blob
, var
->type
);
290 ctx
->last_type
= var
->type
;
293 if (var
->interface_type
&& !flags
.u
.interface_type_same_as_last
) {
294 encode_type_to_blob(ctx
->blob
, var
->interface_type
);
295 ctx
->last_interface_type
= var
->interface_type
;
298 if (flags
.u
.has_name
)
299 blob_write_string(ctx
->blob
, var
->name
);
301 if (flags
.u
.data_encoding
== var_encode_full
||
302 flags
.u
.data_encoding
== var_encode_location_diff
) {
303 if (flags
.u
.data_encoding
== var_encode_full
) {
304 blob_write_bytes(ctx
->blob
, &data
, sizeof(data
));
306 /* Serialize only the difference in locations from the last variable.
308 union packed_var_data_diff diff
;
310 diff
.u
.location
= data
.location
- ctx
->last_var_data
.location
;
311 diff
.u
.location_frac
= data
.location_frac
-
312 ctx
->last_var_data
.location_frac
;
313 diff
.u
.driver_location
= data
.driver_location
-
314 ctx
->last_var_data
.driver_location
;
316 blob_write_uint32(ctx
->blob
, diff
.u32
);
319 ctx
->last_var_data
= data
;
322 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
323 blob_write_bytes(ctx
->blob
, &var
->state_slots
[i
],
324 sizeof(var
->state_slots
[i
]));
326 if (var
->constant_initializer
)
327 write_constant(ctx
, var
->constant_initializer
);
328 if (var
->pointer_initializer
)
329 write_lookup_object(ctx
, var
->pointer_initializer
);
330 if (var
->num_members
> 0) {
331 blob_write_bytes(ctx
->blob
, (uint8_t *) var
->members
,
332 var
->num_members
* sizeof(*var
->members
));
336 static nir_variable
*
337 read_variable(read_ctx
*ctx
)
339 nir_variable
*var
= rzalloc(ctx
->nir
, nir_variable
);
340 read_add_object(ctx
, var
);
342 union packed_var flags
;
343 flags
.u32
= blob_read_uint32(ctx
->blob
);
345 if (flags
.u
.type_same_as_last
) {
346 var
->type
= ctx
->last_type
;
348 var
->type
= decode_type_from_blob(ctx
->blob
);
349 ctx
->last_type
= var
->type
;
352 if (flags
.u
.has_interface_type
) {
353 if (flags
.u
.interface_type_same_as_last
) {
354 var
->interface_type
= ctx
->last_interface_type
;
356 var
->interface_type
= decode_type_from_blob(ctx
->blob
);
357 ctx
->last_interface_type
= var
->interface_type
;
361 if (flags
.u
.has_name
) {
362 const char *name
= blob_read_string(ctx
->blob
);
363 var
->name
= ralloc_strdup(var
, name
);
368 if (flags
.u
.data_encoding
== var_encode_shader_temp
)
369 var
->data
.mode
= nir_var_shader_temp
;
370 else if (flags
.u
.data_encoding
== var_encode_function_temp
)
371 var
->data
.mode
= nir_var_function_temp
;
372 else if (flags
.u
.data_encoding
== var_encode_full
) {
373 blob_copy_bytes(ctx
->blob
, (uint8_t *) &var
->data
, sizeof(var
->data
));
374 ctx
->last_var_data
= var
->data
;
375 } else { /* var_encode_location_diff */
376 union packed_var_data_diff diff
;
377 diff
.u32
= blob_read_uint32(ctx
->blob
);
379 var
->data
= ctx
->last_var_data
;
380 var
->data
.location
+= diff
.u
.location
;
381 var
->data
.location_frac
+= diff
.u
.location_frac
;
382 var
->data
.driver_location
+= diff
.u
.driver_location
;
384 ctx
->last_var_data
= var
->data
;
387 var
->num_state_slots
= flags
.u
.num_state_slots
;
388 if (var
->num_state_slots
!= 0) {
389 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
390 var
->num_state_slots
);
391 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
392 blob_copy_bytes(ctx
->blob
, &var
->state_slots
[i
],
393 sizeof(var
->state_slots
[i
]));
396 if (flags
.u
.has_constant_initializer
)
397 var
->constant_initializer
= read_constant(ctx
, var
);
399 var
->constant_initializer
= NULL
;
401 if (flags
.u
.has_pointer_initializer
)
402 var
->pointer_initializer
= read_object(ctx
);
404 var
->pointer_initializer
= NULL
;
406 var
->num_members
= flags
.u
.num_members
;
407 if (var
->num_members
> 0) {
408 var
->members
= ralloc_array(var
, struct nir_variable_data
,
410 blob_copy_bytes(ctx
->blob
, (uint8_t *) var
->members
,
411 var
->num_members
* sizeof(*var
->members
));
418 write_var_list(write_ctx
*ctx
, const struct exec_list
*src
)
420 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
421 foreach_list_typed(nir_variable
, var
, node
, src
) {
422 write_variable(ctx
, var
);
427 read_var_list(read_ctx
*ctx
, struct exec_list
*dst
)
429 exec_list_make_empty(dst
);
430 unsigned num_vars
= blob_read_uint32(ctx
->blob
);
431 for (unsigned i
= 0; i
< num_vars
; i
++) {
432 nir_variable
*var
= read_variable(ctx
);
433 exec_list_push_tail(dst
, &var
->node
);
438 write_register(write_ctx
*ctx
, const nir_register
*reg
)
440 write_add_object(ctx
, reg
);
441 blob_write_uint32(ctx
->blob
, reg
->num_components
);
442 blob_write_uint32(ctx
->blob
, reg
->bit_size
);
443 blob_write_uint32(ctx
->blob
, reg
->num_array_elems
);
444 blob_write_uint32(ctx
->blob
, reg
->index
);
445 blob_write_uint32(ctx
->blob
, !ctx
->strip
&& reg
->name
);
446 if (!ctx
->strip
&& reg
->name
)
447 blob_write_string(ctx
->blob
, reg
->name
);
450 static nir_register
*
451 read_register(read_ctx
*ctx
)
453 nir_register
*reg
= ralloc(ctx
->nir
, nir_register
);
454 read_add_object(ctx
, reg
);
455 reg
->num_components
= blob_read_uint32(ctx
->blob
);
456 reg
->bit_size
= blob_read_uint32(ctx
->blob
);
457 reg
->num_array_elems
= blob_read_uint32(ctx
->blob
);
458 reg
->index
= blob_read_uint32(ctx
->blob
);
459 bool has_name
= blob_read_uint32(ctx
->blob
);
461 const char *name
= blob_read_string(ctx
->blob
);
462 reg
->name
= ralloc_strdup(reg
, name
);
467 list_inithead(®
->uses
);
468 list_inithead(®
->defs
);
469 list_inithead(®
->if_uses
);
475 write_reg_list(write_ctx
*ctx
, const struct exec_list
*src
)
477 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
478 foreach_list_typed(nir_register
, reg
, node
, src
)
479 write_register(ctx
, reg
);
483 read_reg_list(read_ctx
*ctx
, struct exec_list
*dst
)
485 exec_list_make_empty(dst
);
486 unsigned num_regs
= blob_read_uint32(ctx
->blob
);
487 for (unsigned i
= 0; i
< num_regs
; i
++) {
488 nir_register
*reg
= read_register(ctx
);
489 exec_list_push_tail(dst
, ®
->node
);
496 unsigned is_ssa
:1; /* <-- Header */
497 unsigned is_indirect
:1;
498 unsigned object_idx
:20;
499 unsigned _footer
:10; /* <-- Footer */
502 unsigned _header
:22; /* <-- Header */
503 unsigned negate
:1; /* <-- Footer */
505 unsigned swizzle_x
:2;
506 unsigned swizzle_y
:2;
507 unsigned swizzle_z
:2;
508 unsigned swizzle_w
:2;
511 unsigned _header
:22; /* <-- Header */
512 unsigned src_type
:5; /* <-- Footer */
518 write_src_full(write_ctx
*ctx
, const nir_src
*src
, union packed_src header
)
520 /* Since sources are very frequent, we try to save some space when storing
521 * them. In particular, we store whether the source is a register and
522 * whether the register has an indirect index in the low two bits. We can
523 * assume that the high two bits of the index are zero, since otherwise our
524 * address space would've been exhausted allocating the remap table!
526 header
.any
.is_ssa
= src
->is_ssa
;
528 header
.any
.object_idx
= write_lookup_object(ctx
, src
->ssa
);
529 blob_write_uint32(ctx
->blob
, header
.u32
);
531 header
.any
.object_idx
= write_lookup_object(ctx
, src
->reg
.reg
);
532 header
.any
.is_indirect
= !!src
->reg
.indirect
;
533 blob_write_uint32(ctx
->blob
, header
.u32
);
534 blob_write_uint32(ctx
->blob
, src
->reg
.base_offset
);
535 if (src
->reg
.indirect
) {
536 union packed_src header
= {0};
537 write_src_full(ctx
, src
->reg
.indirect
, header
);
543 write_src(write_ctx
*ctx
, const nir_src
*src
)
545 union packed_src header
= {0};
546 write_src_full(ctx
, src
, header
);
549 static union packed_src
550 read_src(read_ctx
*ctx
, nir_src
*src
, void *mem_ctx
)
552 STATIC_ASSERT(sizeof(union packed_src
) == 4);
553 union packed_src header
;
554 header
.u32
= blob_read_uint32(ctx
->blob
);
556 src
->is_ssa
= header
.any
.is_ssa
;
558 src
->ssa
= read_lookup_object(ctx
, header
.any
.object_idx
);
560 src
->reg
.reg
= read_lookup_object(ctx
, header
.any
.object_idx
);
561 src
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
562 if (header
.any
.is_indirect
) {
563 src
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
564 read_src(ctx
, src
->reg
.indirect
, mem_ctx
);
566 src
->reg
.indirect
= NULL
;
577 uint8_t num_components
:3;
582 uint8_t is_indirect
:1;
587 enum intrinsic_const_indices_encoding
{
588 /* Use the 9 bits of packed_const_indices to store 1-9 indices.
589 * 1 9-bit index, or 2 4-bit indices, or 3 3-bit indices, or
590 * 4 2-bit indices, or 5-9 1-bit indices.
592 * The common case for load_ubo is 0, 0, 0, which is trivially represented.
593 * The common cases for load_interpolated_input also fit here, e.g.: 7, 3
595 const_indices_9bit_all_combined
,
597 const_indices_8bit
, /* 8 bits per element */
598 const_indices_16bit
, /* 16 bits per element */
599 const_indices_32bit
, /* 32 bits per element */
602 enum load_const_packing
{
603 /* Constants are not packed and are stored in following dwords. */
606 /* packed_value contains high 19 bits, low bits are 0,
607 * good for floating-point decimals
609 load_const_scalar_hi_19bits
,
611 /* packed_value contains low 19 bits, high bits are sign-extended */
612 load_const_scalar_lo_19bits_sext
,
618 unsigned instr_type
:4; /* always present */
620 unsigned dest
:8; /* always last */
623 unsigned instr_type
:4;
625 unsigned no_signed_wrap
:1;
626 unsigned no_unsigned_wrap
:1;
628 /* Reg: writemask; SSA: swizzles for 2 srcs */
629 unsigned writemask_or_two_swizzles
:4;
631 unsigned packed_src_ssa_16bit
:1;
632 /* Scalarized ALUs always have the same header. */
633 unsigned num_followup_alu_sharing_header
:2;
637 unsigned instr_type
:4;
638 unsigned deref_type
:3;
639 unsigned cast_type_same_as_last
:1;
640 unsigned mode
:12; /* deref_var redefines this */
641 unsigned packed_src_ssa_16bit
:1; /* deref_var redefines this */
642 unsigned _pad
:3; /* deref_var redefines this */
646 unsigned instr_type
:4;
647 unsigned deref_type
:3;
649 unsigned object_idx
:16; /* if 0, the object ID is a separate uint32 */
653 unsigned instr_type
:4;
654 unsigned intrinsic
:9;
655 unsigned const_indices_encoding
:2;
656 unsigned packed_const_indices
:9;
660 unsigned instr_type
:4;
661 unsigned last_component
:4;
663 unsigned packing
:2; /* enum load_const_packing */
664 unsigned packed_value
:19; /* meaning determined by packing */
667 unsigned instr_type
:4;
668 unsigned last_component
:4;
673 unsigned instr_type
:4;
680 unsigned instr_type
:4;
681 unsigned num_srcs
:20;
685 unsigned instr_type
:4;
691 /* Write "lo24" as low 24 bits in the first uint32. */
693 write_dest(write_ctx
*ctx
, const nir_dest
*dst
, union packed_instr header
,
694 nir_instr_type instr_type
)
696 STATIC_ASSERT(sizeof(union packed_dest
) == 1);
697 union packed_dest dest
;
700 dest
.ssa
.is_ssa
= dst
->is_ssa
;
702 dest
.ssa
.has_name
= !ctx
->strip
&& dst
->ssa
.name
;
703 dest
.ssa
.num_components
=
704 encode_num_components_in_3bits(dst
->ssa
.num_components
);
705 dest
.ssa
.bit_size
= encode_bit_size_3bits(dst
->ssa
.bit_size
);
707 dest
.reg
.is_indirect
= !!(dst
->reg
.indirect
);
709 header
.any
.dest
= dest
.u8
;
711 /* Check if the current ALU instruction has the same header as the previous
712 * instruction that is also ALU. If it is, we don't have to write
713 * the current header. This is a typical occurence after scalarization.
715 if (instr_type
== nir_instr_type_alu
) {
716 bool equal_header
= false;
718 if (ctx
->last_instr_type
== nir_instr_type_alu
) {
719 assert(ctx
->last_alu_header_offset
);
720 union packed_instr
*last_header
=
721 (union packed_instr
*)(ctx
->blob
->data
+
722 ctx
->last_alu_header_offset
);
724 /* Clear the field that counts ALUs with equal headers. */
725 union packed_instr clean_header
;
726 clean_header
.u32
= last_header
->u32
;
727 clean_header
.alu
.num_followup_alu_sharing_header
= 0;
729 /* There can be at most 4 consecutive ALU instructions
730 * sharing the same header.
732 if (last_header
->alu
.num_followup_alu_sharing_header
< 3 &&
733 header
.u32
== clean_header
.u32
) {
734 last_header
->alu
.num_followup_alu_sharing_header
++;
740 ctx
->last_alu_header_offset
= ctx
->blob
->size
;
741 blob_write_uint32(ctx
->blob
, header
.u32
);
744 blob_write_uint32(ctx
->blob
, header
.u32
);
747 if (dest
.ssa
.is_ssa
&&
748 dest
.ssa
.num_components
== NUM_COMPONENTS_IS_SEPARATE_7
)
749 blob_write_uint32(ctx
->blob
, dst
->ssa
.num_components
);
752 write_add_object(ctx
, &dst
->ssa
);
753 if (dest
.ssa
.has_name
)
754 blob_write_string(ctx
->blob
, dst
->ssa
.name
);
756 blob_write_uint32(ctx
->blob
, write_lookup_object(ctx
, dst
->reg
.reg
));
757 blob_write_uint32(ctx
->blob
, dst
->reg
.base_offset
);
758 if (dst
->reg
.indirect
)
759 write_src(ctx
, dst
->reg
.indirect
);
764 read_dest(read_ctx
*ctx
, nir_dest
*dst
, nir_instr
*instr
,
765 union packed_instr header
)
767 union packed_dest dest
;
768 dest
.u8
= header
.any
.dest
;
770 if (dest
.ssa
.is_ssa
) {
771 unsigned bit_size
= decode_bit_size_3bits(dest
.ssa
.bit_size
);
772 unsigned num_components
;
773 if (dest
.ssa
.num_components
== NUM_COMPONENTS_IS_SEPARATE_7
)
774 num_components
= blob_read_uint32(ctx
->blob
);
776 num_components
= decode_num_components_in_3bits(dest
.ssa
.num_components
);
777 char *name
= dest
.ssa
.has_name
? blob_read_string(ctx
->blob
) : NULL
;
778 nir_ssa_dest_init(instr
, dst
, num_components
, bit_size
, name
);
779 read_add_object(ctx
, &dst
->ssa
);
781 dst
->reg
.reg
= read_object(ctx
);
782 dst
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
783 if (dest
.reg
.is_indirect
) {
784 dst
->reg
.indirect
= ralloc(instr
, nir_src
);
785 read_src(ctx
, dst
->reg
.indirect
, instr
);
791 are_object_ids_16bit(write_ctx
*ctx
)
793 /* Check the highest object ID, because they are monotonic. */
794 return ctx
->next_idx
< (1 << 16);
798 is_alu_src_ssa_16bit(write_ctx
*ctx
, const nir_alu_instr
*alu
)
800 unsigned num_srcs
= nir_op_infos
[alu
->op
].num_inputs
;
802 for (unsigned i
= 0; i
< num_srcs
; i
++) {
803 if (!alu
->src
[i
].src
.is_ssa
|| alu
->src
[i
].abs
|| alu
->src
[i
].negate
)
806 unsigned src_components
= nir_ssa_alu_instr_src_components(alu
, i
);
808 for (unsigned chan
= 0; chan
< src_components
; chan
++) {
809 /* The swizzles for src0.x and src1.x are stored
810 * in writemask_or_two_swizzles for SSA ALUs.
812 if (alu
->dest
.dest
.is_ssa
&& i
< 2 && chan
== 0 &&
813 alu
->src
[i
].swizzle
[chan
] < 4)
816 if (alu
->src
[i
].swizzle
[chan
] != chan
)
821 return are_object_ids_16bit(ctx
);
825 write_alu(write_ctx
*ctx
, const nir_alu_instr
*alu
)
827 unsigned num_srcs
= nir_op_infos
[alu
->op
].num_inputs
;
828 unsigned dst_components
= nir_dest_num_components(alu
->dest
.dest
);
830 /* 9 bits for nir_op */
831 STATIC_ASSERT(nir_num_opcodes
<= 512);
832 union packed_instr header
;
835 header
.alu
.instr_type
= alu
->instr
.type
;
836 header
.alu
.exact
= alu
->exact
;
837 header
.alu
.no_signed_wrap
= alu
->no_signed_wrap
;
838 header
.alu
.no_unsigned_wrap
= alu
->no_unsigned_wrap
;
839 header
.alu
.saturate
= alu
->dest
.saturate
;
840 header
.alu
.op
= alu
->op
;
841 header
.alu
.packed_src_ssa_16bit
= is_alu_src_ssa_16bit(ctx
, alu
);
843 if (header
.alu
.packed_src_ssa_16bit
&&
844 alu
->dest
.dest
.is_ssa
) {
845 /* For packed srcs of SSA ALUs, this field stores the swizzles. */
846 header
.alu
.writemask_or_two_swizzles
= alu
->src
[0].swizzle
[0];
848 header
.alu
.writemask_or_two_swizzles
|= alu
->src
[1].swizzle
[0] << 2;
849 } else if (!alu
->dest
.dest
.is_ssa
&& dst_components
<= 4) {
850 /* For vec4 registers, this field is a writemask. */
851 header
.alu
.writemask_or_two_swizzles
= alu
->dest
.write_mask
;
854 write_dest(ctx
, &alu
->dest
.dest
, header
, alu
->instr
.type
);
856 if (!alu
->dest
.dest
.is_ssa
&& dst_components
> 4)
857 blob_write_uint32(ctx
->blob
, alu
->dest
.write_mask
);
859 if (header
.alu
.packed_src_ssa_16bit
) {
860 for (unsigned i
= 0; i
< num_srcs
; i
++) {
861 assert(alu
->src
[i
].src
.is_ssa
);
862 unsigned idx
= write_lookup_object(ctx
, alu
->src
[i
].src
.ssa
);
863 assert(idx
< (1 << 16));
864 blob_write_uint16(ctx
->blob
, idx
);
867 for (unsigned i
= 0; i
< num_srcs
; i
++) {
868 unsigned src_channels
= nir_ssa_alu_instr_src_components(alu
, i
);
869 unsigned src_components
= nir_src_num_components(alu
->src
[i
].src
);
870 union packed_src src
;
871 bool packed
= src_components
<= 4 && src_channels
<= 4;
874 src
.alu
.negate
= alu
->src
[i
].negate
;
875 src
.alu
.abs
= alu
->src
[i
].abs
;
878 src
.alu
.swizzle_x
= alu
->src
[i
].swizzle
[0];
879 src
.alu
.swizzle_y
= alu
->src
[i
].swizzle
[1];
880 src
.alu
.swizzle_z
= alu
->src
[i
].swizzle
[2];
881 src
.alu
.swizzle_w
= alu
->src
[i
].swizzle
[3];
884 write_src_full(ctx
, &alu
->src
[i
].src
, src
);
886 /* Store swizzles for vec8 and vec16. */
888 for (unsigned o
= 0; o
< src_channels
; o
+= 8) {
891 for (unsigned j
= 0; j
< 8 && o
+ j
< src_channels
; j
++) {
892 value
|= (uint32_t)alu
->src
[i
].swizzle
[o
+ j
] <<
893 (4 * j
); /* 4 bits per swizzle */
896 blob_write_uint32(ctx
->blob
, value
);
903 static nir_alu_instr
*
904 read_alu(read_ctx
*ctx
, union packed_instr header
)
906 unsigned num_srcs
= nir_op_infos
[header
.alu
.op
].num_inputs
;
907 nir_alu_instr
*alu
= nir_alu_instr_create(ctx
->nir
, header
.alu
.op
);
909 alu
->exact
= header
.alu
.exact
;
910 alu
->no_signed_wrap
= header
.alu
.no_signed_wrap
;
911 alu
->no_unsigned_wrap
= header
.alu
.no_unsigned_wrap
;
912 alu
->dest
.saturate
= header
.alu
.saturate
;
914 read_dest(ctx
, &alu
->dest
.dest
, &alu
->instr
, header
);
916 unsigned dst_components
= nir_dest_num_components(alu
->dest
.dest
);
918 if (alu
->dest
.dest
.is_ssa
) {
919 alu
->dest
.write_mask
= u_bit_consecutive(0, dst_components
);
920 } else if (dst_components
<= 4) {
921 alu
->dest
.write_mask
= header
.alu
.writemask_or_two_swizzles
;
923 alu
->dest
.write_mask
= blob_read_uint32(ctx
->blob
);
926 if (header
.alu
.packed_src_ssa_16bit
) {
927 for (unsigned i
= 0; i
< num_srcs
; i
++) {
928 nir_alu_src
*src
= &alu
->src
[i
];
929 src
->src
.is_ssa
= true;
930 src
->src
.ssa
= read_lookup_object(ctx
, blob_read_uint16(ctx
->blob
));
932 memset(&src
->swizzle
, 0, sizeof(src
->swizzle
));
934 unsigned src_components
= nir_ssa_alu_instr_src_components(alu
, i
);
936 for (unsigned chan
= 0; chan
< src_components
; chan
++)
937 src
->swizzle
[chan
] = chan
;
940 for (unsigned i
= 0; i
< num_srcs
; i
++) {
941 union packed_src src
= read_src(ctx
, &alu
->src
[i
].src
, &alu
->instr
);
942 unsigned src_channels
= nir_ssa_alu_instr_src_components(alu
, i
);
943 unsigned src_components
= nir_src_num_components(alu
->src
[i
].src
);
944 bool packed
= src_components
<= 4 && src_channels
<= 4;
946 alu
->src
[i
].negate
= src
.alu
.negate
;
947 alu
->src
[i
].abs
= src
.alu
.abs
;
949 memset(&alu
->src
[i
].swizzle
, 0, sizeof(alu
->src
[i
].swizzle
));
952 alu
->src
[i
].swizzle
[0] = src
.alu
.swizzle_x
;
953 alu
->src
[i
].swizzle
[1] = src
.alu
.swizzle_y
;
954 alu
->src
[i
].swizzle
[2] = src
.alu
.swizzle_z
;
955 alu
->src
[i
].swizzle
[3] = src
.alu
.swizzle_w
;
957 /* Load swizzles for vec8 and vec16. */
958 for (unsigned o
= 0; o
< src_channels
; o
+= 8) {
959 unsigned value
= blob_read_uint32(ctx
->blob
);
961 for (unsigned j
= 0; j
< 8 && o
+ j
< src_channels
; j
++) {
962 alu
->src
[i
].swizzle
[o
+ j
] =
963 (value
>> (4 * j
)) & 0xf; /* 4 bits per swizzle */
970 if (header
.alu
.packed_src_ssa_16bit
&&
971 alu
->dest
.dest
.is_ssa
) {
972 alu
->src
[0].swizzle
[0] = header
.alu
.writemask_or_two_swizzles
& 0x3;
974 alu
->src
[1].swizzle
[0] = header
.alu
.writemask_or_two_swizzles
>> 2;
981 write_deref(write_ctx
*ctx
, const nir_deref_instr
*deref
)
983 assert(deref
->deref_type
< 8);
984 assert(deref
->mode
< (1 << 12));
986 union packed_instr header
;
989 header
.deref
.instr_type
= deref
->instr
.type
;
990 header
.deref
.deref_type
= deref
->deref_type
;
992 if (deref
->deref_type
== nir_deref_type_cast
) {
993 header
.deref
.mode
= deref
->mode
;
994 header
.deref
.cast_type_same_as_last
= deref
->type
== ctx
->last_type
;
997 unsigned var_idx
= 0;
998 if (deref
->deref_type
== nir_deref_type_var
) {
999 var_idx
= write_lookup_object(ctx
, deref
->var
);
1000 if (var_idx
&& var_idx
< (1 << 16))
1001 header
.deref_var
.object_idx
= var_idx
;
1004 if (deref
->deref_type
== nir_deref_type_array
||
1005 deref
->deref_type
== nir_deref_type_ptr_as_array
) {
1006 header
.deref
.packed_src_ssa_16bit
=
1007 deref
->parent
.is_ssa
&& deref
->arr
.index
.is_ssa
&&
1008 are_object_ids_16bit(ctx
);
1011 write_dest(ctx
, &deref
->dest
, header
, deref
->instr
.type
);
1013 switch (deref
->deref_type
) {
1014 case nir_deref_type_var
:
1015 if (!header
.deref_var
.object_idx
)
1016 blob_write_uint32(ctx
->blob
, var_idx
);
1019 case nir_deref_type_struct
:
1020 write_src(ctx
, &deref
->parent
);
1021 blob_write_uint32(ctx
->blob
, deref
->strct
.index
);
1024 case nir_deref_type_array
:
1025 case nir_deref_type_ptr_as_array
:
1026 if (header
.deref
.packed_src_ssa_16bit
) {
1027 blob_write_uint16(ctx
->blob
,
1028 write_lookup_object(ctx
, deref
->parent
.ssa
));
1029 blob_write_uint16(ctx
->blob
,
1030 write_lookup_object(ctx
, deref
->arr
.index
.ssa
));
1032 write_src(ctx
, &deref
->parent
);
1033 write_src(ctx
, &deref
->arr
.index
);
1037 case nir_deref_type_cast
:
1038 write_src(ctx
, &deref
->parent
);
1039 blob_write_uint32(ctx
->blob
, deref
->cast
.ptr_stride
);
1040 if (!header
.deref
.cast_type_same_as_last
) {
1041 encode_type_to_blob(ctx
->blob
, deref
->type
);
1042 ctx
->last_type
= deref
->type
;
1046 case nir_deref_type_array_wildcard
:
1047 write_src(ctx
, &deref
->parent
);
1051 unreachable("Invalid deref type");
1055 static nir_deref_instr
*
1056 read_deref(read_ctx
*ctx
, union packed_instr header
)
1058 nir_deref_type deref_type
= header
.deref
.deref_type
;
1059 nir_deref_instr
*deref
= nir_deref_instr_create(ctx
->nir
, deref_type
);
1061 read_dest(ctx
, &deref
->dest
, &deref
->instr
, header
);
1063 nir_deref_instr
*parent
;
1065 switch (deref
->deref_type
) {
1066 case nir_deref_type_var
:
1067 if (header
.deref_var
.object_idx
)
1068 deref
->var
= read_lookup_object(ctx
, header
.deref_var
.object_idx
);
1070 deref
->var
= read_object(ctx
);
1072 deref
->type
= deref
->var
->type
;
1075 case nir_deref_type_struct
:
1076 read_src(ctx
, &deref
->parent
, &deref
->instr
);
1077 parent
= nir_src_as_deref(deref
->parent
);
1078 deref
->strct
.index
= blob_read_uint32(ctx
->blob
);
1079 deref
->type
= glsl_get_struct_field(parent
->type
, deref
->strct
.index
);
1082 case nir_deref_type_array
:
1083 case nir_deref_type_ptr_as_array
:
1084 if (header
.deref
.packed_src_ssa_16bit
) {
1085 deref
->parent
.is_ssa
= true;
1086 deref
->parent
.ssa
= read_lookup_object(ctx
, blob_read_uint16(ctx
->blob
));
1087 deref
->arr
.index
.is_ssa
= true;
1088 deref
->arr
.index
.ssa
= read_lookup_object(ctx
, blob_read_uint16(ctx
->blob
));
1090 read_src(ctx
, &deref
->parent
, &deref
->instr
);
1091 read_src(ctx
, &deref
->arr
.index
, &deref
->instr
);
1094 parent
= nir_src_as_deref(deref
->parent
);
1095 if (deref
->deref_type
== nir_deref_type_array
)
1096 deref
->type
= glsl_get_array_element(parent
->type
);
1098 deref
->type
= parent
->type
;
1101 case nir_deref_type_cast
:
1102 read_src(ctx
, &deref
->parent
, &deref
->instr
);
1103 deref
->cast
.ptr_stride
= blob_read_uint32(ctx
->blob
);
1104 if (header
.deref
.cast_type_same_as_last
) {
1105 deref
->type
= ctx
->last_type
;
1107 deref
->type
= decode_type_from_blob(ctx
->blob
);
1108 ctx
->last_type
= deref
->type
;
1112 case nir_deref_type_array_wildcard
:
1113 read_src(ctx
, &deref
->parent
, &deref
->instr
);
1114 parent
= nir_src_as_deref(deref
->parent
);
1115 deref
->type
= glsl_get_array_element(parent
->type
);
1119 unreachable("Invalid deref type");
1122 if (deref_type
== nir_deref_type_var
) {
1123 deref
->mode
= deref
->var
->data
.mode
;
1124 } else if (deref
->deref_type
== nir_deref_type_cast
) {
1125 deref
->mode
= header
.deref
.mode
;
1127 assert(deref
->parent
.is_ssa
);
1128 deref
->mode
= nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
)->mode
;
1135 write_intrinsic(write_ctx
*ctx
, const nir_intrinsic_instr
*intrin
)
1137 /* 9 bits for nir_intrinsic_op */
1138 STATIC_ASSERT(nir_num_intrinsics
<= 512);
1139 unsigned num_srcs
= nir_intrinsic_infos
[intrin
->intrinsic
].num_srcs
;
1140 unsigned num_indices
= nir_intrinsic_infos
[intrin
->intrinsic
].num_indices
;
1141 assert(intrin
->intrinsic
< 512);
1143 union packed_instr header
;
1146 header
.intrinsic
.instr_type
= intrin
->instr
.type
;
1147 header
.intrinsic
.intrinsic
= intrin
->intrinsic
;
1149 /* Analyze constant indices to decide how to encode them. */
1151 unsigned max_bits
= 0;
1152 for (unsigned i
= 0; i
< num_indices
; i
++) {
1153 unsigned max
= util_last_bit(intrin
->const_index
[i
]);
1154 max_bits
= MAX2(max_bits
, max
);
1157 if (max_bits
* num_indices
<= 9) {
1158 header
.intrinsic
.const_indices_encoding
= const_indices_9bit_all_combined
;
1160 /* Pack all const indices into 6 bits. */
1161 unsigned bit_size
= 9 / num_indices
;
1162 for (unsigned i
= 0; i
< num_indices
; i
++) {
1163 header
.intrinsic
.packed_const_indices
|=
1164 intrin
->const_index
[i
] << (i
* bit_size
);
1166 } else if (max_bits
<= 8)
1167 header
.intrinsic
.const_indices_encoding
= const_indices_8bit
;
1168 else if (max_bits
<= 16)
1169 header
.intrinsic
.const_indices_encoding
= const_indices_16bit
;
1171 header
.intrinsic
.const_indices_encoding
= const_indices_32bit
;
1174 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
)
1175 write_dest(ctx
, &intrin
->dest
, header
, intrin
->instr
.type
);
1177 blob_write_uint32(ctx
->blob
, header
.u32
);
1179 for (unsigned i
= 0; i
< num_srcs
; i
++)
1180 write_src(ctx
, &intrin
->src
[i
]);
1183 switch (header
.intrinsic
.const_indices_encoding
) {
1184 case const_indices_8bit
:
1185 for (unsigned i
= 0; i
< num_indices
; i
++)
1186 blob_write_uint8(ctx
->blob
, intrin
->const_index
[i
]);
1188 case const_indices_16bit
:
1189 for (unsigned i
= 0; i
< num_indices
; i
++)
1190 blob_write_uint16(ctx
->blob
, intrin
->const_index
[i
]);
1192 case const_indices_32bit
:
1193 for (unsigned i
= 0; i
< num_indices
; i
++)
1194 blob_write_uint32(ctx
->blob
, intrin
->const_index
[i
]);
1200 static nir_intrinsic_instr
*
1201 read_intrinsic(read_ctx
*ctx
, union packed_instr header
)
1203 nir_intrinsic_op op
= header
.intrinsic
.intrinsic
;
1204 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(ctx
->nir
, op
);
1206 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
1207 unsigned num_indices
= nir_intrinsic_infos
[op
].num_indices
;
1209 if (nir_intrinsic_infos
[op
].has_dest
)
1210 read_dest(ctx
, &intrin
->dest
, &intrin
->instr
, header
);
1212 for (unsigned i
= 0; i
< num_srcs
; i
++)
1213 read_src(ctx
, &intrin
->src
[i
], &intrin
->instr
);
1215 /* Vectorized instrinsics have num_components same as dst or src that has
1216 * 0 components in the info. Find it.
1218 if (nir_intrinsic_infos
[op
].has_dest
&&
1219 nir_intrinsic_infos
[op
].dest_components
== 0) {
1220 intrin
->num_components
= nir_dest_num_components(intrin
->dest
);
1222 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1223 if (nir_intrinsic_infos
[op
].src_components
[i
] == 0) {
1224 intrin
->num_components
= nir_src_num_components(intrin
->src
[i
]);
1231 switch (header
.intrinsic
.const_indices_encoding
) {
1232 case const_indices_9bit_all_combined
: {
1233 unsigned bit_size
= 9 / num_indices
;
1234 unsigned bit_mask
= u_bit_consecutive(0, bit_size
);
1235 for (unsigned i
= 0; i
< num_indices
; i
++) {
1236 intrin
->const_index
[i
] =
1237 (header
.intrinsic
.packed_const_indices
>> (i
* bit_size
)) &
1242 case const_indices_8bit
:
1243 for (unsigned i
= 0; i
< num_indices
; i
++)
1244 intrin
->const_index
[i
] = blob_read_uint8(ctx
->blob
);
1246 case const_indices_16bit
:
1247 for (unsigned i
= 0; i
< num_indices
; i
++)
1248 intrin
->const_index
[i
] = blob_read_uint16(ctx
->blob
);
1250 case const_indices_32bit
:
1251 for (unsigned i
= 0; i
< num_indices
; i
++)
1252 intrin
->const_index
[i
] = blob_read_uint32(ctx
->blob
);
1261 write_load_const(write_ctx
*ctx
, const nir_load_const_instr
*lc
)
1263 assert(lc
->def
.num_components
>= 1 && lc
->def
.num_components
<= 16);
1264 union packed_instr header
;
1267 header
.load_const
.instr_type
= lc
->instr
.type
;
1268 header
.load_const
.last_component
= lc
->def
.num_components
- 1;
1269 header
.load_const
.bit_size
= encode_bit_size_3bits(lc
->def
.bit_size
);
1270 header
.load_const
.packing
= load_const_full
;
1272 /* Try to pack 1-component constants into the 19 free bits in the header. */
1273 if (lc
->def
.num_components
== 1) {
1274 switch (lc
->def
.bit_size
) {
1276 if ((lc
->value
[0].u64
& 0x1fffffffffffull
) == 0) {
1277 /* packed_value contains high 19 bits, low bits are 0 */
1278 header
.load_const
.packing
= load_const_scalar_hi_19bits
;
1279 header
.load_const
.packed_value
= lc
->value
[0].u64
>> 45;
1280 } else if (((lc
->value
[0].i64
<< 45) >> 45) == lc
->value
[0].i64
) {
1281 /* packed_value contains low 19 bits, high bits are sign-extended */
1282 header
.load_const
.packing
= load_const_scalar_lo_19bits_sext
;
1283 header
.load_const
.packed_value
= lc
->value
[0].u64
;
1288 if ((lc
->value
[0].u32
& 0x1fff) == 0) {
1289 header
.load_const
.packing
= load_const_scalar_hi_19bits
;
1290 header
.load_const
.packed_value
= lc
->value
[0].u32
>> 13;
1291 } else if (((lc
->value
[0].i32
<< 13) >> 13) == lc
->value
[0].i32
) {
1292 header
.load_const
.packing
= load_const_scalar_lo_19bits_sext
;
1293 header
.load_const
.packed_value
= lc
->value
[0].u32
;
1298 header
.load_const
.packing
= load_const_scalar_lo_19bits_sext
;
1299 header
.load_const
.packed_value
= lc
->value
[0].u16
;
1302 header
.load_const
.packing
= load_const_scalar_lo_19bits_sext
;
1303 header
.load_const
.packed_value
= lc
->value
[0].u8
;
1306 header
.load_const
.packing
= load_const_scalar_lo_19bits_sext
;
1307 header
.load_const
.packed_value
= lc
->value
[0].b
;
1310 unreachable("invalid bit_size");
1314 blob_write_uint32(ctx
->blob
, header
.u32
);
1316 if (header
.load_const
.packing
== load_const_full
) {
1317 switch (lc
->def
.bit_size
) {
1319 blob_write_bytes(ctx
->blob
, lc
->value
,
1320 sizeof(*lc
->value
) * lc
->def
.num_components
);
1324 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1325 blob_write_uint32(ctx
->blob
, lc
->value
[i
].u32
);
1329 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1330 blob_write_uint16(ctx
->blob
, lc
->value
[i
].u16
);
1334 assert(lc
->def
.bit_size
<= 8);
1335 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1336 blob_write_uint8(ctx
->blob
, lc
->value
[i
].u8
);
1341 write_add_object(ctx
, &lc
->def
);
1344 static nir_load_const_instr
*
1345 read_load_const(read_ctx
*ctx
, union packed_instr header
)
1347 nir_load_const_instr
*lc
=
1348 nir_load_const_instr_create(ctx
->nir
, header
.load_const
.last_component
+ 1,
1349 decode_bit_size_3bits(header
.load_const
.bit_size
));
1351 switch (header
.load_const
.packing
) {
1352 case load_const_scalar_hi_19bits
:
1353 switch (lc
->def
.bit_size
) {
1355 lc
->value
[0].u64
= (uint64_t)header
.load_const
.packed_value
<< 45;
1358 lc
->value
[0].u32
= (uint64_t)header
.load_const
.packed_value
<< 13;
1361 unreachable("invalid bit_size");
1365 case load_const_scalar_lo_19bits_sext
:
1366 switch (lc
->def
.bit_size
) {
1368 lc
->value
[0].i64
= ((int64_t)header
.load_const
.packed_value
<< 45) >> 45;
1371 lc
->value
[0].i32
= ((int32_t)header
.load_const
.packed_value
<< 13) >> 13;
1374 lc
->value
[0].u16
= header
.load_const
.packed_value
;
1377 lc
->value
[0].u8
= header
.load_const
.packed_value
;
1380 lc
->value
[0].b
= header
.load_const
.packed_value
;
1383 unreachable("invalid bit_size");
1387 case load_const_full
:
1388 switch (lc
->def
.bit_size
) {
1390 blob_copy_bytes(ctx
->blob
, lc
->value
, sizeof(*lc
->value
) * lc
->def
.num_components
);
1394 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1395 lc
->value
[i
].u32
= blob_read_uint32(ctx
->blob
);
1399 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1400 lc
->value
[i
].u16
= blob_read_uint16(ctx
->blob
);
1404 assert(lc
->def
.bit_size
<= 8);
1405 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1406 lc
->value
[i
].u8
= blob_read_uint8(ctx
->blob
);
1412 read_add_object(ctx
, &lc
->def
);
1417 write_ssa_undef(write_ctx
*ctx
, const nir_ssa_undef_instr
*undef
)
1419 assert(undef
->def
.num_components
>= 1 && undef
->def
.num_components
<= 16);
1421 union packed_instr header
;
1424 header
.undef
.instr_type
= undef
->instr
.type
;
1425 header
.undef
.last_component
= undef
->def
.num_components
- 1;
1426 header
.undef
.bit_size
= encode_bit_size_3bits(undef
->def
.bit_size
);
1428 blob_write_uint32(ctx
->blob
, header
.u32
);
1429 write_add_object(ctx
, &undef
->def
);
1432 static nir_ssa_undef_instr
*
1433 read_ssa_undef(read_ctx
*ctx
, union packed_instr header
)
1435 nir_ssa_undef_instr
*undef
=
1436 nir_ssa_undef_instr_create(ctx
->nir
, header
.undef
.last_component
+ 1,
1437 decode_bit_size_3bits(header
.undef
.bit_size
));
1439 read_add_object(ctx
, &undef
->def
);
1443 union packed_tex_data
{
1446 unsigned sampler_dim
:4;
1447 unsigned dest_type
:8;
1448 unsigned coord_components
:3;
1449 unsigned is_array
:1;
1450 unsigned is_shadow
:1;
1451 unsigned is_new_style_shadow
:1;
1452 unsigned component
:2;
1453 unsigned texture_non_uniform
:1;
1454 unsigned sampler_non_uniform
:1;
1455 unsigned unused
:8; /* Mark unused for valgrind. */
1460 write_tex(write_ctx
*ctx
, const nir_tex_instr
*tex
)
1462 assert(tex
->num_srcs
< 16);
1463 assert(tex
->op
< 16);
1465 union packed_instr header
;
1468 header
.tex
.instr_type
= tex
->instr
.type
;
1469 header
.tex
.num_srcs
= tex
->num_srcs
;
1470 header
.tex
.op
= tex
->op
;
1472 write_dest(ctx
, &tex
->dest
, header
, tex
->instr
.type
);
1474 blob_write_uint32(ctx
->blob
, tex
->texture_index
);
1475 blob_write_uint32(ctx
->blob
, tex
->sampler_index
);
1476 if (tex
->op
== nir_texop_tg4
)
1477 blob_write_bytes(ctx
->blob
, tex
->tg4_offsets
, sizeof(tex
->tg4_offsets
));
1479 STATIC_ASSERT(sizeof(union packed_tex_data
) == sizeof(uint32_t));
1480 union packed_tex_data packed
= {
1481 .u
.sampler_dim
= tex
->sampler_dim
,
1482 .u
.dest_type
= tex
->dest_type
,
1483 .u
.coord_components
= tex
->coord_components
,
1484 .u
.is_array
= tex
->is_array
,
1485 .u
.is_shadow
= tex
->is_shadow
,
1486 .u
.is_new_style_shadow
= tex
->is_new_style_shadow
,
1487 .u
.component
= tex
->component
,
1488 .u
.texture_non_uniform
= tex
->texture_non_uniform
,
1489 .u
.sampler_non_uniform
= tex
->sampler_non_uniform
,
1491 blob_write_uint32(ctx
->blob
, packed
.u32
);
1493 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1494 union packed_src src
;
1496 src
.tex
.src_type
= tex
->src
[i
].src_type
;
1497 write_src_full(ctx
, &tex
->src
[i
].src
, src
);
1501 static nir_tex_instr
*
1502 read_tex(read_ctx
*ctx
, union packed_instr header
)
1504 nir_tex_instr
*tex
= nir_tex_instr_create(ctx
->nir
, header
.tex
.num_srcs
);
1506 read_dest(ctx
, &tex
->dest
, &tex
->instr
, header
);
1508 tex
->op
= header
.tex
.op
;
1509 tex
->texture_index
= blob_read_uint32(ctx
->blob
);
1510 tex
->sampler_index
= blob_read_uint32(ctx
->blob
);
1511 if (tex
->op
== nir_texop_tg4
)
1512 blob_copy_bytes(ctx
->blob
, tex
->tg4_offsets
, sizeof(tex
->tg4_offsets
));
1514 union packed_tex_data packed
;
1515 packed
.u32
= blob_read_uint32(ctx
->blob
);
1516 tex
->sampler_dim
= packed
.u
.sampler_dim
;
1517 tex
->dest_type
= packed
.u
.dest_type
;
1518 tex
->coord_components
= packed
.u
.coord_components
;
1519 tex
->is_array
= packed
.u
.is_array
;
1520 tex
->is_shadow
= packed
.u
.is_shadow
;
1521 tex
->is_new_style_shadow
= packed
.u
.is_new_style_shadow
;
1522 tex
->component
= packed
.u
.component
;
1523 tex
->texture_non_uniform
= packed
.u
.texture_non_uniform
;
1524 tex
->sampler_non_uniform
= packed
.u
.sampler_non_uniform
;
1526 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1527 union packed_src src
= read_src(ctx
, &tex
->src
[i
].src
, &tex
->instr
);
1528 tex
->src
[i
].src_type
= src
.tex
.src_type
;
1535 write_phi(write_ctx
*ctx
, const nir_phi_instr
*phi
)
1537 union packed_instr header
;
1540 header
.phi
.instr_type
= phi
->instr
.type
;
1541 header
.phi
.num_srcs
= exec_list_length(&phi
->srcs
);
1543 /* Phi nodes are special, since they may reference SSA definitions and
1544 * basic blocks that don't exist yet. We leave two empty uint32_t's here,
1545 * and then store enough information so that a later fixup pass can fill
1546 * them in correctly.
1548 write_dest(ctx
, &phi
->dest
, header
, phi
->instr
.type
);
1550 nir_foreach_phi_src(src
, phi
) {
1551 assert(src
->src
.is_ssa
);
1552 size_t blob_offset
= blob_reserve_uint32(ctx
->blob
);
1553 ASSERTED
size_t blob_offset2
= blob_reserve_uint32(ctx
->blob
);
1554 assert(blob_offset
+ sizeof(uint32_t) == blob_offset2
);
1555 write_phi_fixup fixup
= {
1556 .blob_offset
= blob_offset
,
1557 .src
= src
->src
.ssa
,
1560 util_dynarray_append(&ctx
->phi_fixups
, write_phi_fixup
, fixup
);
1565 write_fixup_phis(write_ctx
*ctx
)
1567 util_dynarray_foreach(&ctx
->phi_fixups
, write_phi_fixup
, fixup
) {
1568 uint32_t *blob_ptr
= (uint32_t *)(ctx
->blob
->data
+ fixup
->blob_offset
);
1569 blob_ptr
[0] = write_lookup_object(ctx
, fixup
->src
);
1570 blob_ptr
[1] = write_lookup_object(ctx
, fixup
->block
);
1573 util_dynarray_clear(&ctx
->phi_fixups
);
1576 static nir_phi_instr
*
1577 read_phi(read_ctx
*ctx
, nir_block
*blk
, union packed_instr header
)
1579 nir_phi_instr
*phi
= nir_phi_instr_create(ctx
->nir
);
1581 read_dest(ctx
, &phi
->dest
, &phi
->instr
, header
);
1583 /* For similar reasons as before, we just store the index directly into the
1584 * pointer, and let a later pass resolve the phi sources.
1586 * In order to ensure that the copied sources (which are just the indices
1587 * from the blob for now) don't get inserted into the old shader's use-def
1588 * lists, we have to add the phi instruction *before* we set up its
1591 nir_instr_insert_after_block(blk
, &phi
->instr
);
1593 for (unsigned i
= 0; i
< header
.phi
.num_srcs
; i
++) {
1594 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
1596 src
->src
.is_ssa
= true;
1597 src
->src
.ssa
= (nir_ssa_def
*)(uintptr_t) blob_read_uint32(ctx
->blob
);
1598 src
->pred
= (nir_block
*)(uintptr_t) blob_read_uint32(ctx
->blob
);
1600 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
1601 * we have to set the parent_instr manually. It doesn't really matter
1602 * when we do it, so we might as well do it here.
1604 src
->src
.parent_instr
= &phi
->instr
;
1606 /* Stash it in the list of phi sources. We'll walk this list and fix up
1607 * sources at the very end of read_function_impl.
1609 list_add(&src
->src
.use_link
, &ctx
->phi_srcs
);
1611 exec_list_push_tail(&phi
->srcs
, &src
->node
);
1618 read_fixup_phis(read_ctx
*ctx
)
1620 list_for_each_entry_safe(nir_phi_src
, src
, &ctx
->phi_srcs
, src
.use_link
) {
1621 src
->pred
= read_lookup_object(ctx
, (uintptr_t)src
->pred
);
1622 src
->src
.ssa
= read_lookup_object(ctx
, (uintptr_t)src
->src
.ssa
);
1624 /* Remove from this list */
1625 list_del(&src
->src
.use_link
);
1627 list_addtail(&src
->src
.use_link
, &src
->src
.ssa
->uses
);
1629 assert(list_is_empty(&ctx
->phi_srcs
));
1633 write_jump(write_ctx
*ctx
, const nir_jump_instr
*jmp
)
1635 assert(jmp
->type
< 4);
1637 union packed_instr header
;
1640 header
.jump
.instr_type
= jmp
->instr
.type
;
1641 header
.jump
.type
= jmp
->type
;
1643 blob_write_uint32(ctx
->blob
, header
.u32
);
1646 static nir_jump_instr
*
1647 read_jump(read_ctx
*ctx
, union packed_instr header
)
1649 nir_jump_instr
*jmp
= nir_jump_instr_create(ctx
->nir
, header
.jump
.type
);
1654 write_call(write_ctx
*ctx
, const nir_call_instr
*call
)
1656 blob_write_uint32(ctx
->blob
, write_lookup_object(ctx
, call
->callee
));
1658 for (unsigned i
= 0; i
< call
->num_params
; i
++)
1659 write_src(ctx
, &call
->params
[i
]);
1662 static nir_call_instr
*
1663 read_call(read_ctx
*ctx
)
1665 nir_function
*callee
= read_object(ctx
);
1666 nir_call_instr
*call
= nir_call_instr_create(ctx
->nir
, callee
);
1668 for (unsigned i
= 0; i
< call
->num_params
; i
++)
1669 read_src(ctx
, &call
->params
[i
], call
);
1675 write_instr(write_ctx
*ctx
, const nir_instr
*instr
)
1677 /* We have only 4 bits for the instruction type. */
1678 assert(instr
->type
< 16);
1680 switch (instr
->type
) {
1681 case nir_instr_type_alu
:
1682 write_alu(ctx
, nir_instr_as_alu(instr
));
1684 case nir_instr_type_deref
:
1685 write_deref(ctx
, nir_instr_as_deref(instr
));
1687 case nir_instr_type_intrinsic
:
1688 write_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1690 case nir_instr_type_load_const
:
1691 write_load_const(ctx
, nir_instr_as_load_const(instr
));
1693 case nir_instr_type_ssa_undef
:
1694 write_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1696 case nir_instr_type_tex
:
1697 write_tex(ctx
, nir_instr_as_tex(instr
));
1699 case nir_instr_type_phi
:
1700 write_phi(ctx
, nir_instr_as_phi(instr
));
1702 case nir_instr_type_jump
:
1703 write_jump(ctx
, nir_instr_as_jump(instr
));
1705 case nir_instr_type_call
:
1706 blob_write_uint32(ctx
->blob
, instr
->type
);
1707 write_call(ctx
, nir_instr_as_call(instr
));
1709 case nir_instr_type_parallel_copy
:
1710 unreachable("Cannot write parallel copies");
1712 unreachable("bad instr type");
1716 /* Return the number of instructions read. */
1718 read_instr(read_ctx
*ctx
, nir_block
*block
)
1720 STATIC_ASSERT(sizeof(union packed_instr
) == 4);
1721 union packed_instr header
;
1722 header
.u32
= blob_read_uint32(ctx
->blob
);
1725 switch (header
.any
.instr_type
) {
1726 case nir_instr_type_alu
:
1727 for (unsigned i
= 0; i
<= header
.alu
.num_followup_alu_sharing_header
; i
++)
1728 nir_instr_insert_after_block(block
, &read_alu(ctx
, header
)->instr
);
1729 return header
.alu
.num_followup_alu_sharing_header
+ 1;
1730 case nir_instr_type_deref
:
1731 instr
= &read_deref(ctx
, header
)->instr
;
1733 case nir_instr_type_intrinsic
:
1734 instr
= &read_intrinsic(ctx
, header
)->instr
;
1736 case nir_instr_type_load_const
:
1737 instr
= &read_load_const(ctx
, header
)->instr
;
1739 case nir_instr_type_ssa_undef
:
1740 instr
= &read_ssa_undef(ctx
, header
)->instr
;
1742 case nir_instr_type_tex
:
1743 instr
= &read_tex(ctx
, header
)->instr
;
1745 case nir_instr_type_phi
:
1746 /* Phi instructions are a bit of a special case when reading because we
1747 * don't want inserting the instruction to automatically handle use/defs
1748 * for us. Instead, we need to wait until all the blocks/instructions
1749 * are read so that we can set their sources up.
1751 read_phi(ctx
, block
, header
);
1753 case nir_instr_type_jump
:
1754 instr
= &read_jump(ctx
, header
)->instr
;
1756 case nir_instr_type_call
:
1757 instr
= &read_call(ctx
)->instr
;
1759 case nir_instr_type_parallel_copy
:
1760 unreachable("Cannot read parallel copies");
1762 unreachable("bad instr type");
1765 nir_instr_insert_after_block(block
, instr
);
1770 write_block(write_ctx
*ctx
, const nir_block
*block
)
1772 write_add_object(ctx
, block
);
1773 blob_write_uint32(ctx
->blob
, exec_list_length(&block
->instr_list
));
1775 ctx
->last_instr_type
= ~0;
1776 ctx
->last_alu_header_offset
= 0;
1778 nir_foreach_instr(instr
, block
) {
1779 write_instr(ctx
, instr
);
1780 ctx
->last_instr_type
= instr
->type
;
1785 read_block(read_ctx
*ctx
, struct exec_list
*cf_list
)
1787 /* Don't actually create a new block. Just use the one from the tail of
1788 * the list. NIR guarantees that the tail of the list is a block and that
1789 * no two blocks are side-by-side in the IR; It should be empty.
1792 exec_node_data(nir_block
, exec_list_get_tail(cf_list
), cf_node
.node
);
1794 read_add_object(ctx
, block
);
1795 unsigned num_instrs
= blob_read_uint32(ctx
->blob
);
1796 for (unsigned i
= 0; i
< num_instrs
;) {
1797 i
+= read_instr(ctx
, block
);
1802 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
);
1805 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
);
1808 write_if(write_ctx
*ctx
, nir_if
*nif
)
1810 write_src(ctx
, &nif
->condition
);
1812 write_cf_list(ctx
, &nif
->then_list
);
1813 write_cf_list(ctx
, &nif
->else_list
);
1817 read_if(read_ctx
*ctx
, struct exec_list
*cf_list
)
1819 nir_if
*nif
= nir_if_create(ctx
->nir
);
1821 read_src(ctx
, &nif
->condition
, nif
);
1823 nir_cf_node_insert_end(cf_list
, &nif
->cf_node
);
1825 read_cf_list(ctx
, &nif
->then_list
);
1826 read_cf_list(ctx
, &nif
->else_list
);
1830 write_loop(write_ctx
*ctx
, nir_loop
*loop
)
1832 write_cf_list(ctx
, &loop
->body
);
1836 read_loop(read_ctx
*ctx
, struct exec_list
*cf_list
)
1838 nir_loop
*loop
= nir_loop_create(ctx
->nir
);
1840 nir_cf_node_insert_end(cf_list
, &loop
->cf_node
);
1842 read_cf_list(ctx
, &loop
->body
);
1846 write_cf_node(write_ctx
*ctx
, nir_cf_node
*cf
)
1848 blob_write_uint32(ctx
->blob
, cf
->type
);
1851 case nir_cf_node_block
:
1852 write_block(ctx
, nir_cf_node_as_block(cf
));
1854 case nir_cf_node_if
:
1855 write_if(ctx
, nir_cf_node_as_if(cf
));
1857 case nir_cf_node_loop
:
1858 write_loop(ctx
, nir_cf_node_as_loop(cf
));
1861 unreachable("bad cf type");
1866 read_cf_node(read_ctx
*ctx
, struct exec_list
*list
)
1868 nir_cf_node_type type
= blob_read_uint32(ctx
->blob
);
1871 case nir_cf_node_block
:
1872 read_block(ctx
, list
);
1874 case nir_cf_node_if
:
1877 case nir_cf_node_loop
:
1878 read_loop(ctx
, list
);
1881 unreachable("bad cf type");
1886 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
)
1888 blob_write_uint32(ctx
->blob
, exec_list_length(cf_list
));
1889 foreach_list_typed(nir_cf_node
, cf
, node
, cf_list
) {
1890 write_cf_node(ctx
, cf
);
1895 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
)
1897 uint32_t num_cf_nodes
= blob_read_uint32(ctx
->blob
);
1898 for (unsigned i
= 0; i
< num_cf_nodes
; i
++)
1899 read_cf_node(ctx
, cf_list
);
1903 write_function_impl(write_ctx
*ctx
, const nir_function_impl
*fi
)
1905 blob_write_uint8(ctx
->blob
, fi
->structured
);
1907 write_var_list(ctx
, &fi
->locals
);
1908 write_reg_list(ctx
, &fi
->registers
);
1909 blob_write_uint32(ctx
->blob
, fi
->reg_alloc
);
1911 write_cf_list(ctx
, &fi
->body
);
1912 write_fixup_phis(ctx
);
1915 static nir_function_impl
*
1916 read_function_impl(read_ctx
*ctx
, nir_function
*fxn
)
1918 nir_function_impl
*fi
= nir_function_impl_create_bare(ctx
->nir
);
1921 fi
->structured
= blob_read_uint8(ctx
->blob
);
1923 read_var_list(ctx
, &fi
->locals
);
1924 read_reg_list(ctx
, &fi
->registers
);
1925 fi
->reg_alloc
= blob_read_uint32(ctx
->blob
);
1927 read_cf_list(ctx
, &fi
->body
);
1928 read_fixup_phis(ctx
);
1930 fi
->valid_metadata
= 0;
1936 write_function(write_ctx
*ctx
, const nir_function
*fxn
)
1938 uint32_t flags
= fxn
->is_entrypoint
;
1943 blob_write_uint32(ctx
->blob
, flags
);
1945 blob_write_string(ctx
->blob
, fxn
->name
);
1947 write_add_object(ctx
, fxn
);
1949 blob_write_uint32(ctx
->blob
, fxn
->num_params
);
1950 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1952 ((uint32_t)fxn
->params
[i
].num_components
) |
1953 ((uint32_t)fxn
->params
[i
].bit_size
) << 8;
1954 blob_write_uint32(ctx
->blob
, val
);
1957 /* At first glance, it looks like we should write the function_impl here.
1958 * However, call instructions need to be able to reference at least the
1959 * function and those will get processed as we write the function_impls.
1960 * We stop here and write function_impls as a second pass.
1965 read_function(read_ctx
*ctx
)
1967 uint32_t flags
= blob_read_uint32(ctx
->blob
);
1968 bool has_name
= flags
& 0x2;
1969 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
1971 nir_function
*fxn
= nir_function_create(ctx
->nir
, name
);
1973 read_add_object(ctx
, fxn
);
1975 fxn
->num_params
= blob_read_uint32(ctx
->blob
);
1976 fxn
->params
= ralloc_array(fxn
, nir_parameter
, fxn
->num_params
);
1977 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1978 uint32_t val
= blob_read_uint32(ctx
->blob
);
1979 fxn
->params
[i
].num_components
= val
& 0xff;
1980 fxn
->params
[i
].bit_size
= (val
>> 8) & 0xff;
1983 fxn
->is_entrypoint
= flags
& 0x1;
1985 fxn
->impl
= NIR_SERIALIZE_FUNC_HAS_IMPL
;
1989 * Serialize NIR into a binary blob.
1991 * \param strip Don't serialize information only useful for debugging,
1992 * such as variable names, making cache hits from similar
1993 * shaders more likely.
1996 nir_serialize(struct blob
*blob
, const nir_shader
*nir
, bool strip
)
1998 write_ctx ctx
= {0};
1999 ctx
.remap_table
= _mesa_pointer_hash_table_create(NULL
);
2003 util_dynarray_init(&ctx
.phi_fixups
, NULL
);
2005 size_t idx_size_offset
= blob_reserve_uint32(blob
);
2007 struct shader_info info
= nir
->info
;
2008 uint32_t strings
= 0;
2009 if (!strip
&& info
.name
)
2011 if (!strip
&& info
.label
)
2013 blob_write_uint32(blob
, strings
);
2014 if (!strip
&& info
.name
)
2015 blob_write_string(blob
, info
.name
);
2016 if (!strip
&& info
.label
)
2017 blob_write_string(blob
, info
.label
);
2018 info
.name
= info
.label
= NULL
;
2019 blob_write_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
2021 write_var_list(&ctx
, &nir
->variables
);
2023 blob_write_uint32(blob
, nir
->num_inputs
);
2024 blob_write_uint32(blob
, nir
->num_uniforms
);
2025 blob_write_uint32(blob
, nir
->num_outputs
);
2026 blob_write_uint32(blob
, nir
->shared_size
);
2027 blob_write_uint32(blob
, nir
->scratch_size
);
2029 blob_write_uint32(blob
, exec_list_length(&nir
->functions
));
2030 nir_foreach_function(fxn
, nir
) {
2031 write_function(&ctx
, fxn
);
2034 nir_foreach_function(fxn
, nir
) {
2036 write_function_impl(&ctx
, fxn
->impl
);
2039 blob_write_uint32(blob
, nir
->constant_data_size
);
2040 if (nir
->constant_data_size
> 0)
2041 blob_write_bytes(blob
, nir
->constant_data
, nir
->constant_data_size
);
2043 *(uint32_t *)(blob
->data
+ idx_size_offset
) = ctx
.next_idx
;
2045 _mesa_hash_table_destroy(ctx
.remap_table
, NULL
);
2046 util_dynarray_fini(&ctx
.phi_fixups
);
2050 nir_deserialize(void *mem_ctx
,
2051 const struct nir_shader_compiler_options
*options
,
2052 struct blob_reader
*blob
)
2056 list_inithead(&ctx
.phi_srcs
);
2057 ctx
.idx_table_len
= blob_read_uint32(blob
);
2058 ctx
.idx_table
= calloc(ctx
.idx_table_len
, sizeof(uintptr_t));
2060 uint32_t strings
= blob_read_uint32(blob
);
2061 char *name
= (strings
& 0x1) ? blob_read_string(blob
) : NULL
;
2062 char *label
= (strings
& 0x2) ? blob_read_string(blob
) : NULL
;
2064 struct shader_info info
;
2065 blob_copy_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
2067 ctx
.nir
= nir_shader_create(mem_ctx
, info
.stage
, options
, NULL
);
2069 info
.name
= name
? ralloc_strdup(ctx
.nir
, name
) : NULL
;
2070 info
.label
= label
? ralloc_strdup(ctx
.nir
, label
) : NULL
;
2072 ctx
.nir
->info
= info
;
2074 read_var_list(&ctx
, &ctx
.nir
->variables
);
2076 ctx
.nir
->num_inputs
= blob_read_uint32(blob
);
2077 ctx
.nir
->num_uniforms
= blob_read_uint32(blob
);
2078 ctx
.nir
->num_outputs
= blob_read_uint32(blob
);
2079 ctx
.nir
->shared_size
= blob_read_uint32(blob
);
2080 ctx
.nir
->scratch_size
= blob_read_uint32(blob
);
2082 unsigned num_functions
= blob_read_uint32(blob
);
2083 for (unsigned i
= 0; i
< num_functions
; i
++)
2084 read_function(&ctx
);
2086 nir_foreach_function(fxn
, ctx
.nir
) {
2087 if (fxn
->impl
== NIR_SERIALIZE_FUNC_HAS_IMPL
)
2088 fxn
->impl
= read_function_impl(&ctx
, fxn
);
2091 ctx
.nir
->constant_data_size
= blob_read_uint32(blob
);
2092 if (ctx
.nir
->constant_data_size
> 0) {
2093 ctx
.nir
->constant_data
=
2094 ralloc_size(ctx
.nir
, ctx
.nir
->constant_data_size
);
2095 blob_copy_bytes(blob
, ctx
.nir
->constant_data
,
2096 ctx
.nir
->constant_data_size
);
2099 free(ctx
.idx_table
);
2105 nir_shader_serialize_deserialize(nir_shader
*shader
)
2107 const struct nir_shader_compiler_options
*options
= shader
->options
;
2111 nir_serialize(&writer
, shader
, false);
2113 /* Delete all of dest's ralloc children but leave dest alone */
2114 void *dead_ctx
= ralloc_context(NULL
);
2115 ralloc_adopt(dead_ctx
, shader
);
2116 ralloc_free(dead_ctx
);
2118 dead_ctx
= ralloc_context(NULL
);
2120 struct blob_reader reader
;
2121 blob_reader_init(&reader
, writer
.data
, writer
.size
);
2122 nir_shader
*copy
= nir_deserialize(dead_ctx
, options
, &reader
);
2124 blob_finish(&writer
);
2126 nir_shader_replace(shader
, copy
);
2127 ralloc_free(dead_ctx
);