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; }
static nir_shader * load_glsl(unsigned num_files, char* const* files, gl_shader_stage stage) { static const struct standalone_options options = { .glsl_version = 140, .do_link = true, }; struct gl_shader_program *prog; prog = standalone_compile_shader(&options, num_files, files); if (!prog) errx(1, "couldn't parse `%s'", files[0]); nir_shader *nir = glsl_to_nir(prog, stage, ir3_get_compiler_options()); standalone_compiler_cleanup(prog); /* required NIR passes: */ /* TODO cmdline args for some of the conditional lowering passes? */ NIR_PASS_V(nir, nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir), true, true); NIR_PASS_V(nir, nir_lower_global_vars_to_local); NIR_PASS_V(nir, nir_split_var_copies); NIR_PASS_V(nir, nir_lower_var_copies); NIR_PASS_V(nir, nir_split_var_copies); NIR_PASS_V(nir, nir_lower_var_copies); NIR_PASS_V(nir, nir_lower_io_types); // TODO nir_assign_var_locations?? NIR_PASS_V(nir, nir_lower_system_values); NIR_PASS_V(nir, nir_lower_io, nir_var_all, st_glsl_type_size); NIR_PASS_V(nir, nir_lower_samplers, prog); return nir; } static int read_file(const char *filename, void **ptr, size_t *size) { int fd, ret; struct stat st; *ptr = MAP_FAILED; fd = open(filename, O_RDONLY); if (fd == -1) { warnx("couldn't open `%s'", filename); return 1; } ret = fstat(fd, &st); if (ret) errx(1, "couldn't stat `%s'", filename); *size = st.st_size; *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); if (*ptr == MAP_FAILED) errx(1, "couldn't map `%s'", filename); close(fd); return 0; }