2 * Copyright © 2018 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "nir_xfb_info.h"
26 #include <util/u_math.h>
29 add_var_xfb_outputs(nir_xfb_info
*xfb
,
34 const struct glsl_type
*type
)
36 /* If this type contains a 64-bit value, align to 8 bytes */
37 if (glsl_type_contains_64bit(type
))
38 *offset
= ALIGN_POT(*offset
, 8);
40 if (glsl_type_is_array_or_matrix(type
) && !var
->data
.compact
) {
41 unsigned length
= glsl_get_length(type
);
42 const struct glsl_type
*child_type
= glsl_get_array_element(type
);
43 for (unsigned i
= 0; i
< length
; i
++)
44 add_var_xfb_outputs(xfb
, var
, buffer
, location
, offset
, child_type
);
45 } else if (glsl_type_is_struct_or_ifc(type
)) {
46 unsigned length
= glsl_get_length(type
);
47 for (unsigned i
= 0; i
< length
; i
++) {
48 const struct glsl_type
*child_type
= glsl_get_struct_field(type
, i
);
49 add_var_xfb_outputs(xfb
, var
, buffer
, location
, offset
, child_type
);
52 assert(buffer
< NIR_MAX_XFB_BUFFERS
);
53 if (xfb
->buffers_written
& (1 << buffer
)) {
54 assert(xfb
->strides
[buffer
] == var
->data
.xfb_stride
);
55 assert(xfb
->buffer_to_stream
[buffer
] == var
->data
.stream
);
57 xfb
->buffers_written
|= (1 << buffer
);
58 xfb
->strides
[buffer
] = var
->data
.xfb_stride
;
59 xfb
->buffer_to_stream
[buffer
] = var
->data
.stream
;
62 assert(var
->data
.stream
< NIR_MAX_XFB_STREAMS
);
63 xfb
->streams_written
|= (1 << var
->data
.stream
);
66 if (var
->data
.compact
) {
67 /* This only happens for clip/cull which are float arrays */
68 assert(glsl_without_array(type
) == glsl_float_type());
69 assert(var
->data
.location
== VARYING_SLOT_CLIP_DIST0
||
70 var
->data
.location
== VARYING_SLOT_CLIP_DIST1
);
71 comp_slots
= glsl_get_length(type
);
73 comp_slots
= glsl_get_component_slots(type
);
75 UNUSED
unsigned attrib_slots
= DIV_ROUND_UP(comp_slots
, 4);
76 assert(attrib_slots
== glsl_count_attribute_slots(type
, false));
78 /* Ensure that we don't have, for instance, a dvec2 with a
79 * location_frac of 2 which would make it crass a location boundary
80 * even though it fits in a single slot. However, you can have a
81 * dvec3 which crosses the slot boundary with a location_frac of 2.
83 assert(DIV_ROUND_UP(var
->data
.location_frac
+ comp_slots
, 4) ==
87 assert(var
->data
.location_frac
+ comp_slots
<= 8);
88 uint8_t comp_mask
= ((1 << comp_slots
) - 1) << var
->data
.location_frac
;
91 nir_xfb_output_info
*output
= &xfb
->outputs
[xfb
->output_count
++];
93 output
->buffer
= buffer
;
94 output
->offset
= *offset
;
95 output
->location
= *location
;
96 output
->component_mask
= comp_mask
& 0xf;
98 *offset
+= util_bitcount(output
->component_mask
) * 4;
106 compare_xfb_output_offsets(const void *_a
, const void *_b
)
108 const nir_xfb_output_info
*a
= _a
, *b
= _b
;
109 return a
->offset
- b
->offset
;
113 nir_gather_xfb_info(const nir_shader
*shader
, void *mem_ctx
)
115 assert(shader
->info
.stage
== MESA_SHADER_VERTEX
||
116 shader
->info
.stage
== MESA_SHADER_TESS_EVAL
||
117 shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
119 /* Compute the number of outputs we have. This is simply the number of
120 * cumulative locations consumed by all the variables. If a location is
121 * represented by multiple variables, then they each count separately in
122 * number of outputs. This is only an estimate as some variables may have
123 * an xfb_buffer but not an output so it may end up larger than we need but
124 * it should be good enough for allocation.
126 unsigned num_outputs
= 0;
127 nir_foreach_variable(var
, &shader
->outputs
) {
128 if (var
->data
.explicit_xfb_buffer
)
129 num_outputs
+= glsl_count_attribute_slots(var
->type
, false);
131 if (num_outputs
== 0)
134 nir_xfb_info
*xfb
= rzalloc_size(mem_ctx
, nir_xfb_info_size(num_outputs
));
136 /* Walk the list of outputs and add them to the array */
137 nir_foreach_variable(var
, &shader
->outputs
) {
138 if (!var
->data
.explicit_xfb_buffer
)
141 unsigned location
= var
->data
.location
;
143 /* In order to know if we have a array of blocks can't be done just by
144 * checking if we have an interface type and is an array, because due
145 * splitting we could end on a case were we received a split struct
146 * that contains an array.
148 bool is_array_block
= var
->interface_type
!= NULL
&&
149 glsl_type_is_array(var
->type
) &&
150 glsl_without_array(var
->type
) == glsl_get_bare_type(var
->interface_type
);
152 if (var
->data
.explicit_offset
&& !is_array_block
) {
153 unsigned offset
= var
->data
.offset
;
154 add_var_xfb_outputs(xfb
, var
, var
->data
.xfb_buffer
,
155 &location
, &offset
, var
->type
);
156 } else if (is_array_block
) {
157 assert(glsl_type_is_struct_or_ifc(var
->interface_type
));
159 unsigned aoa_size
= glsl_get_aoa_size(var
->type
);
160 const struct glsl_type
*itype
= var
->interface_type
;
161 unsigned nfields
= glsl_get_length(itype
);
162 for (unsigned b
= 0; b
< aoa_size
; b
++) {
163 for (unsigned f
= 0; f
< nfields
; f
++) {
164 int foffset
= glsl_get_struct_field_offset(itype
, f
);
165 const struct glsl_type
*ftype
= glsl_get_struct_field(itype
, f
);
167 location
+= glsl_count_attribute_slots(ftype
, false);
171 unsigned offset
= foffset
;
172 add_var_xfb_outputs(xfb
, var
, var
->data
.xfb_buffer
+ b
,
173 &location
, &offset
, ftype
);
179 /* Everything is easier in the state setup code if the list is sorted in
180 * order of output offset.
182 qsort(xfb
->outputs
, xfb
->output_count
, sizeof(xfb
->outputs
[0]),
183 compare_xfb_output_offsets
);
186 /* Finally, do a sanity check */
187 unsigned max_offset
[NIR_MAX_XFB_BUFFERS
] = {0};
188 for (unsigned i
= 0; i
< xfb
->output_count
; i
++) {
189 assert(xfb
->outputs
[i
].offset
>= max_offset
[xfb
->outputs
[i
].buffer
]);
190 assert(xfb
->outputs
[i
].component_mask
!= 0);
191 unsigned slots
= util_bitcount(xfb
->outputs
[i
].component_mask
);
192 max_offset
[xfb
->outputs
[i
].buffer
] = xfb
->outputs
[i
].offset
+ slots
* 4;