2020-06-17 09:35:46 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2020 Mike Blumenkrantz
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
2023-08-08 12:00:35 -05:00
|
|
|
*
|
2020-06-17 09:35:46 -04:00
|
|
|
* Authors:
|
|
|
|
|
* Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "nir.h"
|
|
|
|
|
#include "nir_builder.h"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This pass uses the enabled clip planes from the rasterizer state to rewrite
|
2020-09-03 10:25:24 -04:00
|
|
|
* vertex shader store operations and store a 0 to the corresponding gl_ClipDistance[n]
|
2020-06-17 09:35:46 -04:00
|
|
|
* value if the plane is disabled
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* recursively nest if/else blocks until we get to an array index,
|
|
|
|
|
* then overwrite it if that plane isn't enabled
|
|
|
|
|
*/
|
|
|
|
|
static void
|
2023-08-12 16:17:15 -04:00
|
|
|
recursive_if_chain(nir_builder *b, nir_deref_instr *deref, nir_def *value, unsigned clip_plane_enable, nir_def *index, unsigned start, unsigned end)
|
2020-06-17 09:35:46 -04:00
|
|
|
{
|
|
|
|
|
if (start == end - 1) {
|
|
|
|
|
/* store the original value again if the clip plane is enabled */
|
|
|
|
|
if (clip_plane_enable & (1 << start))
|
|
|
|
|
nir_store_deref(b, deref, value, 1 << start);
|
|
|
|
|
else
|
2020-09-03 10:25:24 -04:00
|
|
|
nir_store_deref(b, deref, nir_imm_int(b, 0), 1 << start);
|
2020-06-17 09:35:46 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned mid = start + (end - start) / 2;
|
2023-05-08 14:00:41 +02:00
|
|
|
nir_push_if(b, nir_ilt_imm(b, index, mid));
|
2020-06-17 09:35:46 -04:00
|
|
|
recursive_if_chain(b, deref, value, clip_plane_enable, index, start, mid);
|
|
|
|
|
nir_push_else(b, NULL);
|
|
|
|
|
recursive_if_chain(b, deref, value, clip_plane_enable, index, mid, end);
|
|
|
|
|
nir_pop_if(b, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* vulkan (and some drivers) provides no concept of enabling clip planes through api,
|
2020-09-03 10:25:24 -04:00
|
|
|
* so we rewrite disabled clip planes to a zero value in order to disable them
|
2020-06-17 09:35:46 -04:00
|
|
|
*/
|
|
|
|
|
static bool
|
treewide: Use nir_shader_intrinsic_pass sometimes
This converts a lot of trivial passes. Nice boilerplate deletion. Via Coccinelle
patch (with a small manual fix-up for panfrost where coccinelle got confused by
genxml + ninja clang-format squashed in, and for Zink because my semantic patch
was slightly buggy).
@def@
typedef bool;
typedef nir_builder;
typedef nir_instr;
typedef nir_def;
identifier fn, instr, intr, x, builder, data;
@@
static fn(nir_builder* builder,
-nir_instr *instr,
+nir_intrinsic_instr *intr,
...)
{
(
- if (instr->type != nir_instr_type_intrinsic)
- return false;
- nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
|
- nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
- if (instr->type != nir_instr_type_intrinsic)
- return false;
)
<...
(
-instr->x
+intr->instr.x
|
-instr
+&intr->instr
)
...>
}
@pass depends on def@
identifier def.fn;
expression shader, progress;
@@
(
-nir_shader_instructions_pass(shader, fn,
+nir_shader_intrinsics_pass(shader, fn,
...)
|
-NIR_PASS_V(shader, nir_shader_instructions_pass, fn,
+NIR_PASS_V(shader, nir_shader_intrinsics_pass, fn,
...)
|
-NIR_PASS(progress, shader, nir_shader_instructions_pass, fn,
+NIR_PASS(progress, shader, nir_shader_intrinsics_pass, fn,
...)
)
Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Acked-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24852>
2023-08-23 12:48:10 -04:00
|
|
|
lower_clip_plane_store(nir_builder *b, nir_intrinsic_instr *instr,
|
|
|
|
|
void *cb_data)
|
2020-06-17 09:35:46 -04:00
|
|
|
{
|
2021-08-09 15:06:35 +02:00
|
|
|
unsigned clip_plane_enable = *(unsigned *)cb_data;
|
2020-06-17 09:35:46 -04:00
|
|
|
nir_variable *out;
|
|
|
|
|
unsigned plane;
|
|
|
|
|
|
|
|
|
|
if (instr->intrinsic != nir_intrinsic_store_deref)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
nir_deref_instr *deref = nir_src_as_deref(instr->src[0]);
|
|
|
|
|
|
|
|
|
|
out = nir_deref_instr_get_variable(deref);
|
2020-09-02 10:26:15 -04:00
|
|
|
if ((out->data.location != VARYING_SLOT_CLIP_DIST0 &&
|
|
|
|
|
out->data.location != VARYING_SLOT_CLIP_DIST1) ||
|
2023-08-08 12:00:35 -05:00
|
|
|
out->data.mode != nir_var_shader_out)
|
2020-06-17 09:35:46 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
b->cursor = nir_after_instr(&instr->instr);
|
2020-09-02 10:26:15 -04:00
|
|
|
if (deref->deref_type == nir_deref_type_var) {
|
|
|
|
|
int wrmask = nir_intrinsic_write_mask(instr);
|
|
|
|
|
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def *components[4];
|
2020-09-02 10:26:15 -04:00
|
|
|
int start = out->data.location == VARYING_SLOT_CLIP_DIST1 ? 4 : 0;
|
|
|
|
|
/* rewrite components as zeroes for planes that aren't enabled */
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
if (wrmask & (1 << i)) {
|
|
|
|
|
if (!(clip_plane_enable & (1 << (start + i))))
|
|
|
|
|
components[i] = nir_imm_int(b, 0);
|
|
|
|
|
else
|
2023-09-15 10:57:20 -04:00
|
|
|
components[i] = nir_channel(b, instr->src[1].ssa, i);
|
2020-09-02 10:26:15 -04:00
|
|
|
} else
|
2023-08-12 16:17:15 -04:00
|
|
|
components[i] = nir_undef(b, 1, 32);
|
2020-09-02 10:26:15 -04:00
|
|
|
}
|
|
|
|
|
nir_store_deref(b, deref, nir_vec(b, components, instr->num_components), wrmask);
|
2023-08-08 12:00:35 -05:00
|
|
|
} else if (nir_src_is_const(deref->arr.index)) {
|
2020-06-17 09:35:46 -04:00
|
|
|
/* storing using a constant index */
|
|
|
|
|
plane = nir_src_as_uint(deref->arr.index);
|
|
|
|
|
/* no need to make changes if the clip plane is enabled */
|
|
|
|
|
if (clip_plane_enable & (1 << plane))
|
|
|
|
|
return false;
|
|
|
|
|
|
2021-04-22 11:58:35 +02:00
|
|
|
assert(nir_intrinsic_write_mask(instr) == 1);
|
|
|
|
|
nir_store_deref(b, deref, nir_imm_int(b, 0), 1);
|
2020-06-17 09:35:46 -04:00
|
|
|
} else {
|
|
|
|
|
/* storing using a variable index */
|
2023-09-15 10:57:20 -04:00
|
|
|
nir_def *index = deref->arr.index.ssa;
|
2020-06-17 09:35:46 -04:00
|
|
|
unsigned length = glsl_get_length(nir_deref_instr_parent(deref)->type);
|
|
|
|
|
|
|
|
|
|
recursive_if_chain(b, deref, instr->src[1].ssa, clip_plane_enable, index, 0, length);
|
|
|
|
|
}
|
|
|
|
|
nir_instr_remove(&instr->instr);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-18 17:23:12 -04:00
|
|
|
/* vulkan (and some drivers) provides no concept of enabling clip planes through api,
|
|
|
|
|
* so we rewrite disabled clip planes to a zero value in order to disable them
|
|
|
|
|
*/
|
|
|
|
|
static bool
|
|
|
|
|
lower_clip_plane_store_io(nir_builder *b, nir_intrinsic_instr *intr,
|
|
|
|
|
void *cb_data)
|
|
|
|
|
{
|
|
|
|
|
unsigned clip_plane_enable = *(unsigned *)cb_data;
|
|
|
|
|
|
|
|
|
|
switch (intr->intrinsic) {
|
|
|
|
|
case nir_intrinsic_store_output:
|
|
|
|
|
case nir_intrinsic_store_per_primitive_output:
|
|
|
|
|
case nir_intrinsic_store_per_vertex_output:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
|
|
|
|
|
if (sem.location != VARYING_SLOT_CLIP_DIST0 &&
|
|
|
|
|
sem.location != VARYING_SLOT_CLIP_DIST1)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
b->cursor = nir_before_instr(&intr->instr);
|
|
|
|
|
nir_src *src_offset = nir_get_io_offset_src(intr);
|
|
|
|
|
int wrmask = nir_intrinsic_write_mask(intr);
|
|
|
|
|
int c = nir_intrinsic_component(intr);
|
|
|
|
|
nir_def *zero = nir_imm_int(b, 0);
|
|
|
|
|
if (nir_src_is_const(*src_offset)) {
|
|
|
|
|
int i = sem.location == VARYING_SLOT_CLIP_DIST1 ? 4 : 0;
|
|
|
|
|
i += nir_src_as_const_value(*src_offset)->u32 * 4;
|
|
|
|
|
i += c;
|
|
|
|
|
nir_def *val;
|
|
|
|
|
if (wrmask & 0x1) {
|
|
|
|
|
if (clip_plane_enable & BITFIELD_BIT(i))
|
|
|
|
|
return false;
|
|
|
|
|
val = zero;
|
|
|
|
|
} else
|
|
|
|
|
val = nir_undef(b, 1, 32);
|
|
|
|
|
nir_src_rewrite(&intr->src[0], val);
|
|
|
|
|
} else {
|
|
|
|
|
nir_def *first = clip_plane_enable & BITFIELD_BIT(c) ? intr->src[0].ssa : zero;
|
|
|
|
|
nir_def *second = clip_plane_enable & BITFIELD_BIT(c + 4) ? intr->src[0].ssa : zero;
|
|
|
|
|
nir_def *val = nir_bcsel(b, nir_ieq_imm(b, src_offset->ssa, 0), first, second);
|
|
|
|
|
nir_src_rewrite(&intr->src[0], val);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 09:35:46 -04:00
|
|
|
bool
|
|
|
|
|
nir_lower_clip_disable(nir_shader *shader, unsigned clip_plane_enable)
|
|
|
|
|
{
|
2020-09-02 10:09:21 -04:00
|
|
|
/* if all user planes are enabled in API that are written in the array, always ignore;
|
|
|
|
|
* this explicitly covers the 2x vec4 case
|
|
|
|
|
*/
|
|
|
|
|
if (clip_plane_enable == u_bit_consecutive(0, shader->info.clip_distance_array_size))
|
|
|
|
|
return false;
|
2020-06-17 09:35:46 -04:00
|
|
|
|
2024-03-18 17:23:12 -04:00
|
|
|
return nir_shader_intrinsics_pass(shader,
|
|
|
|
|
shader->info.io_lowered ? lower_clip_plane_store_io : lower_clip_plane_store,
|
2021-08-09 15:06:35 +02:00
|
|
|
nir_metadata_block_index |
|
2023-08-08 12:00:35 -05:00
|
|
|
nir_metadata_dominance,
|
2021-08-09 15:06:35 +02:00
|
|
|
&clip_plane_enable);
|
2020-06-17 09:35:46 -04:00
|
|
|
}
|