lima_program.c 14 KB
Newer Older
Qiang Yu's avatar
Qiang Yu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * Copyright (c) 2017-2019 Lima Project
 *
 * 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, sub license,
 * 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 NON-INFRINGEMENT. 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.
 *
 */

#include "util/u_memory.h"
#include "util/ralloc.h"
#include "util/u_debug.h"

#include "tgsi/tgsi_dump.h"
#include "compiler/nir/nir.h"
#include "nir/tgsi_to_nir.h"

#include "pipe/p_state.h"

#include "lima_screen.h"
#include "lima_context.h"
37
#include "lima_job.h"
Qiang Yu's avatar
Qiang Yu committed
38 39
#include "lima_program.h"
#include "lima_bo.h"
40 41
#include "lima_format.h"

Qiang Yu's avatar
Qiang Yu committed
42 43 44 45 46 47 48
#include "ir/lima_ir.h"

static const nir_shader_compiler_options vs_nir_options = {
   .lower_ffma = true,
   .lower_fpow = true,
   .lower_ffract = true,
   .lower_fdiv = true,
49
   .lower_fmod = true,
Qiang Yu's avatar
Qiang Yu committed
50 51 52 53 54 55
   .lower_fsqrt = true,
   .lower_sub = true,
   .lower_flrp32 = true,
   .lower_flrp64 = true,
   /* could be implemented by clamp */
   .lower_fsat = true,
56
   .lower_bitops = true,
57
   .lower_rotate = true,
58
   .lower_sincos = true,
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
59
   .lower_fceil = true,
Qiang Yu's avatar
Qiang Yu committed
60 61 62
};

static const nir_shader_compiler_options fs_nir_options = {
Andreas Baierl's avatar
Andreas Baierl committed
63
   .lower_ffma = true,
Qiang Yu's avatar
Qiang Yu committed
64 65
   .lower_fpow = true,
   .lower_fdiv = true,
66
   .lower_fmod = true,
Qiang Yu's avatar
Qiang Yu committed
67 68 69
   .lower_sub = true,
   .lower_flrp32 = true,
   .lower_flrp64 = true,
70
   .lower_fsign = true,
71
   .lower_rotate = true,
72
   .lower_fdot = true,
Erico Nunes's avatar
Erico Nunes committed
73
   .lower_fdph = true,
74
   .lower_bitops = true,
75
   .lower_vector_cmp = true,
Qiang Yu's avatar
Qiang Yu committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
};

const void *
lima_program_get_compiler_options(enum pipe_shader_type shader)
{
   switch (shader) {
   case PIPE_SHADER_VERTEX:
      return &vs_nir_options;
   case PIPE_SHADER_FRAGMENT:
      return &fs_nir_options;
   default:
      return NULL;
   }
}

static int
92
type_size(const struct glsl_type *type, bool bindless)
Qiang Yu's avatar
Qiang Yu committed
93 94 95 96
{
   return glsl_count_attribute_slots(type, false);
}

