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 (glsl_type_is_array(type
) || glsl_type_is_matrix(type
)) {
37 unsigned length
= glsl_get_length(type
);
38 const struct glsl_type
*child_type
= glsl_get_array_element(type
);
39 for (unsigned i
= 0; i
< length
; i
++)
40 add_var_xfb_outputs(xfb
, var
, buffer
, location
, offset
, child_type
);
41 } else if (glsl_type_is_struct(type
)) {
42 unsigned length
= glsl_get_length(type
);
43 for (unsigned i
= 0; i
< length
; i
++) {
44 const struct glsl_type
*child_type
= glsl_get_struct_field(type
, i
);
45 add_var_xfb_outputs(xfb
, var
, buffer
, location
, offset
, child_type
);
48 assert(buffer
< NIR_MAX_XFB_BUFFERS
);
49 if (xfb
->buffers_written
& (1 << buffer
)) {
50 assert(xfb
->strides
[buffer
] == var
->data
.xfb_stride
);
51 assert(xfb
->buffer_to_stream
[buffer
] == var
->data
.stream
);
53 xfb
->buffers_written
|= (1 << buffer
);
54 xfb
->strides
[buffer
] = var
->data
.xfb_stride
;
55 xfb
->buffer_to_stream
[buffer
] = var
->data
.stream
;
58 assert(var
->data
.stream
< NIR_MAX_XFB_STREAMS
);
59 xfb
->streams_written
|= (1 << var
->data
.stream
);
61 unsigned comp_slots
= glsl_get_component_slots(type
);
62 unsigned attrib_slots
= DIV_ROUND_UP(comp_slots
, 4);
63 assert(attrib_slots
== glsl_count_attribute_slots(type
, false));
65 /* Ensure that we don't have, for instance, a dvec2 with a location_frac
66 * of 2 which would make it crass a location boundary even though it
67 * fits in a single slot. However, you can have a dvec3 which crosses
68 * the slot boundary with a location_frac of 2.
70 assert(DIV_ROUND_UP(var
->data
.location_frac
+ comp_slots
, 4) == attrib_slots
);
72 assert(var
->data
.location_frac
+ comp_slots
<= 8);
73 uint8_t comp_mask
= ((1 << comp_slots
) - 1) << var
->data
.location_frac
;
75 assert(attrib_slots
<= 2);
76 for (unsigned s
= 0; s
< attrib_slots
; s
++) {
77 nir_xfb_output_info
*output
= &xfb
->outputs
[xfb
->output_count
++];
79 output
->buffer
= buffer
;
80 output
->offset
= *offset
+ s
* 16;
81 output
->location
= *location
;
82 output
->component_mask
= (comp_mask
>> (s
* 4)) & 0xf;
86 *offset
+= comp_slots
* 4;
91 compare_xfb_output_offsets(const void *_a
, const void *_b
)
93 const nir_xfb_output_info
*a
= _a
, *b
= _b
;
94 return a
->offset
- b
->offset
;
98 nir_gather_xfb_info(const nir_shader
*shader
, void *mem_ctx
)
100 assert(shader
->info
.stage
== MESA_SHADER_VERTEX
||
101 shader
->info
.stage
== MESA_SHADER_TESS_EVAL
||
102 shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
104 /* Compute the number of outputs we have. This is simply the number of
105 * cumulative locations consumed by all the variables. If a location is
106 * represented by multiple variables, then they each count separately in
107 * number of outputs. This is only an estimate as some variables may have
108 * an xfb_buffer but not an output so it may end up larger than we need but
109 * it should be good enough for allocation.
111 unsigned num_outputs
= 0;
112 nir_foreach_variable(var
, &shader
->outputs
) {
113 if (var
->data
.explicit_xfb_buffer
)
114 num_outputs
+= glsl_count_attribute_slots(var
->type
, false);
116 if (num_outputs
== 0)
119 nir_xfb_info
*xfb
= rzalloc_size(mem_ctx
, nir_xfb_info_size(num_outputs
));
121 /* Walk the list of outputs and add them to the array */
122 nir_foreach_variable(var
, &shader
->outputs
) {
123 if (!var
->data
.explicit_xfb_buffer
)
126 unsigned location
= var
->data
.location
;
128 /* In order to know if we have a array of blocks can't be done just by
129 * checking if we have an interface type and is an array, because due
130 * splitting we could end on a case were we received a split struct
131 * that contains an array.
133 bool is_array_block
= var
->interface_type
!= NULL
&&
134 glsl_type_is_array(var
->type
) &&
135 glsl_without_array(var
->type
) == glsl_get_bare_type(var
->interface_type
);
137 if (var
->data
.explicit_offset
&& !is_array_block
) {
138 unsigned offset
= var
->data
.offset
;
139 add_var_xfb_outputs(xfb
, var
, var
->data
.xfb_buffer
,
140 &location
, &offset
, var
->type
);
141 } else if (is_array_block
) {
142 assert(glsl_type_is_struct(var
->interface_type
));
144 unsigned aoa_size
= glsl_get_aoa_size(var
->type
);
145 const struct glsl_type
*itype
= var
->interface_type
;
146 unsigned nfields
= glsl_get_length(itype
);
147 for (unsigned b
= 0; b
< aoa_size
; b
++) {
148 for (unsigned f
= 0; f
< nfields
; f
++) {
149 int foffset
= glsl_get_struct_field_offset(itype
, f
);
150 const struct glsl_type
*ftype
= glsl_get_struct_field(itype
, f
);
152 location
+= glsl_count_attribute_slots(ftype
, false);
156 unsigned offset
= foffset
;
157 add_var_xfb_outputs(xfb
, var
, var
->data
.xfb_buffer
+ b
,
158 &location
, &offset
, ftype
);
164 /* Everything is easier in the state setup code if the list is sorted in
165 * order of output offset.
167 qsort(xfb
->outputs
, xfb
->output_count
, sizeof(xfb
->outputs
[0]),
168 compare_xfb_output_offsets
);
171 /* Finally, do a sanity check */
172 unsigned max_offset
[NIR_MAX_XFB_BUFFERS
] = {0};
173 for (unsigned i
= 0; i
< xfb
->output_count
; i
++) {
174 assert(xfb
->outputs
[i
].offset
>= max_offset
[xfb
->outputs
[i
].buffer
]);
175 assert(xfb
->outputs
[i
].component_mask
!= 0);
176 unsigned slots
= util_bitcount(xfb
->outputs
[i
].component_mask
);
177 max_offset
[xfb
->outputs
[i
].buffer
] = xfb
->outputs
[i
].offset
+ slots
* 4;