2015-10-24 19:30:31 +01:00
|
|
|
/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
* Rob Clark <robclark@freedesktop.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "freedreno_util.h"
|
|
|
|
|
|
|
|
#include "ir3_nir.h"
|
|
|
|
#include "ir3_compiler.h"
|
|
|
|
#include "ir3_shader.h"
|
|
|
|
|
|
|
|
#include "nir/tgsi_to_nir.h"
|
|
|
|
|
2016-03-28 15:28:29 +01:00
|
|
|
static const nir_shader_compiler_options options = {
|
|
|
|
.lower_fpow = true,
|
|
|
|
.lower_fsat = true,
|
|
|
|
.lower_scmp = true,
|
|
|
|
.lower_flrp32 = true,
|
|
|
|
.lower_flrp64 = true,
|
|
|
|
.lower_ffract = true,
|
2016-05-17 14:52:24 +01:00
|
|
|
.lower_fmod32 = true,
|
|
|
|
.lower_fmod64 = true,
|
2016-05-14 18:40:48 +01:00
|
|
|
.lower_fdiv = true,
|
2016-03-28 15:28:29 +01:00
|
|
|
.fuse_ffma = true,
|
|
|
|
.native_integers = true,
|
|
|
|
.vertex_id_zero_based = true,
|
|
|
|
.lower_extract_byte = true,
|
|
|
|
.lower_extract_word = true,
|
|
|
|
};
|
|
|
|
|
2015-10-24 19:30:31 +01:00
|
|
|
struct nir_shader *
|
|
|
|
ir3_tgsi_to_nir(const struct tgsi_token *tokens)
|
|
|
|
{
|
|
|
|
return tgsi_to_nir(tokens, &options);
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:28:29 +01:00
|
|
|
const nir_shader_compiler_options *
|
|
|
|
ir3_get_compiler_options(void)
|
|
|
|
{
|
|
|
|
return &options;
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:30:31 +01:00
|
|
|
/* for given shader key, are any steps handled in nir? */
|
|
|
|
bool
|
|
|
|
ir3_key_lowers_nir(const struct ir3_shader_key *key)
|
|
|
|
{
|
|
|
|
return key->fsaturate_s | key->fsaturate_t | key->fsaturate_r |
|
|
|
|
key->vsaturate_s | key->vsaturate_t | key->vsaturate_r |
|
2016-04-25 14:07:04 +01:00
|
|
|
key->ucp_enables | key->color_two_side |
|
|
|
|
key->fclamp_color | key->vclamp_color;
|
2015-10-24 19:30:31 +01:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:54:56 +01:00
|
|
|
#define OPT(nir, pass, ...) ({ \
|
|
|
|
bool this_progress = false; \
|
|
|
|
NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
|
|
|
|
this_progress; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define OPT_V(nir, pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
|
|
|
|
|
2015-10-24 19:30:31 +01:00
|
|
|
struct nir_shader *
|
|
|
|
ir3_optimize_nir(struct ir3_shader *shader, nir_shader *s,
|
|
|
|
const struct ir3_shader_key *key)
|
|
|
|
{
|
|
|
|
struct nir_lower_tex_options tex_options = {
|
|
|
|
.lower_rect = 0,
|
|
|
|
};
|
|
|
|
bool progress;
|
|
|
|
|
|
|
|
if (key) {
|
|
|
|
switch (shader->type) {
|
|
|
|
case SHADER_FRAGMENT:
|
|
|
|
case SHADER_COMPUTE:
|
|
|
|
tex_options.saturate_s = key->fsaturate_s;
|
|
|
|
tex_options.saturate_t = key->fsaturate_t;
|
|
|
|
tex_options.saturate_r = key->fsaturate_r;
|
|
|
|
break;
|
|
|
|
case SHADER_VERTEX:
|
|
|
|
tex_options.saturate_s = key->vsaturate_s;
|
|
|
|
tex_options.saturate_t = key->vsaturate_t;
|
|
|
|
tex_options.saturate_r = key->vsaturate_r;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shader->compiler->gpu_id >= 400) {
|
|
|
|
/* a4xx seems to have *no* sam.p */
|
|
|
|
tex_options.lower_txp = ~0; /* lower all txp */
|
|
|
|
} else {
|
|
|
|
/* a3xx just needs to avoid sam.p for 3d tex */
|
|
|
|
tex_options.lower_txp = (1 << GLSL_SAMPLER_DIM_3D);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd_mesa_debug & FD_DBG_DISASM) {
|
|
|
|
debug_printf("----------------------\n");
|
|
|
|
nir_print_shader(s, stdout);
|
|
|
|
debug_printf("----------------------\n");
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:54:56 +01:00
|
|
|
OPT_V(s, nir_opt_global_to_local);
|
|
|
|
OPT_V(s, nir_convert_to_ssa);
|
|
|
|
|
2015-10-24 19:30:31 +01:00
|
|
|
if (key) {
|
|
|
|
if (s->stage == MESA_SHADER_VERTEX) {
|
2015-10-24 19:54:56 +01:00
|
|
|
OPT_V(s, nir_lower_clip_vs, key->ucp_enables);
|
2016-04-25 14:07:04 +01:00
|
|
|
if (key->vclamp_color)
|
|
|
|
OPT_V(s, nir_lower_clamp_color_outputs);
|
2015-10-24 19:30:31 +01:00
|
|
|
} else if (s->stage == MESA_SHADER_FRAGMENT) {
|
2015-10-24 19:54:56 +01:00
|
|
|
OPT_V(s, nir_lower_clip_fs, key->ucp_enables);
|
2016-04-25 14:07:04 +01:00
|
|
|
if (key->fclamp_color)
|
|
|
|
OPT_V(s, nir_lower_clamp_color_outputs);
|
2015-10-24 19:30:31 +01:00
|
|
|
}
|
|
|
|
if (key->color_two_side) {
|
2015-10-24 19:54:56 +01:00
|
|
|
OPT_V(s, nir_lower_two_sided_color);
|
2015-10-24 19:30:31 +01:00
|
|
|
}
|
2016-04-23 14:03:28 +01:00
|
|
|
} else {
|
|
|
|
/* only want to do this the first time (when key is null)
|
|
|
|
* and not again on any potential 2nd variant lowering pass:
|
|
|
|
*/
|
|
|
|
OPT_V(s, ir3_nir_apply_trig_workarounds);
|
2015-10-24 19:30:31 +01:00
|
|
|
}
|
2015-10-24 19:54:56 +01:00
|
|
|
|
|
|
|
OPT_V(s, nir_lower_tex, &tex_options);
|
|
|
|
OPT_V(s, nir_lower_idiv);
|
|
|
|
OPT_V(s, nir_lower_load_const_to_scalar);
|
2015-10-24 19:30:31 +01:00
|
|
|
|
|
|
|
do {
|
|
|
|
progress = false;
|
|
|
|
|
2015-10-24 19:54:56 +01:00
|
|
|
OPT_V(s, nir_lower_vars_to_ssa);
|
|
|
|
OPT_V(s, nir_lower_alu_to_scalar);
|
|
|
|
OPT_V(s, nir_lower_phis_to_scalar);
|
2015-10-24 19:30:31 +01:00
|
|
|
|
2015-10-24 19:54:56 +01:00
|
|
|
progress |= OPT(s, nir_copy_prop);
|
|
|
|
progress |= OPT(s, nir_opt_dce);
|
|
|
|
progress |= OPT(s, nir_opt_cse);
|
|
|
|
progress |= OPT(s, ir3_nir_lower_if_else);
|
|
|
|
progress |= OPT(s, nir_opt_algebraic);
|
|
|
|
progress |= OPT(s, nir_opt_constant_folding);
|
2015-10-24 19:30:31 +01:00
|
|
|
|
|
|
|
} while (progress);
|
|
|
|
|
2016-03-25 21:26:11 +00:00
|
|
|
OPT_V(s, nir_remove_dead_variables, nir_var_local);
|
2015-10-24 19:30:31 +01:00
|
|
|
|
|
|
|
if (fd_mesa_debug & FD_DBG_DISASM) {
|
|
|
|
debug_printf("----------------------\n");
|
|
|
|
nir_print_shader(s, stdout);
|
|
|
|
debug_printf("----------------------\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
nir_sweep(s);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|