97
void
Qiang Yu's avatar
Qiang Yu committed
98 99 100 101
lima_program_optimize_vs_nir(struct nir_shader *s)
{
   bool progress;

102
   NIR_PASS_V(s, nir_lower_viewport_transform);
103
   NIR_PASS_V(s, nir_lower_point_size, 1.0f, 100.0f);
Qiang Yu's avatar
Qiang Yu committed
104 105 106 107 108 109 110 111 112 113
   NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
   NIR_PASS_V(s, nir_lower_load_const_to_scalar);
   NIR_PASS_V(s, lima_nir_lower_uniform_to_scalar);
   NIR_PASS_V(s, nir_lower_io_to_scalar,
              nir_var_shader_in|nir_var_shader_out);

   do {
      progress = false;

      NIR_PASS_V(s, nir_lower_vars_to_ssa);
114
      NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL, NULL);
Qiang Yu's avatar
Qiang Yu committed
115 116 117 118 119 120 121 122
      NIR_PASS(progress, s, nir_lower_phis_to_scalar);
      NIR_PASS(progress, s, nir_copy_prop);
      NIR_PASS(progress, s, nir_opt_remove_phis);
      NIR_PASS(progress, s, nir_opt_dce);
      NIR_PASS(progress, s, nir_opt_dead_cf);
      NIR_PASS(progress, s, nir_opt_cse);
      NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
      NIR_PASS(progress, s, nir_opt_algebraic);
123
      NIR_PASS(progress, s, lima_nir_lower_ftrunc);
Qiang Yu's avatar
Qiang Yu committed
124 125 126 127 128 129 130 131
      NIR_PASS(progress, s, nir_opt_constant_folding);
      NIR_PASS(progress, s, nir_opt_undef);
      NIR_PASS(progress, s, nir_opt_loop_unroll,
               nir_var_shader_in |
               nir_var_shader_out |
               nir_var_function_temp);
   } while (progress);

132
   NIR_PASS_V(s, nir_lower_int_to_float);
133 134
   /* int_to_float pass generates ftrunc, so lower it */
   NIR_PASS(progress, s, lima_nir_lower_ftrunc);
135 136
   NIR_PASS_V(s, nir_lower_bool_to_float);

137
   NIR_PASS_V(s, nir_copy_prop);
138
   NIR_PASS_V(s, nir_opt_dce);
Qiang Yu's avatar
Qiang Yu committed
139 140 141 142 143 144
   NIR_PASS_V(s, nir_lower_locals_to_regs);
   NIR_PASS_V(s, nir_convert_from_ssa, true);
   NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
   nir_sweep(s);
}

145 146
static bool
lima_alu_to_scalar_filter_cb(const nir_instr *instr, const void *data)
Qiang Yu's avatar
Qiang Yu committed
147
{
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
   if (instr->type != nir_instr_type_alu)
      return false;

   nir_alu_instr *alu = nir_instr_as_alu(instr);
   switch (alu->op) {
   case nir_op_frcp:
   case nir_op_frsq:
   case nir_op_flog2:
   case nir_op_fexp2:
   case nir_op_fsqrt:
   case nir_op_fsin:
   case nir_op_fcos:
      return true;
   default:
      break;
   }

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
   /* nir vec4 fcsel assumes that each component of the condition will be
    * used to select the same component from the two options, but Utgard PP
    * has only 1 component condition. If all condition components are not the
    * same we need to lower it to scalar.
    */
   switch (alu->op) {
   case nir_op_bcsel:
   case nir_op_fcsel:
      break;
   default:
      return false;
   }

   int num_components = nir_dest_num_components(alu->dest.dest);

   uint8_t swizzle = alu->src[0].swizzle[0];

   for (int i = 1; i < num_components; i++)
      if (alu->src[0].swizzle[i] != swizzle)
         return true;

186 187 188 189
   return false;
}

void
190 191
lima_program_optimize_fs_nir(struct nir_shader *s,
                             struct nir_lower_tex_options *tex_options)
