2018-10-05 09:13:25 -05:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nir_xfb_info.h"
|
|
|
|
|
|
|
|
#include <util/u_math.h>
|
|
|
|
|
2019-01-10 15:04:37 +01:00
|
|
|
static void
|
|
|
|
add_var_xfb_varying(nir_xfb_info *xfb,
|
2019-03-14 11:02:52 +01:00
|
|
|
nir_xfb_varyings_info *varyings,
|
2019-01-10 15:04:37 +01:00
|
|
|
nir_variable *var,
|
|
|
|
unsigned offset,
|
|
|
|
const struct glsl_type *type)
|
|
|
|
{
|
2019-03-14 11:02:52 +01:00
|
|
|
if (varyings == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nir_xfb_varying_info *varying = &varyings->varyings[varyings->varying_count++];
|
2019-01-10 15:04:37 +01:00
|
|
|
|
|
|
|
varying->type = type;
|
|
|
|
varying->buffer = var->data.xfb_buffer;
|
|
|
|
varying->offset = offset;
|
|
|
|
xfb->buffers[var->data.xfb_buffer].varying_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-09 18:19:45 +01:00
|
|
|
static nir_xfb_info *
|
2019-03-14 11:02:52 +01:00
|
|
|
nir_xfb_info_create(void *mem_ctx, uint16_t output_count)
|
2019-01-09 18:19:45 +01:00
|
|
|
{
|
2019-03-14 11:02:52 +01:00
|
|
|
return rzalloc_size(mem_ctx, nir_xfb_info_size(output_count));
|
|
|
|
}
|
2019-01-09 18:19:45 +01:00
|
|
|
|
2019-03-14 11:02:52 +01:00
|
|
|
static size_t
|
|
|
|
nir_xfb_varyings_info_size(uint16_t varying_count)
|
|
|
|
{
|
|
|
|
return sizeof(nir_xfb_info) + sizeof(nir_xfb_varying_info) * varying_count;
|
|
|
|
}
|
2019-01-09 18:19:45 +01:00
|
|
|
|
2019-03-14 11:02:52 +01:00
|
|
|
static nir_xfb_varyings_info *
|
|
|
|
nir_xfb_varyings_info_create(void *mem_ctx, uint16_t varying_count)
|
|
|
|
{
|
|
|
|
return rzalloc_size(mem_ctx, nir_xfb_varyings_info_size(varying_count));
|
2019-01-09 18:19:45 +01:00
|
|
|
}
|
|
|
|
|
2018-10-05 09:13:25 -05:00
|
|
|
static void
|
|
|
|
add_var_xfb_outputs(nir_xfb_info *xfb,
|
2019-03-14 11:02:52 +01:00
|
|
|
nir_xfb_varyings_info *varyings,
|
2018-10-05 09:13:25 -05:00
|
|
|
nir_variable *var,
|
2019-01-08 18:22:16 -06:00
|
|
|
unsigned buffer,
|
2018-10-05 09:13:25 -05:00
|
|
|
unsigned *location,
|
|
|
|
unsigned *offset,
|
2019-01-10 15:04:37 +01:00
|
|
|
const struct glsl_type *type,
|
|
|
|
bool varying_added)
|
2018-10-05 09:13:25 -05:00
|
|
|
{
|
2019-02-12 12:49:08 -06:00
|
|
|
/* If this type contains a 64-bit value, align to 8 bytes */
|
|
|
|
if (glsl_type_contains_64bit(type))
|
|
|
|
*offset = ALIGN_POT(*offset, 8);
|
|
|
|
|
2019-02-13 16:34:27 -06:00
|
|
|
if (glsl_type_is_array_or_matrix(type) && !var->data.compact) {
|
2018-10-05 09:13:25 -05:00
|
|
|
unsigned length = glsl_get_length(type);
|
2019-01-10 15:04:37 +01:00
|
|
|
|
2018-10-05 09:13:25 -05:00
|
|
|
const struct glsl_type *child_type = glsl_get_array_element(type);
|
2019-01-10 15:04:37 +01:00
|
|
|
if (!glsl_type_is_array(child_type) &&
|
|
|
|
!glsl_type_is_struct(child_type)) {
|
|
|
|
|
2019-03-14 11:02:52 +01:00
|
|
|
add_var_xfb_varying(xfb, varyings, var, *offset, type);
|
2019-01-10 15:04:37 +01:00
|
|
|
varying_added = true;
|
|
|
|
}
|
|
|
|
|
2018-10-05 09:13:25 -05:00
|
|
|
for (unsigned i = 0; i < length; i++)
|
2019-03-14 11:02:52 +01:00
|
|
|
add_var_xfb_outputs(xfb, varyings, var, buffer, location, offset,
|
|
|
|
child_type, varying_added);
|
2019-03-05 16:07:12 +11:00
|
|
|
} else if (glsl_type_is_struct_or_ifc(type)) {
|
2018-10-05 09:13:25 -05:00
|
|
|
unsigned length = glsl_get_length(type);
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
|
|
|
const struct glsl_type *child_type = glsl_get_struct_field(type, i);
|
2019-03-14 11:02:52 +01:00
|
|
|
add_var_xfb_outputs(xfb, varyings, var, buffer, location, offset,
|
|
|
|
child_type, varying_added);
|
2018-10-05 09:13:25 -05:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-08 18:22:16 -06:00
|
|
|
assert(buffer < NIR_MAX_XFB_BUFFERS);
|
|
|
|
if (xfb->buffers_written & (1 << buffer)) {
|
2019-01-09 18:19:45 +01:00
|
|
|
assert(xfb->buffers[buffer].stride == var->data.xfb_stride);
|
2019-01-08 18:22:16 -06:00
|
|
|
assert(xfb->buffer_to_stream[buffer] == var->data.stream);
|
2018-10-05 09:13:25 -05:00
|
|
|
} else {
|
2019-01-08 18:22:16 -06:00
|
|
|
xfb->buffers_written |= (1 << buffer);
|
2019-01-09 18:19:45 +01:00
|
|
|
xfb->buffers[buffer].stride = var->data.xfb_stride;
|
2019-01-08 18:22:16 -06:00
|
|
|
xfb->buffer_to_stream[buffer] = var->data.stream;
|
2018-10-05 09:13:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(var->data.stream < NIR_MAX_XFB_STREAMS);
|
|
|
|
xfb->streams_written |= (1 << var->data.stream);
|
|
|
|
|
2019-02-13 16:34:27 -06:00
|
|
|
unsigned comp_slots;
|
|
|
|
if (var->data.compact) {
|
|
|
|
/* This only happens for clip/cull which are float arrays */
|
|
|
|
assert(glsl_without_array(type) == glsl_float_type());
|
|
|
|
assert(var->data.location == VARYING_SLOT_CLIP_DIST0 ||
|
|
|
|
var->data.location == VARYING_SLOT_CLIP_DIST1);
|
|
|
|
comp_slots = glsl_get_length(type);
|
|
|
|
} else {
|
|
|
|
comp_slots = glsl_get_component_slots(type);
|
|
|
|
|
2019-02-21 09:44:33 -06:00
|
|
|
UNUSED unsigned attrib_slots = DIV_ROUND_UP(comp_slots, 4);
|
2019-02-13 16:34:27 -06:00
|
|
|
assert(attrib_slots == glsl_count_attribute_slots(type, false));
|
2018-10-05 09:13:25 -05:00
|
|
|
|
2019-02-13 16:34:27 -06:00
|
|
|
/* Ensure that we don't have, for instance, a dvec2 with a
|
|
|
|
* location_frac of 2 which would make it crass a location boundary
|
|
|
|
* even though it fits in a single slot. However, you can have a
|
|
|
|
* dvec3 which crosses the slot boundary with a location_frac of 2.
|
|
|
|
*/
|
|
|
|
assert(DIV_ROUND_UP(var->data.location_frac + comp_slots, 4) ==
|
|
|
|
attrib_slots);
|
|
|
|
}
|
2018-10-05 09:13:25 -05:00
|
|
|
|
|
|
|
assert(var->data.location_frac + comp_slots <= 8);
|
|
|
|
uint8_t comp_mask = ((1 << comp_slots) - 1) << var->data.location_frac;
|
2018-11-06 18:10:01 +01:00
|
|
|
unsigned comp_offset = var->data.location_frac;
|
2018-10-05 09:13:25 -05:00
|
|
|
|
2019-01-10 15:04:37 +01:00
|
|
|
if (!varying_added) {
|
2019-03-14 11:02:52 +01:00
|
|
|
add_var_xfb_varying(xfb, varyings, var, *offset, type);
|
2019-01-10 15:04:37 +01:00
|
|
|
}
|
2019-01-09 18:19:45 +01:00
|
|
|
|
2019-02-13 16:22:37 -06:00
|
|
|
while (comp_mask) {
|
2018-10-05 09:13:25 -05:00
|
|
|
nir_xfb_output_info *output = &xfb->outputs[xfb->output_count++];
|
|
|
|
|
2019-01-08 18:22:16 -06:00
|
|
|
output->buffer = buffer;
|
2019-02-13 16:22:37 -06:00
|
|
|
output->offset = *offset;
|
2018-10-05 09:13:25 -05:00
|
|
|
output->location = *location;
|
2019-02-13 16:22:37 -06:00
|
|
|
output->component_mask = comp_mask & 0xf;
|
2018-11-06 18:10:01 +01:00
|
|
|
output->component_offset = comp_offset;
|
2018-10-05 09:13:25 -05:00
|
|
|
|
2019-02-13 16:22:37 -06:00
|
|
|
*offset += util_bitcount(output->component_mask) * 4;
|
2018-10-05 09:13:25 -05:00
|
|
|
(*location)++;
|
2019-02-13 16:22:37 -06:00
|
|
|
comp_mask >>= 4;
|
2018-11-06 18:10:01 +01:00
|
|
|
comp_offset = 0;
|
2018-10-05 09:13:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:42:49 +01:00
|
|
|
static int
|
|
|
|
compare_xfb_varying_offsets(const void *_a, const void *_b)
|
|
|
|
{
|
|
|
|
const nir_xfb_varying_info *a = _a, *b = _b;
|
|
|
|
|
|
|
|
if (a->buffer != b->buffer)
|
|
|
|
return a->buffer - b->buffer;
|
|
|
|
|
|
|
|
return a->offset - b->offset;
|
|
|
|
}
|
|
|
|
|
2018-10-05 09:13:25 -05:00
|
|
|
static int
|
|
|
|
compare_xfb_output_offsets(const void *_a, const void *_b)
|
|
|
|
{
|
|
|
|
const nir_xfb_output_info *a = _a, *b = _b;
|
2019-03-07 17:42:49 +01:00
|
|
|
|
2018-10-05 09:13:25 -05:00
|
|
|
return a->offset - b->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
nir_xfb_info *
|
|
|
|
nir_gather_xfb_info(const nir_shader *shader, void *mem_ctx)
|
2019-03-14 11:02:52 +01:00
|
|
|
{
|
|
|
|
return nir_gather_xfb_info_with_varyings(shader, mem_ctx, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
nir_xfb_info *
|
|
|
|
nir_gather_xfb_info_with_varyings(const nir_shader *shader,
|
|
|
|
void *mem_ctx,
|
|
|
|
nir_xfb_varyings_info **varyings_info_out)
|
2018-10-05 09:13:25 -05:00
|
|
|
{
|
|
|
|
assert(shader->info.stage == MESA_SHADER_VERTEX ||
|
|
|
|
shader->info.stage == MESA_SHADER_TESS_EVAL ||
|
|
|
|
shader->info.stage == MESA_SHADER_GEOMETRY);
|
|
|
|
|
|
|
|
/* Compute the number of outputs we have. This is simply the number of
|
|
|
|
* cumulative locations consumed by all the variables. If a location is
|
|
|
|
* represented by multiple variables, then they each count separately in
|
2019-01-08 18:22:16 -06:00
|
|
|
* number of outputs. This is only an estimate as some variables may have
|
|
|
|
* an xfb_buffer but not an output so it may end up larger than we need but
|
|
|
|
* it should be good enough for allocation.
|
2018-10-05 09:13:25 -05:00
|
|
|
*/
|
|
|
|
unsigned num_outputs = 0;
|
2019-01-09 18:19:45 +01:00
|
|
|
unsigned num_varyings = 0;
|
2019-03-14 11:02:52 +01:00
|
|
|
nir_xfb_varyings_info *varyings_info = NULL;
|
2018-10-05 09:13:25 -05:00
|
|
|
nir_foreach_variable(var, &shader->outputs) {
|
2019-01-09 18:19:45 +01:00
|
|
|
if (var->data.explicit_xfb_buffer) {
|
2018-10-05 09:13:25 -05:00
|
|
|
num_outputs += glsl_count_attribute_slots(var->type, false);
|
2019-01-09 18:19:45 +01:00
|
|
|
num_varyings += glsl_varying_count(var->type);
|
|
|
|
}
|
2018-10-05 09:13:25 -05:00
|
|
|
}
|
2019-01-09 18:19:45 +01:00
|
|
|
if (num_outputs == 0 || num_varyings == 0)
|
2018-10-05 09:13:25 -05:00
|
|
|
return NULL;
|
|
|
|
|
2019-03-14 11:02:52 +01:00
|
|
|
nir_xfb_info *xfb = nir_xfb_info_create(mem_ctx, num_outputs);
|
|
|
|
if (varyings_info_out != NULL) {
|
|
|
|
*varyings_info_out = nir_xfb_varyings_info_create(mem_ctx, num_varyings);
|
|
|
|
varyings_info = *varyings_info_out;
|
|
|
|
}
|
2018-10-05 09:13:25 -05:00
|
|
|
|
|
|
|
/* Walk the list of outputs and add them to the array */
|
|
|
|
nir_foreach_variable(var, &shader->outputs) {
|
2019-01-08 18:22:16 -06:00
|
|
|
if (!var->data.explicit_xfb_buffer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned location = var->data.location;
|
nir/xfb: don't assert when xfb_buffer/stride is present but not xfb_offset
In order to allow nir_gather_xfb_info to be used on OpenGL,
specifically ARB_gl_spirv.
So, from OpenGL 4.6 spec, section 11.1.2.1, "Output Variables":
"outputs specifying both an *XfbBuffer* and an *Offset* are
captured, while outputs not specifying both of these are not
captured. Values are captured each time the shader writes to such
a decorated object."
This implies that are captured if both are present, and not if one of
those are lacking. Technically, it doesn't explicitly point that
having just one or the other is a mistake. In some cases, glslang is
adding some extra XfbBuffer without XfbOffset around, and mentioning
that technically that is not a bug (see issue#1526)
And for the case of Vulkan, as the same glslang issue mentions, it is
not clear if that should be a mistake or not. But even if it is a
mistake, it is not really needed to be checked on the driver, and we
can let the validation layers to check that.
v2: simplify explicit_xfb_buffer and explicit_offset checks (Jason).
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
2018-10-22 18:30:39 +02:00
|
|
|
|
2019-01-10 19:45:12 +01:00
|
|
|
/* In order to know if we have a array of blocks can't be done just by
|
|
|
|
* checking if we have an interface type and is an array, because due
|
|
|
|
* splitting we could end on a case were we received a split struct
|
|
|
|
* that contains an array.
|
|
|
|
*/
|
|
|
|
bool is_array_block = var->interface_type != NULL &&
|
|
|
|
glsl_type_is_array(var->type) &&
|
|
|
|
glsl_without_array(var->type) == glsl_get_bare_type(var->interface_type);
|
|
|
|
|
|
|
|
if (var->data.explicit_offset && !is_array_block) {
|
2018-10-05 09:13:25 -05:00
|
|
|
unsigned offset = var->data.offset;
|
2019-03-14 11:02:52 +01:00
|
|
|
add_var_xfb_outputs(xfb, varyings_info, var, var->data.xfb_buffer,
|
2019-01-10 15:04:37 +01:00
|
|
|
&location, &offset, var->type, false);
|
2019-01-10 19:45:12 +01:00
|
|
|
} else if (is_array_block) {
|
2019-03-05 16:07:12 +11:00
|
|
|
assert(glsl_type_is_struct_or_ifc(var->interface_type));
|
2019-01-10 19:45:12 +01:00
|
|
|
|
2019-01-08 18:22:16 -06:00
|
|
|
unsigned aoa_size = glsl_get_aoa_size(var->type);
|
2019-01-10 19:45:12 +01:00
|
|
|
const struct glsl_type *itype = var->interface_type;
|
|
|
|
unsigned nfields = glsl_get_length(itype);
|
2019-01-08 18:22:16 -06:00
|
|
|
for (unsigned b = 0; b < aoa_size; b++) {
|
|
|
|
for (unsigned f = 0; f < nfields; f++) {
|
2019-01-10 19:45:12 +01:00
|
|
|
int foffset = glsl_get_struct_field_offset(itype, f);
|
|
|
|
const struct glsl_type *ftype = glsl_get_struct_field(itype, f);
|
2019-01-08 18:22:16 -06:00
|
|
|
if (foffset < 0) {
|
|
|
|
location += glsl_count_attribute_slots(ftype, false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned offset = foffset;
|
2019-03-14 11:02:52 +01:00
|
|
|
add_var_xfb_outputs(xfb, varyings_info, var, var->data.xfb_buffer + b,
|
2019-01-10 15:04:37 +01:00
|
|
|
&location, &offset, ftype, false);
|
2019-01-08 18:22:16 -06:00
|
|
|
}
|
|
|
|
}
|
2018-10-05 09:13:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:42:49 +01:00
|
|
|
/* Everything is easier in the state setup code if outputs and varyings are
|
|
|
|
* sorted in order of output offset (and buffer for varyings).
|
2018-10-05 09:13:25 -05:00
|
|
|
*/
|
|
|
|
qsort(xfb->outputs, xfb->output_count, sizeof(xfb->outputs[0]),
|
|
|
|
compare_xfb_output_offsets);
|
|
|
|
|
2019-03-14 11:02:52 +01:00
|
|
|
if (varyings_info != NULL) {
|
|
|
|
qsort(varyings_info->varyings, varyings_info->varying_count,
|
|
|
|
sizeof(varyings_info->varyings[0]),
|
|
|
|
compare_xfb_varying_offsets);
|
|
|
|
}
|
2019-03-07 17:42:49 +01:00
|
|
|
|
2019-02-14 14:33:34 -06:00
|
|
|
#ifndef NDEBUG
|
2018-10-05 09:13:25 -05:00
|
|
|
/* Finally, do a sanity check */
|
2018-10-29 11:15:09 -06:00
|
|
|
unsigned max_offset[NIR_MAX_XFB_BUFFERS] = {0};
|
2018-10-05 09:13:25 -05:00
|
|
|
for (unsigned i = 0; i < xfb->output_count; i++) {
|
|
|
|
assert(xfb->outputs[i].offset >= max_offset[xfb->outputs[i].buffer]);
|
|
|
|
assert(xfb->outputs[i].component_mask != 0);
|
|
|
|
unsigned slots = util_bitcount(xfb->outputs[i].component_mask);
|
|
|
|
max_offset[xfb->outputs[i].buffer] = xfb->outputs[i].offset + slots * 4;
|
|
|
|
}
|
2019-02-14 14:33:34 -06:00
|
|
|
#endif
|
2018-10-05 09:13:25 -05:00
|
|
|
|
|
|
|
return xfb;
|
|
|
|
}
|