2019-06-04 14:13:13 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2019 Valve 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.
|
|
|
|
*/
|
|
|
|
|
2019-09-27 16:43:31 -04:00
|
|
|
#include "nir.h"
|
2019-06-04 14:13:13 +02:00
|
|
|
|
|
|
|
/* This pass optimizes GL access qualifiers. So far it does two things:
|
|
|
|
*
|
|
|
|
* - Infer readonly when it's missing.
|
|
|
|
* - Infer ACCESS_CAN_REORDER when the following are true:
|
2020-12-01 14:38:32 +00:00
|
|
|
* - Either there are no writes, or ACCESS_NON_WRITEABLE is set. In either
|
|
|
|
* case there are no writes to the underlying memory.
|
2019-06-04 14:13:13 +02:00
|
|
|
* - ACCESS_VOLATILE is not set.
|
|
|
|
*
|
|
|
|
* If these conditions are true, then image and buffer reads may be treated as
|
|
|
|
* if they were uniform buffer reads, i.e. they may be arbitrarily moved,
|
|
|
|
* combined, rematerialized etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct access_state {
|
2020-08-25 17:03:03 +01:00
|
|
|
nir_shader *shader;
|
|
|
|
|
2019-06-04 14:13:13 +02:00
|
|
|
struct set *vars_written;
|
|
|
|
bool images_written;
|
|
|
|
bool buffers_written;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gather_intrinsic(struct access_state *state, nir_intrinsic_instr *instr)
|
|
|
|
{
|
2020-08-25 17:03:03 +01:00
|
|
|
const nir_variable *var;
|
2019-06-04 14:13:13 +02:00
|
|
|
switch (instr->intrinsic) {
|
|
|
|
case nir_intrinsic_image_deref_store:
|
|
|
|
case nir_intrinsic_image_deref_atomic_add:
|
2019-08-20 22:32:50 -05:00
|
|
|
case nir_intrinsic_image_deref_atomic_imin:
|
|
|
|
case nir_intrinsic_image_deref_atomic_umin:
|
|
|
|
case nir_intrinsic_image_deref_atomic_imax:
|
|
|
|
case nir_intrinsic_image_deref_atomic_umax:
|
2019-06-04 14:13:13 +02:00
|
|
|
case nir_intrinsic_image_deref_atomic_and:
|
|
|
|
case nir_intrinsic_image_deref_atomic_or:
|
|
|
|
case nir_intrinsic_image_deref_atomic_xor:
|
|
|
|
case nir_intrinsic_image_deref_atomic_exchange:
|
|
|
|
case nir_intrinsic_image_deref_atomic_comp_swap:
|
|
|
|
case nir_intrinsic_image_deref_atomic_fadd:
|
|
|
|
var = nir_intrinsic_get_var(instr, 0);
|
|
|
|
|
|
|
|
/* In OpenGL, buffer images use normal buffer objects, whereas other
|
|
|
|
* image types use textures which cannot alias with buffer objects.
|
|
|
|
* Therefore we have to group buffer samplers together with SSBO's.
|
|
|
|
*/
|
2019-06-19 10:00:39 -07:00
|
|
|
if (glsl_get_sampler_dim(glsl_without_array(var->type)) ==
|
|
|
|
GLSL_SAMPLER_DIM_BUF)
|
2019-06-04 14:13:13 +02:00
|
|
|
state->buffers_written = true;
|
|
|
|
else
|
|
|
|
state->images_written = true;
|
|
|
|
|
|
|
|
if (var->data.mode == nir_var_uniform)
|
|
|
|
_mesa_set_add(state->vars_written, var);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nir_intrinsic_bindless_image_store:
|
|
|
|
case nir_intrinsic_bindless_image_atomic_add:
|
2019-08-20 22:32:50 -05:00
|
|
|
case nir_intrinsic_bindless_image_atomic_imin:
|
|
|
|
case nir_intrinsic_bindless_image_atomic_umin:
|
|
|
|
case nir_intrinsic_bindless_image_atomic_imax:
|
|
|
|
case nir_intrinsic_bindless_image_atomic_umax:
|
2019-06-04 14:13:13 +02:00
|
|
|
case nir_intrinsic_bindless_image_atomic_and:
|
|
|
|
case nir_intrinsic_bindless_image_atomic_or:
|
|
|
|
case nir_intrinsic_bindless_image_atomic_xor:
|
|
|
|
case nir_intrinsic_bindless_image_atomic_exchange:
|
|
|
|
case nir_intrinsic_bindless_image_atomic_comp_swap:
|
|
|
|
case nir_intrinsic_bindless_image_atomic_fadd:
|
|
|
|
if (nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_BUF)
|
|
|
|
state->buffers_written = true;
|
|
|
|
else
|
|
|
|
state->images_written = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nir_intrinsic_store_deref:
|
|
|
|
case nir_intrinsic_deref_atomic_add:
|
|
|
|
case nir_intrinsic_deref_atomic_imin:
|
|
|
|
case nir_intrinsic_deref_atomic_umin:
|
|
|
|
case nir_intrinsic_deref_atomic_imax:
|
|
|
|
case nir_intrinsic_deref_atomic_umax:
|
|
|
|
case nir_intrinsic_deref_atomic_and:
|
|
|
|
case nir_intrinsic_deref_atomic_or:
|
|
|
|
case nir_intrinsic_deref_atomic_xor:
|
|
|
|
case nir_intrinsic_deref_atomic_exchange:
|
|
|
|
case nir_intrinsic_deref_atomic_comp_swap:
|
|
|
|
case nir_intrinsic_deref_atomic_fadd:
|
|
|
|
case nir_intrinsic_deref_atomic_fmin:
|
|
|
|
case nir_intrinsic_deref_atomic_fmax:
|
|
|
|
case nir_intrinsic_deref_atomic_fcomp_swap:
|
2020-08-25 17:03:03 +01:00
|
|
|
if (!nir_deref_mode_is(nir_src_as_deref(instr->src[0]), nir_var_mem_ssbo))
|
2019-06-04 14:13:13 +02:00
|
|
|
break;
|
2020-08-25 17:03:03 +01:00
|
|
|
var = nir_get_binding_variable(state->shader, nir_chase_binding(instr->src[0]));
|
2019-06-04 14:13:13 +02:00
|
|
|
|
2020-08-25 16:50:24 +01:00
|
|
|
if (var) {
|
|
|
|
_mesa_set_add(state->vars_written, var);
|
|
|
|
} else {
|
|
|
|
nir_foreach_variable_with_modes(possible_var, state->shader, nir_var_mem_ssbo)
|
|
|
|
_mesa_set_add(state->vars_written, possible_var);
|
|
|
|
}
|
|
|
|
|
2019-06-04 14:13:13 +02:00
|
|
|
state->buffers_written = true;
|
2020-07-01 18:56:09 +10:00
|
|
|
break;
|
2019-06-04 14:13:13 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
process_variable(struct access_state *state, nir_variable *var)
|
|
|
|
{
|
2020-12-01 14:26:40 +00:00
|
|
|
const struct glsl_type *type = glsl_without_array(var->type);
|
2019-06-04 14:13:13 +02:00
|
|
|
if (var->data.mode != nir_var_mem_ssbo &&
|
2020-12-01 14:26:40 +00:00
|
|
|
!(var->data.mode == nir_var_uniform && glsl_type_is_image(type)))
|
2019-06-04 14:13:13 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Ignore variables we've already marked */
|
2019-11-07 16:53:58 -05:00
|
|
|
if (var->data.access & ACCESS_CAN_REORDER)
|
2019-06-04 14:13:13 +02:00
|
|
|
return false;
|
|
|
|
|
2020-12-01 14:34:48 +00:00
|
|
|
if (!(var->data.access & ACCESS_NON_WRITEABLE)) {
|
|
|
|
if ((var->data.access & ACCESS_RESTRICT) &&
|
|
|
|
!_mesa_set_search(state->vars_written, var)) {
|
|
|
|
var->data.access |= ACCESS_NON_WRITEABLE;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_buffer = var->data.mode == nir_var_mem_ssbo ||
|
|
|
|
glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF;
|
|
|
|
if (is_buffer ? !state->buffers_written : !state->images_written) {
|
|
|
|
var->data.access |= ACCESS_NON_WRITEABLE;
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-04 14:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2020-08-25 14:30:52 +01:00
|
|
|
update_access(struct access_state *state, nir_intrinsic_instr *instr, bool is_image, bool is_buffer)
|
2019-06-04 14:13:13 +02:00
|
|
|
{
|
2020-08-25 14:30:52 +01:00
|
|
|
enum gl_access_qualifier access = nir_intrinsic_access(instr);
|
2019-06-04 14:13:13 +02:00
|
|
|
|
2020-08-25 14:30:52 +01:00
|
|
|
bool is_memory_readonly = access & ACCESS_NON_WRITEABLE;
|
2019-06-04 14:13:13 +02:00
|
|
|
|
2020-08-25 14:30:52 +01:00
|
|
|
if (instr->intrinsic != nir_intrinsic_bindless_image_load) {
|
2020-08-25 17:03:03 +01:00
|
|
|
const nir_variable *var = nir_get_binding_variable(
|
|
|
|
state->shader, nir_chase_binding(instr->src[0]));
|
2020-08-25 16:50:24 +01:00
|
|
|
is_memory_readonly |= var && (var->data.access & ACCESS_NON_WRITEABLE);
|
2020-08-25 14:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
is_memory_readonly |= is_buffer ? !state->buffers_written : !state->images_written;
|
|
|
|
|
|
|
|
if (is_memory_readonly)
|
|
|
|
access |= ACCESS_NON_WRITEABLE;
|
|
|
|
if (!(access & ACCESS_VOLATILE) && is_memory_readonly)
|
|
|
|
access |= ACCESS_CAN_REORDER;
|
|
|
|
|
|
|
|
bool progress = nir_intrinsic_access(instr) != access;
|
|
|
|
nir_intrinsic_set_access(instr, access);
|
|
|
|
return progress;
|
2019-06-04 14:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
process_intrinsic(struct access_state *state, nir_intrinsic_instr *instr)
|
|
|
|
{
|
|
|
|
switch (instr->intrinsic) {
|
|
|
|
case nir_intrinsic_bindless_image_load:
|
2020-08-25 14:30:52 +01:00
|
|
|
return update_access(state, instr, true,
|
|
|
|
nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_BUF);
|
2019-06-04 14:13:13 +02:00
|
|
|
|
2020-08-25 14:30:52 +01:00
|
|
|
case nir_intrinsic_load_deref: {
|
2020-08-25 17:03:03 +01:00
|
|
|
if (!nir_deref_mode_is(nir_src_as_deref(instr->src[0]), nir_var_mem_ssbo))
|
2019-06-04 14:13:13 +02:00
|
|
|
return false;
|
|
|
|
|
2020-08-25 14:30:52 +01:00
|
|
|
return update_access(state, instr, false, true);
|
|
|
|
}
|
2019-06-04 14:13:13 +02:00
|
|
|
|
2020-08-25 14:30:52 +01:00
|
|
|
case nir_intrinsic_image_deref_load: {
|
|
|
|
nir_variable *var = nir_intrinsic_get_var(instr, 0);
|
2019-06-04 14:13:13 +02:00
|
|
|
|
2020-08-25 14:30:52 +01:00
|
|
|
bool is_buffer =
|
2019-06-04 14:13:13 +02:00
|
|
|
glsl_get_sampler_dim(glsl_without_array(var->type)) == GLSL_SAMPLER_DIM_BUF;
|
|
|
|
|
2020-08-25 14:30:52 +01:00
|
|
|
return update_access(state, instr, true, is_buffer);
|
2019-06-04 14:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
opt_access_impl(struct access_state *state,
|
|
|
|
nir_function_impl *impl)
|
|
|
|
{
|
|
|
|
bool progress = false;
|
|
|
|
|
|
|
|
nir_foreach_block(block, impl) {
|
|
|
|
nir_foreach_instr(instr, block) {
|
|
|
|
if (instr->type == nir_instr_type_intrinsic)
|
|
|
|
progress |= process_intrinsic(state,
|
|
|
|
nir_instr_as_intrinsic(instr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (progress) {
|
|
|
|
nir_metadata_preserve(impl,
|
|
|
|
nir_metadata_block_index |
|
|
|
|
nir_metadata_dominance |
|
|
|
|
nir_metadata_live_ssa_defs |
|
|
|
|
nir_metadata_loop_analysis);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-08-25 17:03:03 +01:00
|
|
|
nir_opt_access(nir_shader *shader, bool is_vulkan)
|
2019-06-04 14:13:13 +02:00
|
|
|
{
|
|
|
|
struct access_state state = {
|
2020-08-25 17:03:03 +01:00
|
|
|
.shader = shader,
|
2019-06-04 14:13:13 +02:00
|
|
|
.vars_written = _mesa_pointer_set_create(NULL),
|
|
|
|
};
|
|
|
|
|
2019-06-19 11:39:24 -07:00
|
|
|
bool var_progress = false;
|
2019-06-04 14:13:13 +02:00
|
|
|
bool progress = false;
|
|
|
|
|
|
|
|
nir_foreach_function(func, shader) {
|
|
|
|
if (func->impl) {
|
|
|
|
nir_foreach_block(block, func->impl) {
|
|
|
|
nir_foreach_instr(instr, block) {
|
|
|
|
if (instr->type == nir_instr_type_intrinsic)
|
|
|
|
gather_intrinsic(&state, nir_instr_as_intrinsic(instr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:03:03 +01:00
|
|
|
/* In Vulkan, buffers and images can alias. */
|
|
|
|
if (is_vulkan) {
|
|
|
|
state.buffers_written |= state.images_written;
|
|
|
|
state.images_written |= state.buffers_written;
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:30:37 -05:00
|
|
|
nir_foreach_variable_with_modes(var, shader, nir_var_uniform |
|
|
|
|
nir_var_mem_ubo |
|
|
|
|
nir_var_mem_ssbo)
|
2019-06-19 11:39:24 -07:00
|
|
|
var_progress |= process_variable(&state, var);
|
2019-06-04 14:13:13 +02:00
|
|
|
|
|
|
|
nir_foreach_function(func, shader) {
|
|
|
|
if (func->impl) {
|
|
|
|
progress |= opt_access_impl(&state, func->impl);
|
2019-06-19 11:39:24 -07:00
|
|
|
|
|
|
|
/* If we make a change to the uniforms, update all the impls. */
|
|
|
|
if (var_progress) {
|
|
|
|
nir_metadata_preserve(func->impl,
|
|
|
|
nir_metadata_block_index |
|
|
|
|
nir_metadata_dominance |
|
|
|
|
nir_metadata_live_ssa_defs |
|
|
|
|
nir_metadata_loop_analysis);
|
|
|
|
}
|
2019-06-04 14:13:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 11:39:24 -07:00
|
|
|
progress |= var_progress;
|
|
|
|
|
2019-06-04 14:13:13 +02:00
|
|
|
_mesa_set_destroy(state.vars_written, NULL);
|
|
|
|
return progress;
|
|
|
|
}
|