192 193
{
   bool progress;
194

195
   NIR_PASS_V(s, nir_lower_fragcoord_wtrans);
Qiang Yu's avatar
Qiang Yu committed
196 197
   NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
   NIR_PASS_V(s, nir_lower_regs_to_ssa);
198
   NIR_PASS_V(s, nir_lower_tex, tex_options);
Qiang Yu's avatar
Qiang Yu committed
199

200 201 202 203 204
   do {
      progress = false;
      NIR_PASS(progress, s, nir_opt_vectorize);
   } while (progress);

Qiang Yu's avatar
Qiang Yu committed
205 206 207 208
   do {
      progress = false;

      NIR_PASS_V(s, nir_lower_vars_to_ssa);
209
      NIR_PASS(progress, s, nir_lower_alu_to_scalar, lima_alu_to_scalar_filter_cb, NULL);
Qiang Yu's avatar
Qiang Yu committed
210 211 212 213 214 215 216 217 218 219 220 221 222
      NIR_PASS(progress, s, nir_copy_prop);
      NIR_PASS(progress, s, nir_opt_remove_phis);
      NIR_PASS(progress, s, nir_opt_dce);
      NIR_PASS(progress, s, nir_opt_dead_cf);
      NIR_PASS(progress, s, nir_opt_cse);
      NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
      NIR_PASS(progress, s, nir_opt_algebraic);
      NIR_PASS(progress, s, nir_opt_constant_folding);
      NIR_PASS(progress, s, nir_opt_undef);
      NIR_PASS(progress, s, nir_opt_loop_unroll,
               nir_var_shader_in |
               nir_var_shader_out |
               nir_var_function_temp);
223
      NIR_PASS(progress, s, lima_nir_split_load_input);
Qiang Yu's avatar
Qiang Yu committed
224 225
   } while (progress);

226
   NIR_PASS_V(s, nir_lower_int_to_float);
227
   NIR_PASS_V(s, nir_lower_bool_to_float);
228

229 230 231 232 233 234 235
   /* Some ops must be lowered after being converted from int ops,
    * so re-run nir_opt_algebraic after int lowering. */
   do {
      progress = false;
      NIR_PASS(progress, s, nir_opt_algebraic);
   } while (progress);

236 237 238
   /* Must be run after optimization loop */
   NIR_PASS_V(s, lima_nir_scale_trig);

Qiang Yu's avatar
Qiang Yu committed
239 240 241 242 243 244 245 246 247 248 249 250
   /* Lower modifiers */
   NIR_PASS_V(s, nir_lower_to_source_mods, nir_lower_all_source_mods);
   NIR_PASS_V(s, nir_copy_prop);
   NIR_PASS_V(s, nir_opt_dce);

   NIR_PASS_V(s, nir_lower_locals_to_regs);
   NIR_PASS_V(s, nir_convert_from_ssa, true);
   NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);

   NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
   NIR_PASS_V(s, nir_lower_vec_to_movs);

251 252
   NIR_PASS_V(s, lima_nir_duplicate_load_uniforms);
   NIR_PASS_V(s, lima_nir_duplicate_load_inputs);
253
   NIR_PASS_V(s, lima_nir_duplicate_load_consts);
254

Qiang Yu's avatar
Qiang Yu committed
255 256 257
   nir_sweep(s);
}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
static bool
lima_fs_compile_shader(struct lima_context *ctx,
                       struct lima_fs_shader_state *fs,
                       struct nir_lower_tex_options *tex_options)
{
   struct lima_screen *screen = lima_screen(ctx->base.screen);
   nir_shader *nir = nir_shader_clone(fs, fs->base.ir.nir);

   lima_program_optimize_fs_nir(nir, tex_options);

   if (lima_debug & LIMA_DEBUG_PP)
      nir_print_shader(nir, stdout);

   if (!ppir_compile_nir(fs, nir, screen->pp_ra, &ctx->debug)) {
      ralloc_free(nir);
      return false;
   }

   fs->uses_discard = nir->info.fs.uses_discard;
   ralloc_free(nir);

   return true;
}

