2016-10-04 06:37:40 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2015 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.h"
|
|
|
|
#include "nir_builder.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
2019-02-13 22:15:42 +00:00
|
|
|
* This pass combines clip and cull distance arrays in separate locations and
|
|
|
|
* colocates them both in VARYING_SLOT_CLIP_DIST0. It does so by maintaining
|
|
|
|
* two arrays but making them compact and using location_frac to stack them on
|
|
|
|
* top of each other.
|
2016-10-04 06:37:40 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the length of the clip/cull distance array, looking past
|
|
|
|
* any interface block arrays.
|
|
|
|
*/
|
|
|
|
static unsigned
|
|
|
|
get_unwrapped_array_length(nir_shader *nir, nir_variable *var)
|
|
|
|
{
|
|
|
|
if (!var)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Unwrap GS input and TCS input/output interfaces. We want the
|
|
|
|
* underlying clip/cull distance array length, not the per-vertex
|
|
|
|
* array length.
|
|
|
|
*/
|
|
|
|
const struct glsl_type *type = var->type;
|
2021-04-27 22:49:40 +01:00
|
|
|
if (nir_is_arrayed_io(var, nir->info.stage))
|
2016-10-04 06:37:40 +01:00
|
|
|
type = glsl_get_array_element(type);
|
|
|
|
|
2021-12-17 15:11:16 +00:00
|
|
|
if (var->data.per_view) {
|
|
|
|
assert(glsl_type_is_array(type));
|
|
|
|
type = glsl_get_array_element(type);
|
|
|
|
}
|
|
|
|
|
2016-10-04 06:37:40 +01:00
|
|
|
assert(glsl_type_is_array(type));
|
|
|
|
|
|
|
|
return glsl_get_length(type);
|
|
|
|
}
|
|
|
|
|
2017-02-24 23:38:28 +00:00
|
|
|
static bool
|
2016-10-04 06:37:40 +01:00
|
|
|
combine_clip_cull(nir_shader *nir,
|
2020-07-20 23:34:42 +01:00
|
|
|
nir_variable_mode mode,
|
2016-10-04 06:37:40 +01:00
|
|
|
bool store_info)
|
|
|
|
{
|
|
|
|
nir_variable *cull = NULL;
|
|
|
|
nir_variable *clip = NULL;
|
|
|
|
|
2020-07-20 23:34:42 +01:00
|
|
|
nir_foreach_variable_with_modes(var, nir, mode) {
|
2016-10-04 06:37:40 +01:00
|
|
|
if (var->data.location == VARYING_SLOT_CLIP_DIST0)
|
|
|
|
clip = var;
|
|
|
|
|
|
|
|
if (var->data.location == VARYING_SLOT_CULL_DIST0)
|
|
|
|
cull = var;
|
|
|
|
}
|
|
|
|
|
2020-03-13 21:54:35 +00:00
|
|
|
if (!cull && !clip) {
|
|
|
|
/* If this is run after optimizations and the variables have been
|
|
|
|
* eliminated, we should update the shader info, because no other
|
|
|
|
* place does that.
|
|
|
|
*/
|
|
|
|
if (store_info) {
|
|
|
|
nir->info.clip_distance_array_size = 0;
|
|
|
|
nir->info.cull_distance_array_size = 0;
|
|
|
|
}
|
2019-02-13 22:15:42 +00:00
|
|
|
return false;
|
2020-03-13 21:54:35 +00:00
|
|
|
}
|
2019-02-13 22:15:42 +00:00
|
|
|
|
2018-05-09 04:21:43 +01:00
|
|
|
if (!cull && clip) {
|
2019-01-23 10:36:44 +00:00
|
|
|
/* The GLSL IR lowering pass must have converted these to vectors */
|
|
|
|
if (!clip->data.compact)
|
2018-05-09 04:21:43 +01:00
|
|
|
return false;
|
2019-01-23 09:15:05 +00:00
|
|
|
|
|
|
|
/* If this pass has already run, don't repeat. We would think that
|
|
|
|
* the combined clip/cull distance array was clip-only and mess up.
|
|
|
|
*/
|
|
|
|
if (clip->data.how_declared == nir_var_hidden)
|
|
|
|
return false;
|
2018-05-09 04:21:43 +01:00
|
|
|
}
|
|
|
|
|
2016-10-04 06:37:40 +01:00
|
|
|
const unsigned clip_array_size = get_unwrapped_array_length(nir, clip);
|
|
|
|
const unsigned cull_array_size = get_unwrapped_array_length(nir, cull);
|
|
|
|
|
|
|
|
if (store_info) {
|
2017-05-08 17:20:21 +01:00
|
|
|
nir->info.clip_distance_array_size = clip_array_size;
|
|
|
|
nir->info.cull_distance_array_size = cull_array_size;
|
2016-10-04 06:37:40 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 09:15:05 +00:00
|
|
|
if (clip) {
|
2019-02-13 22:15:42 +00:00
|
|
|
assert(clip->data.compact);
|
2019-01-23 09:15:05 +00:00
|
|
|
clip->data.how_declared = nir_var_hidden;
|
|
|
|
}
|
2016-10-04 06:37:40 +01:00
|
|
|
|
2019-01-23 09:15:05 +00:00
|
|
|
if (cull) {
|
2019-02-21 15:53:30 +00:00
|
|
|
assert(cull->data.compact);
|
2019-01-23 09:15:05 +00:00
|
|
|
cull->data.how_declared = nir_var_hidden;
|
2019-02-13 22:15:42 +00:00
|
|
|
cull->data.location = VARYING_SLOT_CLIP_DIST0 + clip_array_size / 4;
|
|
|
|
cull->data.location_frac = clip_array_size % 4;
|
2019-01-23 09:15:05 +00:00
|
|
|
}
|
2016-10-04 06:37:40 +01:00
|
|
|
|
2019-02-13 22:15:42 +00:00
|
|
|
return true;
|
2016-10-04 06:37:40 +01:00
|
|
|
}
|
|
|
|
|
2017-02-24 23:38:28 +00:00
|
|
|
bool
|
2016-10-04 06:37:40 +01:00
|
|
|
nir_lower_clip_cull_distance_arrays(nir_shader *nir)
|
|
|
|
{
|
2017-02-24 23:38:28 +00:00
|
|
|
bool progress = false;
|
|
|
|
|
2021-10-28 15:28:37 +01:00
|
|
|
if (nir->info.stage <= MESA_SHADER_GEOMETRY ||
|
|
|
|
nir->info.stage == MESA_SHADER_MESH)
|
2020-07-20 23:34:42 +01:00
|
|
|
progress |= combine_clip_cull(nir, nir_var_shader_out, true);
|
2016-10-04 06:37:40 +01:00
|
|
|
|
2021-11-24 11:38:07 +00:00
|
|
|
if (nir->info.stage > MESA_SHADER_VERTEX &&
|
|
|
|
nir->info.stage <= MESA_SHADER_FRAGMENT) {
|
2020-09-24 14:23:58 +01:00
|
|
|
progress |= combine_clip_cull(nir, nir_var_shader_in,
|
|
|
|
nir->info.stage == MESA_SHADER_FRAGMENT);
|
|
|
|
}
|
2017-02-24 23:38:28 +00:00
|
|
|
|
2020-05-22 04:34:37 +01:00
|
|
|
nir_foreach_function(function, nir) {
|
|
|
|
if (!function->impl)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (progress) {
|
|
|
|
nir_metadata_preserve(function->impl,
|
|
|
|
nir_metadata_block_index |
|
|
|
|
nir_metadata_dominance |
|
|
|
|
nir_metadata_live_ssa_defs |
|
|
|
|
nir_metadata_loop_analysis);
|
|
|
|
} else {
|
|
|
|
nir_metadata_preserve(function->impl, nir_metadata_all);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 23:38:28 +00:00
|
|
|
return progress;
|
2016-10-04 06:37:40 +01:00
|
|
|
}
|