2019-11-04 17:27:18 -05:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2019 Collabora, Ltd.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* Authors (Collabora):
|
|
|
|
|
* Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "nir.h"
|
|
|
|
|
#include "nir_builder.h"
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Lowers SSBOs to globals, for hardware that lack native SSBO support. When
|
|
|
|
|
* lowering, *_ssbo_* instructions will become *_global_* instructions,
|
|
|
|
|
* augmented with load_ssbo_address.
|
|
|
|
|
*
|
|
|
|
|
* DOES NOT PERFORM BOUNDS CHECKING. DO NOT USE IN PRODUCTION ON UNTRUSTED
|
|
|
|
|
* CONTEXTS INCLUDING WEBGL 2.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static nir_intrinsic_op
|
|
|
|
|
lower_ssbo_op(nir_intrinsic_op op)
|
|
|
|
|
{
|
|
|
|
|
switch (op) {
|
|
|
|
|
case nir_intrinsic_load_ssbo:
|
|
|
|
|
return nir_intrinsic_load_global;
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_store_ssbo:
|
|
|
|
|
return nir_intrinsic_store_global;
|
|
|
|
|
|
2023-05-12 10:54:05 -04:00
|
|
|
case nir_intrinsic_ssbo_atomic:
|
|
|
|
|
return nir_intrinsic_global_atomic;
|
|
|
|
|
case nir_intrinsic_ssbo_atomic_swap:
|
|
|
|
|
return nir_intrinsic_global_atomic_swap;
|
2020-02-07 18:44:47 +01:00
|
|
|
|
2019-11-04 17:27:18 -05:00
|
|
|
default:
|
|
|
|
|
unreachable("Invalid SSBO op");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Like SSBO property sysvals, though SSBO index may be indirect. C.f.
|
|
|
|
|
* nir_load_system_value */
|
|
|
|
|
|
2023-08-12 16:17:15 -04:00
|
|
|
static inline nir_def *
|
2019-11-04 17:27:18 -05:00
|
|
|
nir_load_ssbo_prop(nir_builder *b, nir_intrinsic_op op,
|
2023-08-08 12:00:35 -05:00
|
|
|
nir_src *idx, unsigned bitsize)
|
2019-11-04 17:27:18 -05:00
|
|
|
{
|
|
|
|
|
nir_intrinsic_instr *load = nir_intrinsic_instr_create(b->shader, op);
|
|
|
|
|
load->num_components = 1;
|
2023-09-06 13:56:09 +10:00
|
|
|
load->src[0] = nir_src_for_ssa(idx->ssa);
|
2023-08-14 11:56:00 -05:00
|
|
|
nir_def_init(&load->instr, &load->def, 1, bitsize);
|
2019-11-04 17:27:18 -05:00
|
|
|
nir_builder_instr_insert(b, &load->instr);
|
2023-08-14 11:56:00 -05:00
|
|
|
return &load->def;
|
2019-11-04 17:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define nir_ssbo_prop(b, prop, index, bitsize) \
|
|
|
|
|
nir_load_ssbo_prop(b, nir_intrinsic_##prop, index, bitsize)
|
|
|
|
|
|
2023-08-12 16:17:15 -04:00
|
|
|
static nir_def *
|
2019-11-04 17:27:18 -05:00
|
|
|
lower_ssbo_instr(nir_builder *b, nir_intrinsic_instr *intr)
|
|
|
|
|
{
|
|
|
|
|
nir_intrinsic_op op = lower_ssbo_op(intr->intrinsic);
|
|
|
|
|
bool is_store = op == nir_intrinsic_store_global;
|
2020-02-07 18:44:47 +01:00
|
|
|
bool is_atomic = !is_store && op != nir_intrinsic_load_global;
|
2019-11-04 17:27:18 -05:00
|
|
|
|
|
|
|
|
/* We have to calculate the address:
|
|
|
|
|
*
|
|
|
|
|
* &(SSBO[offset]) = &SSBO + offset
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
nir_src index = intr->src[is_store ? 1 : 0];
|
|
|
|
|
nir_src *offset_src = nir_get_io_offset_src(intr);
|
2023-09-15 10:57:20 -04:00
|
|
|
nir_def *offset = offset_src->ssa;
|
2019-11-04 17:27:18 -05:00
|
|
|
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def *address =
|
2019-11-04 17:27:18 -05:00
|
|
|
nir_iadd(b,
|
2023-08-08 12:00:35 -05:00
|
|
|
nir_ssbo_prop(b, load_ssbo_address, &index, 64),
|
|
|
|
|
nir_u2u64(b, offset));
|
2019-11-04 17:27:18 -05:00
|
|
|
|
|
|
|
|
/* Create the replacement intrinsic */
|
|
|
|
|
|
|
|
|
|
nir_intrinsic_instr *global =
|
|
|
|
|
nir_intrinsic_instr_create(b->shader, op);
|
|
|
|
|
|
|
|
|
|
global->num_components = intr->num_components;
|
|
|
|
|
global->src[is_store ? 1 : 0] = nir_src_for_ssa(address);
|
|
|
|
|
|
2023-05-12 10:54:05 -04:00
|
|
|
if (nir_intrinsic_has_atomic_op(intr))
|
|
|
|
|
nir_intrinsic_set_atomic_op(global, nir_intrinsic_atomic_op(intr));
|
|
|
|
|
|
2020-08-06 09:13:32 -04:00
|
|
|
if (!is_atomic) {
|
|
|
|
|
nir_intrinsic_set_align_mul(global, nir_intrinsic_align_mul(intr));
|
|
|
|
|
nir_intrinsic_set_align_offset(global, nir_intrinsic_align_offset(intr));
|
|
|
|
|
}
|
2020-07-17 23:26:41 +12:00
|
|
|
|
2019-11-04 17:27:18 -05:00
|
|
|
if (is_store) {
|
2023-09-06 13:56:09 +10:00
|
|
|
global->src[0] = nir_src_for_ssa(intr->src[0].ssa);
|
2019-11-04 17:27:18 -05:00
|
|
|
nir_intrinsic_set_write_mask(global, nir_intrinsic_write_mask(intr));
|
|
|
|
|
} else {
|
2023-08-14 11:56:00 -05:00
|
|
|
nir_def_init(&global->instr, &global->def,
|
|
|
|
|
intr->def.num_components, intr->def.bit_size);
|
2020-02-07 18:44:47 +01:00
|
|
|
|
|
|
|
|
if (is_atomic) {
|
2023-09-06 13:56:09 +10:00
|
|
|
global->src[1] = nir_src_for_ssa(intr->src[2].ssa);
|
2020-02-07 18:44:47 +01:00
|
|
|
if (nir_intrinsic_infos[op].num_srcs > 2)
|
2023-09-06 13:56:09 +10:00
|
|
|
global->src[2] = nir_src_for_ssa(intr->src[3].ssa);
|
2020-02-07 18:44:47 +01:00
|
|
|
}
|
2019-11-04 17:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nir_builder_instr_insert(b, &global->instr);
|
2023-08-14 11:56:00 -05:00
|
|
|
return is_store ? NULL : &global->def;
|
2019-11-04 17:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
should_lower_ssbo_instr(const nir_instr *instr)
|
|
|
|
|
{
|
|
|
|
|
if (instr->type != nir_instr_type_intrinsic)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
|
|
|
|
|
|
|
|
|
|
switch (intr->intrinsic) {
|
|
|
|
|
case nir_intrinsic_load_ssbo:
|
|
|
|
|
case nir_intrinsic_store_ssbo:
|
2023-05-12 10:54:05 -04:00
|
|
|
case nir_intrinsic_ssbo_atomic:
|
|
|
|
|
case nir_intrinsic_ssbo_atomic_swap:
|
2019-11-04 17:27:18 -05:00
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
nir_lower_ssbo(nir_shader *shader)
|
|
|
|
|
{
|
|
|
|
|
bool progress = false;
|
|
|
|
|
|
2023-06-22 13:27:59 -04:00
|
|
|
nir_foreach_function_impl(impl, shader) {
|
2023-06-26 10:42:29 -04:00
|
|
|
nir_builder b = nir_builder_create(impl);
|
2019-11-04 17:27:18 -05:00
|
|
|
|
|
|
|
|
nir_foreach_block(block, impl) {
|
|
|
|
|
nir_foreach_instr_safe(instr, block) {
|
2023-08-08 12:00:35 -05:00
|
|
|
if (!should_lower_ssbo_instr(instr))
|
|
|
|
|
continue;
|
2019-11-04 17:27:18 -05:00
|
|
|
progress = true;
|
|
|
|
|
b.cursor = nir_before_instr(instr);
|
|
|
|
|
|
|
|
|
|
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def *replace = lower_ssbo_instr(&b, intr);
|
2019-11-04 17:27:18 -05:00
|
|
|
|
2023-08-08 12:00:35 -05:00
|
|
|
if (replace) {
|
2023-08-14 11:56:00 -05:00
|
|
|
nir_def_rewrite_uses(&intr->def,
|
2023-08-12 16:17:15 -04:00
|
|
|
replace);
|
2019-11-04 17:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nir_instr_remove(instr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|