Qiang Yu's avatar
Qiang Yu committed
282 283 284 285
static void *
lima_create_fs_state(struct pipe_context *pctx,
                     const struct pipe_shader_state *cso)
{
286
   struct lima_context *ctx = lima_context(pctx);
Qiang Yu's avatar
Qiang Yu committed
287 288 289 290 291 292 293
   struct lima_fs_shader_state *so = rzalloc(NULL, struct lima_fs_shader_state);

   if (!so)
      return NULL;

   nir_shader *nir;
   if (cso->type == PIPE_SHADER_IR_NIR)
294 295 296
      /* The backend takes ownership of the NIR shader on state
       * creation.
       */
Qiang Yu's avatar
Qiang Yu committed
297 298 299 300
      nir = cso->ir.nir;
   else {
      assert(cso->type == PIPE_SHADER_IR_TGSI);

301
      nir = tgsi_to_nir(cso->tokens, pctx->screen, false);
Qiang Yu's avatar
Qiang Yu committed
302 303
   }

304 305
   so->base.type = PIPE_SHADER_IR_NIR;
   so->base.ir.nir = nir;
Qiang Yu's avatar
Qiang Yu committed
306

307 308 309 310 311 312 313 314 315
   uint8_t identity[4] = { PIPE_SWIZZLE_X,
                           PIPE_SWIZZLE_Y,
                           PIPE_SWIZZLE_Z,
                           PIPE_SWIZZLE_W };

   struct nir_lower_tex_options tex_options = {
      .lower_txp = ~0u,
      .swizzle_result = 0,
   };
Qiang Yu's avatar
Qiang Yu committed
316

317 318 319 320 321
   /* Initialize with identity swizzles. That should suffice for most shaders  */
   for (int i = 0; i < PIPE_MAX_SAMPLERS; i++)
      memcpy(so->swizzles[i], identity, 4);

   if (!lima_fs_compile_shader(ctx, so, &tex_options)) {
Qiang Yu's avatar
Qiang Yu committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
      ralloc_free(so);
      return NULL;
   }

   return so;
}

static void
lima_bind_fs_state(struct pipe_context *pctx, void *hwcso)
{
   struct lima_context *ctx = lima_context(pctx);

   ctx->fs = hwcso;
   ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_FRAG;
}

static void
lima_delete_fs_state(struct pipe_context *pctx, void *hwcso)
{
   struct lima_fs_shader_state *so = hwcso;

   if (so->bo)
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
344
      lima_bo_unreference(so->bo);
Qiang Yu's avatar
Qiang Yu committed
345

346
   ralloc_free(so->base.ir.nir);
Qiang Yu's avatar
Qiang Yu committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
   ralloc_free(so);
}

bool
lima_update_vs_state(struct lima_context *ctx)
{
   struct lima_vs_shader_state *vs = ctx->vs;
   if (!vs->bo) {
      struct lima_screen *screen = lima_screen(ctx->base.screen);
      vs->bo = lima_bo_create(screen, vs->shader_size, 0);
      if (!vs->bo) {
         fprintf(stderr, "lima: create vs shader bo fail\n");
         return false;
      }

      memcpy(lima_bo_map(vs->bo), vs->shader, vs->shader_size);
      ralloc_free(vs->shader);
      vs->shader = NULL;
   }

   return true;
}

bool
lima_update_fs_state(struct lima_context *ctx)
{
   struct lima_fs_shader_state *fs = ctx->fs;
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
   struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj;
   struct nir_lower_tex_options tex_options = {
      .lower_txp = ~0u,
      .swizzle_result = 0,
   };
   bool needs_recompile = false;

   /* Check if texture formats has changed since last compilation.
    * If it has we need to recompile shader.
    */
   if (((ctx->dirty & LIMA_CONTEXT_DIRTY_TEXTURES) &&
       lima_tex->num_samplers &&
       lima_tex->num_textures)) {
      uint8_t identity[4] = { PIPE_SWIZZLE_X,
                              PIPE_SWIZZLE_Y,
                              PIPE_SWIZZLE_Z,
                              PIPE_SWIZZLE_W };
      for (int i = 0; i < lima_tex->num_samplers; i++) {
         struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]);
         struct pipe_resource *prsc = texture->base.texture;
         const uint8_t *swizzle = lima_format_get_texel_swizzle(prsc->format);
         if (memcmp(fs->swizzles[i], swizzle, 4)) {
            needs_recompile = true;
            memcpy(fs->swizzles[i], swizzle, 4);
         }

         for (int j = 0; j < 4; j++)
            tex_options.swizzles[i][j] = swizzle[j];

         if (memcmp(swizzle, identity, 4))
            tex_options.swizzle_result |= (1 << i);
      }

      /* Fill rest with identity swizzle */
      for (int i = lima_tex->num_samplers; i < PIPE_MAX_SAMPLERS; i++)
         memcpy(fs->swizzles[i], identity, 4);
   }

   if (needs_recompile) {
      if (fs->bo) {
         lima_bo_unreference(fs->bo);
         fs->bo = NULL;
      }

      if (!lima_fs_compile_shader(ctx, fs, &tex_options))
         return false;
   }

