nir_shader * brw_create_nir(struct brw_context *brw, const struct gl_shader_program *shader_prog, const struct gl_program *prog, gl_shader_stage stage, bool is_scalar) { struct gl_context *ctx = &brw->ctx; const nir_shader_compiler_options *options = ctx->Const.ShaderCompilerOptions[stage].NirOptions; bool progress; nir_shader *nir; /* First, lower the GLSL IR or Mesa IR to NIR */ if (shader_prog) { nir = glsl_to_nir(shader_prog, stage, options); nir_remove_dead_variables(nir, nir_var_shader_in | nir_var_shader_out); NIR_PASS_V(nir, nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir), true, false); } else { nir = prog_to_nir(prog, options); NIR_PASS_V(nir, nir_convert_to_ssa); /* turn registers into SSA */ } nir_validate_shader(nir); (void)progress; nir = brw_preprocess_nir(brw->screen->compiler, nir); if (stage == MESA_SHADER_FRAGMENT) { static const struct nir_lower_wpos_ytransform_options wpos_options = { .state_tokens = {STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM, 0, 0, 0}, .fs_coord_pixel_center_integer = 1, .fs_coord_origin_upper_left = 1, }; _mesa_add_state_reference(prog->Parameters, (gl_state_index *) wpos_options.state_tokens); NIR_PASS(progress, nir, nir_lower_wpos_ytransform, &wpos_options); } NIR_PASS(progress, nir, nir_lower_system_values); NIR_PASS_V(nir, brw_nir_lower_uniforms, is_scalar); nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir)); if (shader_prog) { NIR_PASS_V(nir, nir_lower_samplers, shader_prog); NIR_PASS_V(nir, nir_lower_atomics, shader_prog); } return nir; }
nir_shader * brw_create_nir(struct brw_context *brw, const struct gl_shader_program *shader_prog, const struct gl_program *prog, gl_shader_stage stage, bool is_scalar) { struct gl_context *ctx = &brw->ctx; const nir_shader_compiler_options *options = ctx->Const.ShaderCompilerOptions[stage].NirOptions; struct gl_shader *shader = shader_prog ? shader_prog->_LinkedShaders[stage] : NULL; bool debug_enabled = INTEL_DEBUG & intel_debug_flag_for_shader_stage(stage); nir_shader *nir; /* First, lower the GLSL IR or Mesa IR to NIR */ if (shader_prog) { nir = glsl_to_nir(shader, options); } else { nir = prog_to_nir(prog, options); nir_convert_to_ssa(nir); /* turn registers into SSA */ } nir_validate_shader(nir); nir_lower_global_vars_to_local(nir); nir_validate_shader(nir); nir_lower_tex_projector(nir); nir_validate_shader(nir); nir_normalize_cubemap_coords(nir); nir_validate_shader(nir); nir_split_var_copies(nir); nir_validate_shader(nir); nir_optimize(nir, is_scalar); /* Lower a bunch of stuff */ nir_lower_var_copies(nir); nir_validate_shader(nir); /* Get rid of split copies */ nir_optimize(nir, is_scalar); if (is_scalar) { nir_assign_var_locations(&nir->uniforms, &nir->num_uniforms, type_size_scalar); nir_assign_var_locations(&nir->inputs, &nir->num_inputs, type_size_scalar); nir_assign_var_locations(&nir->outputs, &nir->num_outputs, type_size_scalar); nir_lower_io(nir, type_size_scalar); } else { nir_assign_var_locations(&nir->uniforms, &nir->num_uniforms, type_size_vec4); nir_assign_var_locations(&nir->inputs, &nir->num_inputs, type_size_vec4); foreach_list_typed(nir_variable, var, node, &nir->outputs) var->data.driver_location = var->data.location; nir_lower_io(nir, type_size_vec4); } nir_validate_shader(nir); nir_remove_dead_variables(nir); nir_validate_shader(nir); if (shader_prog) { nir_lower_samplers(nir, shader_prog); nir_validate_shader(nir); } nir_lower_system_values(nir); nir_validate_shader(nir); nir_lower_atomics(nir); nir_validate_shader(nir); nir_optimize(nir, is_scalar); if (brw->gen >= 6) { /* Try and fuse multiply-adds */ nir_opt_peephole_ffma(nir); nir_validate_shader(nir); } nir_opt_algebraic_late(nir); nir_validate_shader(nir); nir_lower_locals_to_regs(nir); nir_validate_shader(nir); nir_lower_to_source_mods(nir); nir_validate_shader(nir); nir_copy_prop(nir); nir_validate_shader(nir); nir_opt_dce(nir); nir_validate_shader(nir); if (unlikely(debug_enabled)) { /* Re-index SSA defs so we print more sensible numbers. */ nir_foreach_overload(nir, overload) { if (overload->impl) nir_index_ssa_defs(overload->impl); } fprintf(stderr, "NIR (SSA form) for %s shader:\n", _mesa_shader_stage_to_string(stage)); nir_print_shader(nir, stderr); } nir_convert_from_ssa(nir, is_scalar); nir_validate_shader(nir); if (!is_scalar) { nir_lower_vec_to_movs(nir); nir_validate_shader(nir); } /* This is the last pass we run before we start emitting stuff. It * determines when we need to insert boolean resolves on Gen <= 5. We * run it last because it stashes data in instr->pass_flags and we don't * want that to be squashed by other NIR passes. */ if (brw->gen <= 5) brw_nir_analyze_boolean_resolves(nir); nir_sweep(nir); if (unlikely(debug_enabled)) { fprintf(stderr, "NIR (final form) for %s shader:\n", _mesa_shader_stage_to_string(stage)); nir_print_shader(nir, stderr); } return nir; }
static void nir_optimize(nir_shader *nir, bool is_scalar) { bool progress; do { progress = false; nir_lower_vars_to_ssa(nir); nir_validate_shader(nir); if (is_scalar) { nir_lower_alu_to_scalar(nir); nir_validate_shader(nir); } progress |= nir_copy_prop(nir); nir_validate_shader(nir); if (is_scalar) { nir_lower_phis_to_scalar(nir); nir_validate_shader(nir); } progress |= nir_copy_prop(nir); nir_validate_shader(nir); progress |= nir_opt_dce(nir); nir_validate_shader(nir); progress |= nir_opt_cse(nir); nir_validate_shader(nir); progress |= nir_opt_peephole_select(nir); nir_validate_shader(nir); progress |= nir_opt_algebraic(nir); nir_validate_shader(nir); progress |= nir_opt_constant_folding(nir); nir_validate_shader(nir); progress |= nir_opt_remove_phis(nir); nir_validate_shader(nir); } while (progress); }
static nir_shader * create_passthrough_tcs(void *mem_ctx, const struct brw_compiler *compiler, const nir_shader_compiler_options *options, const struct brw_tcs_prog_key *key) { nir_builder b; nir_builder_init_simple_shader(&b, mem_ctx, MESA_SHADER_TESS_CTRL, options); nir_shader *nir = b.shader; nir_variable *var; nir_intrinsic_instr *load; nir_intrinsic_instr *store; nir_ssa_def *zero = nir_imm_int(&b, 0); nir_ssa_def *invoc_id = nir_load_system_value(&b, nir_intrinsic_load_invocation_id, 0); nir->info->inputs_read = key->outputs_written; nir->info->outputs_written = key->outputs_written; nir->info->tcs.vertices_out = key->input_vertices; nir->info->name = ralloc_strdup(nir, "passthrough"); nir->num_uniforms = 8 * sizeof(uint32_t); var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_0"); var->data.location = 0; var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_1"); var->data.location = 1; /* Write the patch URB header. */ for (int i = 0; i <= 1; i++) { load = nir_intrinsic_instr_create(nir, nir_intrinsic_load_uniform); load->num_components = 4; load->src[0] = nir_src_for_ssa(zero); nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL); nir_intrinsic_set_base(load, i * 4 * sizeof(uint32_t)); nir_builder_instr_insert(&b, &load->instr); store = nir_intrinsic_instr_create(nir, nir_intrinsic_store_output); store->num_components = 4; store->src[0] = nir_src_for_ssa(&load->dest.ssa); store->src[1] = nir_src_for_ssa(zero); nir_intrinsic_set_base(store, VARYING_SLOT_TESS_LEVEL_INNER - i); nir_intrinsic_set_write_mask(store, WRITEMASK_XYZW); nir_builder_instr_insert(&b, &store->instr); } /* Copy inputs to outputs. */ uint64_t varyings = key->outputs_written; while (varyings != 0) { const int varying = ffsll(varyings) - 1; load = nir_intrinsic_instr_create(nir, nir_intrinsic_load_per_vertex_input); load->num_components = 4; load->src[0] = nir_src_for_ssa(invoc_id); load->src[1] = nir_src_for_ssa(zero); nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL); nir_intrinsic_set_base(load, varying); nir_builder_instr_insert(&b, &load->instr); store = nir_intrinsic_instr_create(nir, nir_intrinsic_store_per_vertex_output); store->num_components = 4; store->src[0] = nir_src_for_ssa(&load->dest.ssa); store->src[1] = nir_src_for_ssa(invoc_id); store->src[2] = nir_src_for_ssa(zero); nir_intrinsic_set_base(store, varying); nir_intrinsic_set_write_mask(store, WRITEMASK_XYZW); nir_builder_instr_insert(&b, &store->instr); varyings &= ~BITFIELD64_BIT(varying); } nir_validate_shader(nir); nir = brw_preprocess_nir(compiler, nir); return nir; }