2014-07-30 14:43:26 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2014 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.
|
|
|
|
|
*
|
|
|
|
|
* Authors:
|
|
|
|
|
* Connor Abbott (cwabbott0@gmail.com)
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2018-04-30 20:39:43 +10:00
|
|
|
#include "compiler/nir/nir.h"
|
2018-03-20 16:30:54 -07:00
|
|
|
#include "compiler/nir/nir_builder.h"
|
2018-04-30 20:39:43 +10:00
|
|
|
#include "gl_nir.h"
|
|
|
|
|
#include "ir_uniform.h"
|
2015-02-11 15:08:02 -08:00
|
|
|
#include "main/config.h"
|
2022-01-07 15:11:21 +10:00
|
|
|
#include "main/shader_types.h"
|
2014-07-30 14:43:26 -07:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* replace atomic counter intrinsics that use a variable with intrinsics
|
|
|
|
|
* that directly store the buffer index and byte offset
|
|
|
|
|
*/
|
|
|
|
|
|
2018-03-20 16:30:54 -07:00
|
|
|
static bool
|
|
|
|
|
lower_deref_instr(nir_builder *b, nir_intrinsic_instr *instr,
|
|
|
|
|
const struct gl_shader_program *shader_program,
|
|
|
|
|
nir_shader *shader, bool use_binding_as_idx)
|
|
|
|
|
{
|
|
|
|
|
nir_intrinsic_op op;
|
|
|
|
|
switch (instr->intrinsic) {
|
|
|
|
|
case nir_intrinsic_atomic_counter_read_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_read;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_atomic_counter_inc_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_inc;
|
|
|
|
|
break;
|
|
|
|
|
|
nir: Fix OpAtomicCounterIDecrement for uniform atomic counters
From the SPIR-V 1.0 specification, section 3.32.18, "Atomic
Instructions":
"OpAtomicIDecrement:
<skip>
The instruction's result is the Original Value."
However, we were implementing it, for uniform atomic counters, as a
pre-decrement operation, as was the one available from GLSL.
Renamed the former nir intrinsic 'atomic_counter_dec*' to
'atomic_counter_pre_dec*' for clarification purposes, as it implements
a pre-decrement operation as specified for GLSL. From GLSL 4.50 spec,
section 8.10, "Atomic Counter Functions":
"uint atomicCounterDecrement (atomic_uint c)
Atomically
1. decrements the counter for c, and
2. returns the value resulting from the decrement operation.
These two steps are done atomically with respect to the atomic
counter functions in this table."
Added a new nir intrinsic 'atomic_counter_post_dec*' which implements
a post-decrement operation as required by SPIR-V.
v2: (Timothy Arceri)
* Add extra spec quotes on commit message
* Use "post" instead "pos" to avoid confusion with "position"
Signed-off-by: Antia Puentes <apuentes@igalia.com>
Signed-off-by: Alejandro Piñeiro <apinheiro@igalia.com>
Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com>
2018-02-22 13:50:23 +01:00
|
|
|
case nir_intrinsic_atomic_counter_pre_dec_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_pre_dec;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_atomic_counter_post_dec_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_post_dec;
|
2018-03-20 16:30:54 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_atomic_counter_add_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_add;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_atomic_counter_min_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_min;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_atomic_counter_max_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_max;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_atomic_counter_and_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_and;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_atomic_counter_or_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_or;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_atomic_counter_xor_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_xor;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_atomic_counter_exchange_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_exchange;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nir_intrinsic_atomic_counter_comp_swap_deref:
|
|
|
|
|
op = nir_intrinsic_atomic_counter_comp_swap;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nir_deref_instr *deref = nir_src_as_deref(instr->src[0]);
|
|
|
|
|
nir_variable *var = nir_deref_instr_get_variable(deref);
|
|
|
|
|
|
|
|
|
|
if (var->data.mode != nir_var_uniform &&
|
2019-01-16 00:11:23 +01:00
|
|
|
var->data.mode != nir_var_mem_ssbo &&
|
2019-01-16 00:12:38 +01:00
|
|
|
var->data.mode != nir_var_mem_shared)
|
2018-03-20 16:30:54 -07:00
|
|
|
return false; /* atomics passed as function arguments can't be lowered */
|
|
|
|
|
|
|
|
|
|
const unsigned uniform_loc = var->data.location;
|
|
|
|
|
const unsigned idx = use_binding_as_idx ? var->data.binding :
|
|
|
|
|
shader_program->data->UniformStorage[uniform_loc].opaque[shader->info.stage].index;
|
|
|
|
|
|
|
|
|
|
b->cursor = nir_before_instr(&instr->instr);
|
|
|
|
|
|
2022-11-24 17:03:38 +01:00
|
|
|
int offset_value = 0;
|
|
|
|
|
int range_base = 0;
|
|
|
|
|
if (!b->shader->options->lower_atomic_offset_to_range_base)
|
|
|
|
|
offset_value = var->data.offset;
|
|
|
|
|
else
|
|
|
|
|
range_base = var->data.offset;
|
|
|
|
|
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def *offset = nir_imm_int(b, offset_value);
|
2018-03-20 16:30:54 -07:00
|
|
|
for (nir_deref_instr *d = deref; d->deref_type != nir_deref_type_var;
|
|
|
|
|
d = nir_deref_instr_parent(d)) {
|
|
|
|
|
assert(d->deref_type == nir_deref_type_array);
|
|
|
|
|
|
|
|
|
|
unsigned array_stride = ATOMIC_COUNTER_SIZE;
|
|
|
|
|
if (glsl_type_is_array(d->type))
|
|
|
|
|
array_stride *= glsl_get_aoa_size(d->type);
|
|
|
|
|
|
|
|
|
|
offset = nir_iadd(b, offset, nir_imul(b, d->arr.index.ssa,
|
|
|
|
|
nir_imm_int(b, array_stride)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Since the first source is a deref and the first source in the lowered
|
|
|
|
|
* instruction is the offset, we can just swap it out and change the
|
|
|
|
|
* opcode.
|
|
|
|
|
*/
|
|
|
|
|
instr->intrinsic = op;
|
2022-11-24 17:03:38 +01:00
|
|
|
nir_intrinsic_set_range_base(instr, range_base);
|
|
|
|
|
|
2018-03-20 16:30:54 -07:00
|
|
|
nir_instr_rewrite_src(&instr->instr, &instr->src[0],
|
|
|
|
|
nir_src_for_ssa(offset));
|
|
|
|
|
nir_intrinsic_set_base(instr, idx);
|
|
|
|
|
|
|
|
|
|
nir_deref_instr_remove_if_unused(deref);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 15:23:56 +02:00
|
|
|
struct lower_atomics_data {
|
|
|
|
|
bool use_binding_as_idx;
|
|
|
|
|
nir_shader *shader;
|
|
|
|
|
const struct gl_shader_program *shader_program;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
gl_nir_lower_atomics_instr(nir_builder *b, nir_instr *instr, void *cb_data)
|
|
|
|
|
{
|
|
|
|
|
if (instr->type != nir_instr_type_intrinsic)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
struct lower_atomics_data *data = cb_data;
|
|
|
|
|
|
|
|
|
|
return lower_deref_instr(b,
|
|
|
|
|
nir_instr_as_intrinsic(instr),
|
|
|
|
|
data->shader_program,
|
|
|
|
|
data->shader,
|
|
|
|
|
data->use_binding_as_idx);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-02 11:24:19 -08:00
|
|
|
bool
|
2018-04-30 20:39:43 +10:00
|
|
|
gl_nir_lower_atomics(nir_shader *shader,
|
|
|
|
|
const struct gl_shader_program *shader_program,
|
|
|
|
|
bool use_binding_as_idx)
|
2014-07-30 14:43:26 -07:00
|
|
|
{
|
2021-08-06 15:23:56 +02:00
|
|
|
struct lower_atomics_data data = {
|
|
|
|
|
.use_binding_as_idx = use_binding_as_idx,
|
|
|
|
|
.shader = shader,
|
|
|
|
|
.shader_program = shader_program,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return nir_shader_instructions_pass(shader, gl_nir_lower_atomics_instr,
|
|
|
|
|
nir_metadata_block_index |
|
|
|
|
|
nir_metadata_dominance,
|
|
|
|
|
&data);
|
2014-07-30 14:43:26 -07:00
|
|
|
}
|