Qiang Yu's avatar
Qiang Yu committed
422 423 424 425 426 427 428 429 430 431 432 433 434
   if (!fs->bo) {
      struct lima_screen *screen = lima_screen(ctx->base.screen);
      fs->bo = lima_bo_create(screen, fs->shader_size, 0);
      if (!fs->bo) {
         fprintf(stderr, "lima: create fs shader bo fail\n");
         return false;
      }

      memcpy(lima_bo_map(fs->bo), fs->shader, fs->shader_size);
      ralloc_free(fs->shader);
      fs->shader = NULL;
   }

435 436
   struct lima_job *job = lima_job_get(ctx);
   job->pp_max_stack_size = MAX2(job->pp_max_stack_size, ctx->fs->stack_size);
437

Qiang Yu's avatar
Qiang Yu committed
438 439 440 441 442 443 444
   return true;
}

static void *
lima_create_vs_state(struct pipe_context *pctx,
                     const struct pipe_shader_state *cso)
{
445
   struct lima_context *ctx = lima_context(pctx);
Qiang Yu's avatar
Qiang Yu committed
446 447 448 449 450 451 452 453 454 455 456
   struct lima_vs_shader_state *so = rzalloc(NULL, struct lima_vs_shader_state);

   if (!so)
      return NULL;

   nir_shader *nir;
   if (cso->type == PIPE_SHADER_IR_NIR)
      nir = cso->ir.nir;
   else {
      assert(cso->type == PIPE_SHADER_IR_TGSI);

457
      nir = tgsi_to_nir(cso->tokens, pctx->screen, false);
Qiang Yu's avatar
Qiang Yu committed
458 459 460 461 462 463 464
   }

   lima_program_optimize_vs_nir(nir);

   if (lima_debug & LIMA_DEBUG_GP)
      nir_print_shader(nir, stdout);

465
   if (!gpir_compile_nir(so, nir, &ctx->debug)) {
Qiang Yu's avatar
Qiang Yu committed
466 467 468 469
      ralloc_free(so);
      return NULL;
   }

Erico Nunes's avatar
Erico Nunes committed
470 471
   ralloc_free(nir);

Qiang Yu's avatar
Qiang Yu committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
   return so;
}

static void
lima_bind_vs_state(struct pipe_context *pctx, void *hwcso)
{
   struct lima_context *ctx = lima_context(pctx);

   ctx->vs = hwcso;
   ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_VERT;
}

static void
lima_delete_vs_state(struct pipe_context *pctx, void *hwcso)
{
   struct lima_vs_shader_state *so = hwcso;

   if (so->bo)
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
490
      lima_bo_unreference(so->bo);
Qiang Yu's avatar
Qiang Yu committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

   ralloc_free(so);
}

void
lima_program_init(struct lima_context *ctx)
{
   ctx->base.create_fs_state = lima_create_fs_state;
   ctx->base.bind_fs_state = lima_bind_fs_state;
   ctx->base.delete_fs_state = lima_delete_fs_state;

   ctx->base.create_vs_state = lima_create_vs_state;
   ctx->base.bind_vs_state = lima_bind_vs_state;
   ctx->base.delete_vs_state = lima_delete_vs_state;
}