From 0a179bb6e26b487356384c561dee6aefa9af5889 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 24 Aug 2020 13:07:35 -0700 Subject: [PATCH] st/mesa: Generate NIR for ATI_fragment_shader instead of TGSI. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Compiling NIR is much less code, gives us optimization across drivers, and is one less chunk of TGSI in the mesa frontend. Reviewed-by: Marek Olšák Part-of: --- .gitlab-ci/windows/quick_gl.txt | 11 +- src/mesa/Makefile.sources | 4 +- src/mesa/meson.build | 4 +- src/mesa/program/prog_to_nir.c | 30 +- src/mesa/program/prog_to_nir.h | 3 +- src/mesa/state_tracker/st_atifs_to_nir.c | 606 +++++++++++++ .../{st_atifs_to_tgsi.h => st_atifs_to_nir.h} | 26 +- src/mesa/state_tracker/st_atifs_to_tgsi.c | 835 ------------------ src/mesa/state_tracker/st_atom_shader.c | 24 +- src/mesa/state_tracker/st_cb_program.c | 2 +- src/mesa/state_tracker/st_program.c | 87 +- src/mesa/state_tracker/st_program.h | 2 +- 12 files changed, 696 insertions(+), 938 deletions(-) create mode 100644 src/mesa/state_tracker/st_atifs_to_nir.c rename src/mesa/state_tracker/{st_atifs_to_tgsi.h => st_atifs_to_nir.h} (71%) delete mode 100644 src/mesa/state_tracker/st_atifs_to_tgsi.c diff --git a/.gitlab-ci/windows/quick_gl.txt b/.gitlab-ci/windows/quick_gl.txt index 8844e39382b..562891d497b 100644 --- a/.gitlab-ci/windows/quick_gl.txt +++ b/.gitlab-ci/windows/quick_gl.txt @@ -3765,13 +3765,6 @@ spec/arb_viewport_array/scissor-check: skip spec/arb_viewport_array/scissor-indices: skip spec/arb_viewport_array/viewport-indices: skip spec/ati_envmap_bumpmap/ati_envmap_bumpmap-bump: skip -spec/ati_fragment_shader/ati_fragment_shader-render-constants: crash -spec/ati_fragment_shader/ati_fragment_shader-render-default: crash -spec/ati_fragment_shader/ati_fragment_shader-render-fog: crash -spec/ati_fragment_shader/ati_fragment_shader-render-notexture: crash -spec/ati_fragment_shader/ati_fragment_shader-render-precedence: crash -spec/ati_fragment_shader/ati_fragment_shader-render-sources: crash -spec/ati_fragment_shader/ati_fragment_shader-render-textargets: crash spec/ati_texture_compression_3dc/fbo-generatemipmap-formats: skip spec/ati_texture_compression_3dc/texwrap formats: skip spec/ati_texture_compression_3dc/texwrap formats bordercolor: skip @@ -4749,9 +4742,9 @@ wgl/wgl-sanity: skip summary: name: results ---- -------- - pass: 13247 + pass: 13254 fail: 539 - crash: 75 + crash: 68 skip: 4114 timeout: 0 warn: 10 diff --git a/src/mesa/Makefile.sources b/src/mesa/Makefile.sources index ff7a18306fc..3ec14d1607f 100644 --- a/src/mesa/Makefile.sources +++ b/src/mesa/Makefile.sources @@ -432,8 +432,8 @@ VBO_FILES = \ vbo/vbo_util.h STATETRACKER_FILES = \ - state_tracker/st_atifs_to_tgsi.c \ - state_tracker/st_atifs_to_tgsi.h \ + state_tracker/st_atifs_to_nir.c \ + state_tracker/st_atifs_to_nir.h \ state_tracker/st_atom_array.c \ state_tracker/st_atom_atomicbuf.c \ state_tracker/st_atom_blend.c \ diff --git a/src/mesa/meson.build b/src/mesa/meson.build index 681e8bdfdeb..d3dd1b8a7fa 100644 --- a/src/mesa/meson.build +++ b/src/mesa/meson.build @@ -469,8 +469,8 @@ files_libmesa_classic = files( ) files_libmesa_gallium = files( - 'state_tracker/st_atifs_to_tgsi.c', - 'state_tracker/st_atifs_to_tgsi.h', + 'state_tracker/st_atifs_to_nir.c', + 'state_tracker/st_atifs_to_nir.h', 'state_tracker/st_atom_array.c', 'state_tracker/st_atom_atomicbuf.c', 'state_tracker/st_atom_blend.c', diff --git a/src/mesa/program/prog_to_nir.c b/src/mesa/program/prog_to_nir.c index 7bd503363e8..79fe021775e 100644 --- a/src/mesa/program/prog_to_nir.c +++ b/src/mesa/program/prog_to_nir.c @@ -468,9 +468,18 @@ ptn_kil(nir_builder *b, nir_ssa_def **src) } enum glsl_sampler_dim -_mesa_texture_index_to_sampler_dim(gl_texture_index index) +_mesa_texture_index_to_sampler_dim(gl_texture_index index, bool *is_array) { + *is_array = false; + switch (index) { + case TEXTURE_2D_MULTISAMPLE_INDEX: + return GLSL_SAMPLER_DIM_MS; + case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX: + *is_array = true; + return GLSL_SAMPLER_DIM_MS; + case TEXTURE_BUFFER_INDEX: + return GLSL_SAMPLER_DIM_BUF; case TEXTURE_1D_INDEX: return GLSL_SAMPLER_DIM_1D; case TEXTURE_2D_INDEX: @@ -479,11 +488,23 @@ _mesa_texture_index_to_sampler_dim(gl_texture_index index) return GLSL_SAMPLER_DIM_3D; case TEXTURE_CUBE_INDEX: return GLSL_SAMPLER_DIM_CUBE; + case TEXTURE_CUBE_ARRAY_INDEX: + *is_array = true; + return GLSL_SAMPLER_DIM_CUBE; case TEXTURE_RECT_INDEX: return GLSL_SAMPLER_DIM_RECT; - default: - unreachable("unknown texture target"); + case TEXTURE_1D_ARRAY_INDEX: + *is_array = true; + return GLSL_SAMPLER_DIM_1D; + case TEXTURE_2D_ARRAY_INDEX: + *is_array = true; + return GLSL_SAMPLER_DIM_2D; + case TEXTURE_EXTERNAL_INDEX: + return GLSL_SAMPLER_DIM_EXTERNAL; + case NUM_TEXTURE_TARGETS: + break; } + unreachable("unknown texture target"); } static void @@ -532,7 +553,8 @@ ptn_tex(struct ptn_compile *c, nir_alu_dest dest, nir_ssa_def **src, instr->dest_type = nir_type_float; instr->is_shadow = prog_inst->TexShadow; - instr->sampler_dim = _mesa_texture_index_to_sampler_dim(prog_inst->TexSrcTarget); + bool is_array; + instr->sampler_dim = _mesa_texture_index_to_sampler_dim(prog_inst->TexSrcTarget, &is_array); instr->coord_components = glsl_get_sampler_dim_coordinate_components(instr->sampler_dim); diff --git a/src/mesa/program/prog_to_nir.h b/src/mesa/program/prog_to_nir.h index 2d8c7c7a5e5..cfdfc64cd91 100644 --- a/src/mesa/program/prog_to_nir.h +++ b/src/mesa/program/prog_to_nir.h @@ -34,7 +34,8 @@ extern "C" { struct nir_shader *prog_to_nir(const struct gl_program *prog, const nir_shader_compiler_options *options); -enum glsl_sampler_dim _mesa_texture_index_to_sampler_dim(gl_texture_index index); +enum glsl_sampler_dim _mesa_texture_index_to_sampler_dim(gl_texture_index index, + bool *is_array); #ifdef __cplusplus } diff --git a/src/mesa/state_tracker/st_atifs_to_nir.c b/src/mesa/state_tracker/st_atifs_to_nir.c new file mode 100644 index 00000000000..df33b1dc041 --- /dev/null +++ b/src/mesa/state_tracker/st_atifs_to_nir.c @@ -0,0 +1,606 @@ +/* + * Copyright (C) 2016 Miklós Máté + * Copyright (C) 2020 Google LLC + * + * 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 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. + */ + +#include "main/mtypes.h" +#include "main/atifragshader.h" +#include "main/errors.h" +#include "program/prog_parameter.h" +#include "program/prog_instruction.h" +#include "program/prog_to_nir.h" + +#include "st_program.h" +#include "st_atifs_to_nir.h" +#include "compiler/nir/nir_builder.h" + +#define FOG_PARAMS_UNIFORM (MAX_NUM_FRAGMENT_CONSTANTS_ATI + 0) +#define FOG_COLOR_UNIFORM (MAX_NUM_FRAGMENT_CONSTANTS_ATI + 1) + +/** + * Intermediate state used during shader translation. + */ +struct st_translate { + nir_builder *b; + struct ati_fragment_shader *atifs; + const struct st_fp_variant_key *key; + + nir_ssa_def *temps[MAX_PROGRAM_TEMPS]; + + nir_variable *fragcolor; + nir_variable *constants; + nir_variable *samplers[MAX_TEXTURE_UNITS]; + + nir_ssa_def *inputs[VARYING_SLOT_MAX]; + + unsigned current_pass; + + bool regs_written[MAX_NUM_PASSES_ATI][MAX_NUM_FRAGMENT_REGISTERS_ATI]; + + boolean error; +}; + +static nir_ssa_def * +nir_channel_vec4(nir_builder *b, nir_ssa_def *src, unsigned channel) +{ + unsigned swizzle[4] = { channel, channel, channel, channel }; + return nir_swizzle(b, src, swizzle, 4); +} + +static nir_ssa_def * +nir_imm_vec4_float(nir_builder *b, float f) +{ + return nir_channel_vec4(b, nir_imm_float(b, f), 0); +} + +static nir_ssa_def * +get_temp(struct st_translate *t, unsigned index) +{ + if (!t->temps[index]) + t->temps[index] = nir_ssa_undef(t->b, 4, 32); + return t->temps[index]; +} + +static nir_ssa_def * +apply_swizzle(struct st_translate *t, + struct nir_ssa_def *src, GLuint swizzle) +{ + /* From the ATI_fs spec: + * + * "Table 3.20 shows the modes: + * + * Coordinates Used for 1D or Coordinates Used for + * Swizzle 2D SampleMap and PassTexCoord 3D or cubemap SampleMap + * ------- ----------------------------- ----------------------- + * SWIZZLE_STR_ATI (s, t, r, undefined) (s, t, r, undefined) + * SWIZZLE_STQ_ATI (s, t, q, undefined) (s, t, q, undefined) + * SWIZZLE_STR_DR_ATI (s/r, t/r, 1/r, undefined) (undefined) + * SWIZZLE_STQ_DQ_ATI (s/q, t/q, 1/q, undefined) (undefined) + */ + if (swizzle == GL_SWIZZLE_STR_ATI) { + return src; + } else if (swizzle == GL_SWIZZLE_STQ_ATI) { + static unsigned xywz[4] = { 0, 1, 3, 2 }; + return nir_swizzle(t->b, src, xywz, 4); + } else { + nir_ssa_def *rcp = nir_frcp(t->b, nir_channel(t->b, src, + swizzle == GL_SWIZZLE_STR_DR_ATI ? 2 : 3)); + + nir_ssa_def *st_mul = nir_fmul(t->b, nir_channels(t->b, src, 0x3), rcp); + + return nir_vec4(t->b, + nir_channel(t->b, st_mul, 0), + nir_channel(t->b, st_mul, 1), + rcp, + rcp); + } +} + +static nir_ssa_def * +load_input(struct st_translate *t, gl_varying_slot slot) +{ + if (!t->inputs[slot]) { + nir_variable *var = nir_variable_create(t->b->shader, nir_var_shader_in, + slot == VARYING_SLOT_FOGC ? + glsl_float_type() : glsl_vec4_type(), + gl_varying_slot_name(slot)); + var->data.location = slot; + var->data.interpolation = INTERP_MODE_NONE; + + t->inputs[slot] = nir_load_var(t->b, var); + } + + return t->inputs[slot]; +} + +static nir_ssa_def * +atifs_load_uniform(struct st_translate *t, int index) +{ + nir_deref_instr *deref = nir_build_deref_array(t->b, + nir_build_deref_var(t->b, t->constants), + nir_imm_int(t->b, index)); + return nir_load_deref(t->b, deref); +} + +static struct nir_ssa_def * +get_source(struct st_translate *t, GLenum src_type) +{ + if (src_type >= GL_REG_0_ATI && src_type <= GL_REG_5_ATI) { + if (t->regs_written[t->current_pass][src_type - GL_REG_0_ATI]) { + return get_temp(t, src_type - GL_REG_0_ATI); + } else { + return nir_imm_vec4_float(t->b, 0.0); + } + } else if (src_type >= GL_CON_0_ATI && src_type <= GL_CON_7_ATI) { + int index = src_type - GL_CON_0_ATI; + if (t->atifs->LocalConstDef & (1 << index)) { + return nir_imm_vec4(t->b, + t->atifs->Constants[index][0], + t->atifs->Constants[index][1], + t->atifs->Constants[index][2], + t->atifs->Constants[index][3]); + } else { + return atifs_load_uniform(t, index); + } + } else if (src_type == GL_ZERO) { + return nir_imm_vec4_float(t->b, 0.0); + } else if (src_type == GL_ONE) { + return nir_imm_vec4_float(t->b, 1.0); + } else if (src_type == GL_PRIMARY_COLOR_ARB) { + return load_input(t, VARYING_SLOT_COL0); + } else if (src_type == GL_SECONDARY_INTERPOLATOR_ATI) { + return load_input(t, VARYING_SLOT_COL1); + } else { + /* frontend prevents this */ + unreachable("unknown source"); + } +} + +static nir_ssa_def * +prepare_argument(struct st_translate *t, const struct atifs_instruction *inst, + const unsigned argId, bool alpha) +{ + if (argId >= inst->ArgCount[alpha]) { + _mesa_warning(0, "Using 0 for missing argument %d\n", argId); + return nir_imm_vec4_float(t->b, 0.0f); + } + + const struct atifragshader_src_register *srcReg = &inst->SrcReg[alpha][argId]; + + nir_ssa_def *src = get_source(t, srcReg->Index); + + switch (srcReg->argRep) { + case GL_NONE: + break; + case GL_RED: + src = nir_channel_vec4(t->b, src, 0); + break; + case GL_GREEN: + src = nir_channel_vec4(t->b, src, 1); + break; + case GL_BLUE: + src = nir_channel_vec4(t->b, src, 2); + break; + case GL_ALPHA: + src = nir_channel_vec4(t->b, src, 3); + break; + } + + t->temps[MAX_NUM_FRAGMENT_REGISTERS_ATI + argId] = src; + + if (srcReg->argMod & GL_COMP_BIT_ATI) + src = nir_fsub(t->b, nir_imm_vec4_float(t->b, 1.0), src); + if (srcReg->argMod & GL_BIAS_BIT_ATI) + src = nir_fadd(t->b, src, nir_imm_vec4_float(t->b, -0.5)); + if (srcReg->argMod & GL_2X_BIT_ATI) + src = nir_fadd(t->b, src, src); + if (srcReg->argMod & GL_NEGATE_BIT_ATI) + src = nir_fneg(t->b, src); + + return src; +} + +static nir_ssa_def * +emit_arith_inst(struct st_translate *t, + const struct atifs_instruction *inst, + bool alpha) +{ + nir_ssa_def *src[3] = {0}; + for (int i = 0; i < inst->ArgCount[alpha]; i++) + src[i] = prepare_argument(t, inst, i, alpha); + + switch (inst->Opcode[alpha]) { + case GL_MOV_ATI: + return src[0]; + + case GL_ADD_ATI: + return nir_fadd(t->b, src[0], src[1]); + + case GL_SUB_ATI: + return nir_fsub(t->b, src[0], src[1]); + + case GL_MUL_ATI: + return nir_fmul(t->b, src[0], src[1]); + + case GL_MAD_ATI: + return nir_ffma(t->b, src[0], src[1], src[2]); + + case GL_LERP_ATI: + return nir_flrp(t->b, src[2], src[1], src[0]); + + case GL_CND_ATI: + return nir_bcsel(t->b, + nir_fge(t->b, nir_imm_vec4_float(t->b, 0.5), src[2]), + src[1], + src[0]); + + case GL_CND0_ATI: + return nir_bcsel(t->b, + nir_fge(t->b, src[2], nir_imm_vec4_float(t->b, 0.0)), + src[0], + src[1]); + + case GL_DOT2_ADD_ATI: + return nir_channel_vec4(t->b, + nir_fadd(t->b, + nir_fdot2(t->b, src[0], src[1]), + nir_channel(t->b, src[1], 2)), + 0); + + case GL_DOT3_ATI: + return nir_channel_vec4(t->b, nir_fdot3(t->b,src[0], src[1]), 0); + + case GL_DOT4_ATI: + return nir_channel_vec4(t->b, nir_fdot4(t->b,src[0], src[1]), 0); + + default: + unreachable("Unknown ATI_fs opcode"); + } +} + +static nir_ssa_def * +emit_dstmod(struct st_translate *t, + struct nir_ssa_def *dst, GLuint dstMod) +{ + switch (dstMod & ~GL_SATURATE_BIT_ATI) { + case GL_2X_BIT_ATI: + dst = nir_fmul_imm(t->b, dst, 2.0f); + break; + case GL_4X_BIT_ATI: + dst = nir_fmul_imm(t->b, dst, 4.0f); + break; + case GL_8X_BIT_ATI: + dst = nir_fmul_imm(t->b, dst, 8.0f); + break; + case GL_HALF_BIT_ATI: + dst = nir_fmul_imm(t->b, dst, 0.5f); + break; + case GL_QUARTER_BIT_ATI: + dst = nir_fmul_imm(t->b, dst, 0.25f); + break; + case GL_EIGHTH_BIT_ATI: + dst = nir_fmul_imm(t->b, dst, 0.125f); + break; + default: + break; + } + + if (dstMod & GL_SATURATE_BIT_ATI) + dst = nir_fsat(t->b, dst); + + return dst; +} + +/** + * Compile one setup instruction to NIR instructions. + */ +static void +compile_setupinst(struct st_translate *t, + const unsigned r, + const struct atifs_setupinst *texinst) +{ + if (!texinst->Opcode) + return; + + GLuint pass_tex = texinst->src; + + nir_ssa_def *coord; + + if (pass_tex >= GL_TEXTURE0_ARB && pass_tex <= GL_TEXTURE7_ARB) { + unsigned attr = pass_tex - GL_TEXTURE0_ARB; + + coord = load_input(t, VARYING_SLOT_TEX0 + attr); + } else if (pass_tex >= GL_REG_0_ATI && pass_tex <= GL_REG_5_ATI) { + unsigned reg = pass_tex - GL_REG_0_ATI; + + /* the frontend already validated that REG is only allowed in second pass */ + if (t->regs_written[0][reg]) { + coord = t->temps[reg]; + } else { + coord = nir_imm_vec4_float(t->b, 0.0f); + } + } else { + coord = nir_ssa_undef(t->b, 4, 32); + } + coord = apply_swizzle(t, coord, texinst->swizzle); + + if (texinst->Opcode == ATI_FRAGMENT_SHADER_SAMPLE_OP) { + nir_variable *tex_var = t->samplers[r]; + if (!tex_var) { + bool is_array; + enum glsl_sampler_dim sampler_dim = + _mesa_texture_index_to_sampler_dim(t->key->texture_index[r], &is_array); + const struct glsl_type *sampler_type = + glsl_sampler_type(sampler_dim, false, false, GLSL_TYPE_FLOAT); + + tex_var = nir_variable_create(t->b->shader, nir_var_uniform, sampler_type, "tex"); + tex_var->data.binding = r; + tex_var->data.explicit_binding = true; + t->samplers[r] = tex_var; + } + nir_deref_instr *tex_deref = nir_build_deref_var(t->b, t->samplers[r]); + + nir_tex_instr *tex = nir_tex_instr_create(t->b->shader, 3); + tex->op = nir_texop_tex; + tex->sampler_dim = glsl_get_sampler_dim(tex_var->type); + tex->dest_type = nir_type_float; + tex->coord_components = + glsl_get_sampler_dim_coordinate_components(tex->sampler_dim); + + tex->src[0].src_type = nir_tex_src_texture_deref; + tex->src[0].src = nir_src_for_ssa(&tex_deref->dest.ssa); + tex->src[1].src_type = nir_tex_src_sampler_deref; + tex->src[1].src = nir_src_for_ssa(&tex_deref->dest.ssa); + tex->src[2].src_type = nir_tex_src_coord; + tex->src[2].src = + nir_src_for_ssa(nir_channels(t->b, coord, + (1 << tex->coord_components) - 1)); + + nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL); + nir_builder_instr_insert(t->b, &tex->instr); + + t->temps[r] = &tex->dest.ssa; + } else if (texinst->Opcode == ATI_FRAGMENT_SHADER_PASS_OP) { + t->temps[r] = coord; + } + + t->regs_written[t->current_pass][r] = true; +} + +/** + * Compile one arithmetic operation COLOR&ALPHA pair into NIR instructions. + */ +static void +compile_instruction(struct st_translate *t, + const struct atifs_instruction *inst) +{ + unsigned optype; + + for (optype = 0; optype < 2; optype++) { /* color, alpha */ + unsigned dstreg = inst->DstReg[optype].Index - GL_REG_0_ATI; + + if (!inst->Opcode[optype]) + continue; + + /* Execute the op */ + nir_ssa_def *result = emit_arith_inst(t, inst, optype); + result = emit_dstmod(t, result, inst->DstReg[optype].dstMod); + + /* Do the writemask */ + nir_const_value wrmask[4] = { 0 }; + for (int i = 0; i < 4; i++) { + if (inst->DstReg[optype].dstMask & (1 << i)) + wrmask[i].b = 1; + } + + t->temps[dstreg] = nir_bcsel(t->b, + nir_build_imm(t->b, 4, 1, wrmask), + result, + get_temp(t, dstreg)); + t->regs_written[t->current_pass][dstreg] = true; + } +} + + +/* Creates the uniform variable referencing the ATI_fragment_shader constants + * plus the optimized fog state. + */ +static void +st_atifs_setup_uniforms(struct st_translate *t, struct gl_program *program) +{ + const struct glsl_type *type = + glsl_array_type(glsl_vec4_type(), program->Parameters->NumParameters, 0); + t->constants = + nir_variable_create(t->b->shader, nir_var_uniform, type, + "gl_ATI_fragment_shader_constants"); +} + +/** + * Called when a new variant is needed, we need to translate + * the ATI fragment shader to NIR + */ +nir_shader * +st_translate_atifs_program(struct ati_fragment_shader *atifs, + const struct st_fp_variant_key *key, + struct gl_program *program, + const nir_shader_compiler_options *options) +{ + nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT, options, "ATI_fs"); + + struct st_translate translate = { + .atifs = atifs, + .b = &b, + .key = key, + }; + struct st_translate *t = &translate; + + /* Copy the shader_info from the gl_program */ + t->b->shader->info = program->info; + + nir_shader *s = t->b->shader; + s->info.name = ralloc_asprintf(s, "ATIFS%d", program->Id); + + t->fragcolor = nir_variable_create(b.shader, nir_var_shader_out, + glsl_vec4_type(), "gl_FragColor"); + t->fragcolor->data.location = FRAG_RESULT_COLOR; + + st_atifs_setup_uniforms(t, program); + + /* emit instructions */ + for (unsigned pass = 0; pass < atifs->NumPasses; pass++) { + t->current_pass = pass; + for (unsigned r = 0; r < MAX_NUM_FRAGMENT_REGISTERS_ATI; r++) { + struct atifs_setupinst *texinst = &atifs->SetupInst[pass][r]; + compile_setupinst(t, r, texinst); + } + for (unsigned i = 0; i < atifs->numArithInstr[pass]; i++) { + struct atifs_instruction *inst = &atifs->Instructions[pass][i]; + compile_instruction(t, inst); + } + } + + if (t->regs_written[atifs->NumPasses-1][0]) { + nir_ssa_def *color = t->temps[0]; + + if (key->fog) { + nir_ssa_def *fogc = load_input(t, VARYING_SLOT_FOGC); + + nir_ssa_def *params = atifs_load_uniform(t, FOG_PARAMS_UNIFORM); + + /* compute the 1 component fog factor f */ + nir_ssa_def *f = NULL; + if (key->fog == FOG_LINEAR) { + f = nir_ffma(t->b, fogc, + nir_channel(t->b, params, 0), + nir_channel(t->b, params, 1)); + } else if (key->fog == FOG_EXP) { + /* EXP formula: f = exp(-dens * z) + * with optimized parameters: + * f = MUL(fogcoord, oparams.z); f= EX2(-f) + */ + f = nir_fmul(t->b, fogc, nir_channel(t->b, params, 2)); + f = nir_fexp2(t->b, nir_fneg(t->b, f)); + } else if (key->fog == FOG_EXP2) { + /* EXP2 formula: f = exp(-(dens * z)^2) + * with optimized parameters: + * f = MUL(fogcoord, oparams.w); f=MUL(f, f); f= EX2(-f) + */ + f = nir_fmul(t->b, fogc, nir_channel(t->b, params, 3)); + f = nir_fmul(t->b, f, f); + f = nir_fexp2(t->b, nir_fneg(t->b, f)); + } + f = nir_fsat(t->b, f); + + nir_ssa_def *fog_color = nir_flrp(t->b, + atifs_load_uniform(t, FOG_COLOR_UNIFORM), + color, + f); + color = nir_vec4(t->b, + nir_channel(t->b, fog_color, 0), + nir_channel(t->b, fog_color, 1), + nir_channel(t->b, fog_color, 2), + nir_channel(t->b, color, 3)); + } + + nir_store_var(t->b, t->fragcolor, color, 0xf); + } + + return b.shader; +} + +/** + * Called in ProgramStringNotify, we need to fill the metadata of the + * gl_program attached to the ati_fragment_shader + */ +void +st_init_atifs_prog(struct gl_context *ctx, struct gl_program *prog) +{ + /* we know this is st_fragment_program, because of st_new_ati_fs() */ + struct st_program *stfp = (struct st_program *) prog; + struct ati_fragment_shader *atifs = stfp->ati_fs; + + unsigned pass, i, r, optype, arg; + + static const gl_state_index16 fog_params_state[STATE_LENGTH] = + {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED, 0, 0}; + static const gl_state_index16 fog_color[STATE_LENGTH] = + {STATE_FOG_COLOR, 0, 0, 0}; + + prog->info.inputs_read = 0; + prog->info.outputs_written = BITFIELD64_BIT(FRAG_RESULT_COLOR); + prog->SamplersUsed = 0; + prog->Parameters = _mesa_new_parameter_list(); + + /* fill in inputs_read, SamplersUsed, TexturesUsed */ + for (pass = 0; pass < atifs->NumPasses; pass++) { + for (r = 0; r < MAX_NUM_FRAGMENT_REGISTERS_ATI; r++) { + struct atifs_setupinst *texinst = &atifs->SetupInst[pass][r]; + GLuint pass_tex = texinst->src; + + if (texinst->Opcode == ATI_FRAGMENT_SHADER_SAMPLE_OP) { + /* mark which texcoords are used */ + prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_TEX0 + pass_tex - GL_TEXTURE0_ARB); + /* by default there is 1:1 mapping between samplers and textures */ + prog->SamplersUsed |= (1 << r); + /* the target is unknown here, it will be fixed in the draw call */ + prog->TexturesUsed[r] = TEXTURE_2D_BIT; + } else if (texinst->Opcode == ATI_FRAGMENT_SHADER_PASS_OP) { + if (pass_tex >= GL_TEXTURE0_ARB && pass_tex <= GL_TEXTURE7_ARB) { + prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_TEX0 + pass_tex - GL_TEXTURE0_ARB); + } + } + } + } + for (pass = 0; pass < atifs->NumPasses; pass++) { + for (i = 0; i < atifs->numArithInstr[pass]; i++) { + struct atifs_instruction *inst = &atifs->Instructions[pass][i]; + + for (optype = 0; optype < 2; optype++) { /* color, alpha */ + if (inst->Opcode[optype]) { + for (arg = 0; arg < inst->ArgCount[optype]; arg++) { + GLint index = inst->SrcReg[optype][arg].Index; + if (index == GL_PRIMARY_COLOR_EXT) { + prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_COL0); + } else if (index == GL_SECONDARY_INTERPOLATOR_ATI) { + /* note: ATI_fragment_shader.txt never specifies what + * GL_SECONDARY_INTERPOLATOR_ATI is, swrast uses + * VARYING_SLOT_COL1 for this input */ + prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_COL1); + } + } + } + } + } + } + /* we may need fog */ + prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_FOGC); + + /* we always have the ATI_fs constants, and the fog params */ + for (i = 0; i < MAX_NUM_FRAGMENT_CONSTANTS_ATI; i++) { + _mesa_add_parameter(prog->Parameters, PROGRAM_UNIFORM, + NULL, 4, GL_FLOAT, NULL, NULL, true); + } + ASSERTED uint32_t ref; + ref = _mesa_add_state_reference(prog->Parameters, fog_params_state); + assert(ref == FOG_PARAMS_UNIFORM); + ref = _mesa_add_state_reference(prog->Parameters, fog_color); + assert(ref == FOG_COLOR_UNIFORM); +} diff --git a/src/mesa/state_tracker/st_atifs_to_tgsi.h b/src/mesa/state_tracker/st_atifs_to_nir.h similarity index 71% rename from src/mesa/state_tracker/st_atifs_to_tgsi.h rename to src/mesa/state_tracker/st_atifs_to_nir.h index ce547911cfe..db6d7a3296d 100644 --- a/src/mesa/state_tracker/st_atifs_to_tgsi.h +++ b/src/mesa/state_tracker/st_atifs_to_nir.h @@ -25,6 +25,7 @@ #include "main/glheader.h" #include "pipe/p_defines.h" +#include "compiler/nir/nir.h" #if defined __cplusplus extern "C" { @@ -32,34 +33,19 @@ extern "C" { struct gl_context; struct gl_program; -struct ureg_program; -struct tgsi_token; struct ati_fragment_shader; struct st_fp_variant_key; -enum pipe_error -st_translate_atifs_program( - struct ureg_program *ureg, - struct ati_fragment_shader *atifs, - struct gl_program *program, - GLuint numInputs, - const ubyte inputMapping[], - const ubyte inputSemanticName[], - const ubyte inputSemanticIndex[], - const ubyte interpMode[], - GLuint numOutputs, - const ubyte outputMapping[], - const ubyte outputSemanticName[], - const ubyte outputSemanticIndex[]); +nir_shader * +st_translate_atifs_program(struct ati_fragment_shader *atifs, + const struct st_fp_variant_key *key, + struct gl_program *program, + const nir_shader_compiler_options *options); void st_init_atifs_prog(struct gl_context *ctx, struct gl_program *prog); -const struct tgsi_token * -st_fixup_atifs(const struct tgsi_token *tokens, - const struct st_fp_variant_key *key); - #if defined __cplusplus } /* extern "C" */ #endif diff --git a/src/mesa/state_tracker/st_atifs_to_tgsi.c b/src/mesa/state_tracker/st_atifs_to_tgsi.c deleted file mode 100644 index 0de2d53321c..00000000000 --- a/src/mesa/state_tracker/st_atifs_to_tgsi.c +++ /dev/null @@ -1,835 +0,0 @@ -/* - * Copyright (C) 2016 Miklós Máté - * - * 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 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. - */ - -#include "main/mtypes.h" -#include "main/atifragshader.h" -#include "main/errors.h" -#include "program/prog_parameter.h" - -#include "tgsi/tgsi_ureg.h" -#include "tgsi/tgsi_scan.h" -#include "tgsi/tgsi_transform.h" - -#include "st_program.h" -#include "st_atifs_to_tgsi.h" - -/** - * Intermediate state used during shader translation. - */ -struct st_translate { - struct ureg_program *ureg; - struct ati_fragment_shader *atifs; - - struct ureg_dst temps[MAX_PROGRAM_TEMPS]; - struct ureg_src *constants; - struct ureg_dst outputs[PIPE_MAX_SHADER_OUTPUTS]; - struct ureg_src inputs[PIPE_MAX_SHADER_INPUTS]; - struct ureg_src samplers[PIPE_MAX_SAMPLERS]; - - const ubyte *inputMapping; - const ubyte *outputMapping; - - unsigned current_pass; - - bool regs_written[MAX_NUM_PASSES_ATI][MAX_NUM_FRAGMENT_REGISTERS_ATI]; - - boolean error; -}; - -struct instruction_desc { - unsigned TGSI_opcode; - const char *name; - unsigned char arg_count; -}; - -static const struct instruction_desc inst_desc[] = { - {TGSI_OPCODE_MOV, "MOV", 1}, - {TGSI_OPCODE_NOP, "UND", 0}, /* unused */ - {TGSI_OPCODE_ADD, "ADD", 2}, - {TGSI_OPCODE_MUL, "MUL", 2}, - {TGSI_OPCODE_NOP, "SUB", 2}, - {TGSI_OPCODE_DP3, "DOT3", 2}, - {TGSI_OPCODE_DP4, "DOT4", 2}, - {TGSI_OPCODE_MAD, "MAD", 3}, - {TGSI_OPCODE_LRP, "LERP", 3}, - {TGSI_OPCODE_NOP, "CND", 3}, - {TGSI_OPCODE_NOP, "CND0", 3}, - {TGSI_OPCODE_NOP, "DOT2_ADD", 3} -}; - -static struct ureg_dst -get_temp(struct st_translate *t, unsigned index) -{ - if (ureg_dst_is_undef(t->temps[index])) - t->temps[index] = ureg_DECL_temporary(t->ureg); - return t->temps[index]; -} - -static struct ureg_src -apply_swizzle(struct st_translate *t, - struct ureg_src src, GLuint swizzle) -{ - if (swizzle == GL_SWIZZLE_STR_ATI) { - return src; - } else if (swizzle == GL_SWIZZLE_STQ_ATI) { - return ureg_swizzle(src, - TGSI_SWIZZLE_X, - TGSI_SWIZZLE_Y, - TGSI_SWIZZLE_W, - TGSI_SWIZZLE_Z); - } else { - struct ureg_dst tmp[2]; - struct ureg_src imm[3]; - - tmp[0] = get_temp(t, MAX_NUM_FRAGMENT_REGISTERS_ATI); - tmp[1] = get_temp(t, MAX_NUM_FRAGMENT_REGISTERS_ATI + 1); - imm[0] = src; - imm[1] = ureg_imm4f(t->ureg, 1.0f, 1.0f, 0.0f, 0.0f); - imm[2] = ureg_imm4f(t->ureg, 0.0f, 0.0f, 1.0f, 1.0f); - ureg_insn(t->ureg, TGSI_OPCODE_MAD, &tmp[0], 1, imm, 3, 0); - - if (swizzle == GL_SWIZZLE_STR_DR_ATI) { - imm[0] = ureg_scalar(src, TGSI_SWIZZLE_Z); - } else { - imm[0] = ureg_scalar(src, TGSI_SWIZZLE_W); - } - ureg_insn(t->ureg, TGSI_OPCODE_RCP, &tmp[1], 1, &imm[0], 1, 0); - - imm[0] = ureg_src(tmp[0]); - imm[1] = ureg_src(tmp[1]); - ureg_insn(t->ureg, TGSI_OPCODE_MUL, &tmp[0], 1, imm, 2, 0); - - return ureg_src(tmp[0]); - } -} - -static struct ureg_src -get_source(struct st_translate *t, GLuint src_type) -{ - if (src_type >= GL_REG_0_ATI && src_type <= GL_REG_5_ATI) { - if (t->regs_written[t->current_pass][src_type - GL_REG_0_ATI]) { - return ureg_src(get_temp(t, src_type - GL_REG_0_ATI)); - } else { - return ureg_imm1f(t->ureg, 0.0f); - } - } else if (src_type >= GL_CON_0_ATI && src_type <= GL_CON_7_ATI) { - return t->constants[src_type - GL_CON_0_ATI]; - } else if (src_type == GL_ZERO) { - return ureg_imm1f(t->ureg, 0.0f); - } else if (src_type == GL_ONE) { - return ureg_imm1f(t->ureg, 1.0f); - } else if (src_type == GL_PRIMARY_COLOR_ARB) { - return t->inputs[t->inputMapping[VARYING_SLOT_COL0]]; - } else if (src_type == GL_SECONDARY_INTERPOLATOR_ATI) { - return t->inputs[t->inputMapping[VARYING_SLOT_COL1]]; - } else { - /* frontend prevents this */ - unreachable("unknown source"); - } -} - -static struct ureg_src -prepare_argument(struct st_translate *t, const unsigned argId, - const struct atifragshader_src_register *srcReg) -{ - struct ureg_src src = get_source(t, srcReg->Index); - struct ureg_dst arg = get_temp(t, MAX_NUM_FRAGMENT_REGISTERS_ATI + argId); - - switch (srcReg->argRep) { - case GL_NONE: - break; - case GL_RED: - src = ureg_scalar(src, TGSI_SWIZZLE_X); - break; - case GL_GREEN: - src = ureg_scalar(src, TGSI_SWIZZLE_Y); - break; - case GL_BLUE: - src = ureg_scalar(src, TGSI_SWIZZLE_Z); - break; - case GL_ALPHA: - src = ureg_scalar(src, TGSI_SWIZZLE_W); - break; - } - ureg_insn(t->ureg, TGSI_OPCODE_MOV, &arg, 1, &src, 1, 0); - - if (srcReg->argMod & GL_COMP_BIT_ATI) { - struct ureg_src modsrc[2]; - modsrc[0] = ureg_imm1f(t->ureg, 1.0f); - modsrc[1] = ureg_negate(ureg_src(arg)); - - ureg_insn(t->ureg, TGSI_OPCODE_ADD, &arg, 1, modsrc, 2, 0); - } - if (srcReg->argMod & GL_BIAS_BIT_ATI) { - struct ureg_src modsrc[2]; - modsrc[0] = ureg_src(arg); - modsrc[1] = ureg_imm1f(t->ureg, -0.5f); - - ureg_insn(t->ureg, TGSI_OPCODE_ADD, &arg, 1, modsrc, 2, 0); - } - if (srcReg->argMod & GL_2X_BIT_ATI) { - struct ureg_src modsrc[2]; - modsrc[0] = ureg_src(arg); - modsrc[1] = ureg_src(arg); - - ureg_insn(t->ureg, TGSI_OPCODE_ADD, &arg, 1, modsrc, 2, 0); - } - if (srcReg->argMod & GL_NEGATE_BIT_ATI) { - struct ureg_src modsrc[2]; - modsrc[0] = ureg_src(arg); - modsrc[1] = ureg_imm1f(t->ureg, -1.0f); - - ureg_insn(t->ureg, TGSI_OPCODE_MUL, &arg, 1, modsrc, 2, 0); - } - return ureg_src(arg); -} - -/* These instructions need special treatment */ -static void -emit_special_inst(struct st_translate *t, const struct instruction_desc *desc, - struct ureg_dst *dst, struct ureg_src *args, unsigned argcount) -{ - struct ureg_dst tmp[1]; - struct ureg_src src[3]; - - if (!strcmp(desc->name, "SUB")) { - ureg_ADD(t->ureg, *dst, args[0], ureg_negate(args[1])); - } else if (!strcmp(desc->name, "CND")) { - tmp[0] = get_temp(t, MAX_NUM_FRAGMENT_REGISTERS_ATI + 2); /* re-purpose a3 */ - src[0] = ureg_imm1f(t->ureg, 0.5f); - src[1] = ureg_negate(args[2]); - ureg_insn(t->ureg, TGSI_OPCODE_ADD, tmp, 1, src, 2, 0); - src[0] = ureg_src(tmp[0]); - src[1] = args[0]; - src[2] = args[1]; - ureg_insn(t->ureg, TGSI_OPCODE_CMP, dst, 1, src, 3, 0); - } else if (!strcmp(desc->name, "CND0")) { - src[0] = args[2]; - src[1] = args[1]; - src[2] = args[0]; - ureg_insn(t->ureg, TGSI_OPCODE_CMP, dst, 1, src, 3, 0); - } else if (!strcmp(desc->name, "DOT2_ADD")) { - tmp[0] = get_temp(t, MAX_NUM_FRAGMENT_REGISTERS_ATI); /* re-purpose a1 */ - src[0] = args[0]; - src[1] = args[1]; - ureg_insn(t->ureg, TGSI_OPCODE_DP2, tmp, 1, src, 2, 0); - src[0] = ureg_src(tmp[0]); - src[1] = ureg_scalar(args[2], TGSI_SWIZZLE_Z); - ureg_insn(t->ureg, TGSI_OPCODE_ADD, dst, 1, src, 2, 0); - } -} - -static void -emit_arith_inst(struct st_translate *t, - const struct instruction_desc *desc, - struct ureg_dst *dst, struct ureg_src *args, unsigned argcount) -{ - if (desc->TGSI_opcode == TGSI_OPCODE_NOP) { - emit_special_inst(t, desc, dst, args, argcount); - return; - } - - ureg_insn(t->ureg, desc->TGSI_opcode, dst, 1, args, argcount, 0); -} - -static void -emit_dstmod(struct st_translate *t, - struct ureg_dst dst, GLuint dstMod) -{ - float imm; - struct ureg_src src[3]; - GLuint scale = dstMod & ~GL_SATURATE_BIT_ATI; - - if (dstMod == GL_NONE) { - return; - } - - switch (scale) { - case GL_2X_BIT_ATI: - imm = 2.0f; - break; - case GL_4X_BIT_ATI: - imm = 4.0f; - break; - case GL_8X_BIT_ATI: - imm = 8.0f; - break; - case GL_HALF_BIT_ATI: - imm = 0.5f; - break; - case GL_QUARTER_BIT_ATI: - imm = 0.25f; - break; - case GL_EIGHTH_BIT_ATI: - imm = 0.125f; - break; - default: - imm = 1.0f; - } - - src[0] = ureg_src(dst); - src[1] = ureg_imm1f(t->ureg, imm); - if (dstMod & GL_SATURATE_BIT_ATI) { - dst = ureg_saturate(dst); - } - ureg_insn(t->ureg, TGSI_OPCODE_MUL, &dst, 1, src, 2, 0); -} - -/** - * Compile one setup instruction to TGSI instructions. - */ -static void -compile_setupinst(struct st_translate *t, - const unsigned r, - const struct atifs_setupinst *texinst) -{ - struct ureg_dst dst[1]; - struct ureg_src src[2]; - - if (!texinst->Opcode) - return; - - dst[0] = get_temp(t, r); - - GLuint pass_tex = texinst->src; - - if (pass_tex >= GL_TEXTURE0_ARB && pass_tex <= GL_TEXTURE7_ARB) { - unsigned attr = pass_tex - GL_TEXTURE0_ARB + VARYING_SLOT_TEX0; - - src[0] = t->inputs[t->inputMapping[attr]]; - } else if (pass_tex >= GL_REG_0_ATI && pass_tex <= GL_REG_5_ATI) { - unsigned reg = pass_tex - GL_REG_0_ATI; - - /* the frontend already validated that REG is only allowed in second pass */ - if (t->regs_written[0][reg]) { - src[0] = ureg_src(t->temps[reg]); - } else { - src[0] = ureg_imm1f(t->ureg, 0.0f); - } - } - src[0] = apply_swizzle(t, src[0], texinst->swizzle); - - if (texinst->Opcode == ATI_FRAGMENT_SHADER_SAMPLE_OP) { - /* by default texture and sampler indexes are the same */ - src[1] = t->samplers[r]; - /* the texture target is still unknown, it will be fixed in the draw call */ - ureg_tex_insn(t->ureg, TGSI_OPCODE_TEX, dst, 1, TGSI_TEXTURE_2D, - TGSI_RETURN_TYPE_FLOAT, NULL, 0, src, 2); - } else if (texinst->Opcode == ATI_FRAGMENT_SHADER_PASS_OP) { - ureg_insn(t->ureg, TGSI_OPCODE_MOV, dst, 1, src, 1, 0); - } - - t->regs_written[t->current_pass][r] = true; -} - -/** - * Compile one arithmetic operation COLOR&ALPHA pair into TGSI instructions. - */ -static void -compile_instruction(struct st_translate *t, - const struct atifs_instruction *inst) -{ - unsigned optype; - - for (optype = 0; optype < 2; optype++) { /* color, alpha */ - const struct instruction_desc *desc; - struct ureg_dst dst[1]; - struct ureg_src args[3]; /* arguments for the main operation */ - unsigned arg; - unsigned dstreg = inst->DstReg[optype].Index - GL_REG_0_ATI; - - if (!inst->Opcode[optype]) - continue; - - desc = &inst_desc[inst->Opcode[optype] - GL_MOV_ATI]; - - /* prepare the arguments */ - for (arg = 0; arg < desc->arg_count; arg++) { - if (arg >= inst->ArgCount[optype]) { - _mesa_warning(0, "Using 0 for missing argument %d of %s\n", - arg, desc->name); - args[arg] = ureg_imm1f(t->ureg, 0.0f); - } else { - args[arg] = prepare_argument(t, arg, - &inst->SrcReg[optype][arg]); - } - } - - /* prepare dst */ - dst[0] = get_temp(t, dstreg); - - dst[0] = ureg_writemask(dst[0], inst->DstReg[optype].dstMask); - - /* emit the main instruction */ - emit_arith_inst(t, desc, dst, args, arg); - - emit_dstmod(t, *dst, inst->DstReg[optype].dstMod); - - t->regs_written[t->current_pass][dstreg] = true; - } -} - -static void -finalize_shader(struct st_translate *t, unsigned numPasses) -{ - struct ureg_dst dst[1] = { { 0 } }; - struct ureg_src src[1] = { { 0 } }; - - if (t->regs_written[numPasses-1][0]) { - /* copy the result into the OUT slot */ - dst[0] = t->outputs[t->outputMapping[FRAG_RESULT_COLOR]]; - src[0] = ureg_src(t->temps[0]); - ureg_insn(t->ureg, TGSI_OPCODE_MOV, dst, 1, src, 1, 0); - } - - /* signal the end of the program */ - ureg_insn(t->ureg, TGSI_OPCODE_END, dst, 0, src, 0, 0); -} - -/** - * Called when a new variant is needed, we need to translate - * the ATI fragment shader to TGSI - */ -enum pipe_error -st_translate_atifs_program( - struct ureg_program *ureg, - struct ati_fragment_shader *atifs, - struct gl_program *program, - GLuint numInputs, - const ubyte inputMapping[], - const ubyte inputSemanticName[], - const ubyte inputSemanticIndex[], - const ubyte interpMode[], - GLuint numOutputs, - const ubyte outputMapping[], - const ubyte outputSemanticName[], - const ubyte outputSemanticIndex[]) -{ - enum pipe_error ret = PIPE_OK; - - unsigned pass, i, r; - - struct st_translate translate, *t; - t = &translate; - memset(t, 0, sizeof *t); - - t->inputMapping = inputMapping; - t->outputMapping = outputMapping; - t->ureg = ureg; - t->atifs = atifs; - - /* - * Declare input attributes. - */ - for (i = 0; i < numInputs; i++) { - t->inputs[i] = ureg_DECL_fs_input(ureg, - inputSemanticName[i], - inputSemanticIndex[i], - interpMode[i]); - } - - /* - * Declare output attributes: - * we always have numOutputs=1 and it's FRAG_RESULT_COLOR - */ - t->outputs[0] = ureg_DECL_output(ureg, - TGSI_SEMANTIC_COLOR, - outputSemanticIndex[0]); - - /* Emit constants and immediates. Mesa uses a single index space - * for these, so we put all the translated regs in t->constants. - */ - if (program->Parameters) { - t->constants = calloc(program->Parameters->NumParameters, - sizeof t->constants[0]); - if (t->constants == NULL) { - ret = PIPE_ERROR_OUT_OF_MEMORY; - goto out; - } - - for (i = 0; i < program->Parameters->NumParameters; i++) { - unsigned pvo = program->Parameters->Parameters[i].ValueOffset; - - switch (program->Parameters->Parameters[i].Type) { - case PROGRAM_STATE_VAR: - case PROGRAM_UNIFORM: - t->constants[i] = ureg_DECL_constant(ureg, i); - break; - case PROGRAM_CONSTANT: - t->constants[i] = - ureg_DECL_immediate(ureg, - (const float*)program->Parameters->ParameterValues + pvo, - 4); - break; - default: - break; - } - } - } - - /* texture samplers */ - for (i = 0; i < MAX_NUM_FRAGMENT_REGISTERS_ATI; i++) { - if (program->SamplersUsed & (1 << i)) { - t->samplers[i] = ureg_DECL_sampler(ureg, i); - /* the texture target is still unknown, it will be fixed in the draw call */ - ureg_DECL_sampler_view(ureg, i, TGSI_TEXTURE_2D, - TGSI_RETURN_TYPE_FLOAT, - TGSI_RETURN_TYPE_FLOAT, - TGSI_RETURN_TYPE_FLOAT, - TGSI_RETURN_TYPE_FLOAT); - } - } - - /* emit instructions */ - for (pass = 0; pass < atifs->NumPasses; pass++) { - t->current_pass = pass; - for (r = 0; r < MAX_NUM_FRAGMENT_REGISTERS_ATI; r++) { - struct atifs_setupinst *texinst = &atifs->SetupInst[pass][r]; - compile_setupinst(t, r, texinst); - } - for (i = 0; i < atifs->numArithInstr[pass]; i++) { - struct atifs_instruction *inst = &atifs->Instructions[pass][i]; - compile_instruction(t, inst); - } - } - - finalize_shader(t, atifs->NumPasses); - -out: - free(t->constants); - - if (t->error) { - debug_printf("%s: translate error flag set\n", __func__); - } - - return ret; -} - -/** - * Called in ProgramStringNotify, we need to fill the metadata of the - * gl_program attached to the ati_fragment_shader - */ -void -st_init_atifs_prog(struct gl_context *ctx, struct gl_program *prog) -{ - /* we know this is st_fragment_program, because of st_new_ati_fs() */ - struct st_program *stfp = (struct st_program *) prog; - struct ati_fragment_shader *atifs = stfp->ati_fs; - - unsigned pass, i, r, optype, arg; - - static const gl_state_index16 fog_params_state[STATE_LENGTH] = - {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED, 0, 0 }; - static const gl_state_index16 fog_color[STATE_LENGTH] = - {STATE_FOG_COLOR, 0, 0, 0 }; - - prog->info.inputs_read = 0; - prog->info.outputs_written = BITFIELD64_BIT(FRAG_RESULT_COLOR); - prog->SamplersUsed = 0; - prog->Parameters = _mesa_new_parameter_list(); - - /* fill in inputs_read, SamplersUsed, TexturesUsed */ - for (pass = 0; pass < atifs->NumPasses; pass++) { - for (r = 0; r < MAX_NUM_FRAGMENT_REGISTERS_ATI; r++) { - struct atifs_setupinst *texinst = &atifs->SetupInst[pass][r]; - GLuint pass_tex = texinst->src; - - if (texinst->Opcode == ATI_FRAGMENT_SHADER_SAMPLE_OP) { - /* mark which texcoords are used */ - prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_TEX0 + pass_tex - GL_TEXTURE0_ARB); - /* by default there is 1:1 mapping between samplers and textures */ - prog->SamplersUsed |= (1 << r); - /* the target is unknown here, it will be fixed in the draw call */ - prog->TexturesUsed[r] = TEXTURE_2D_BIT; - } else if (texinst->Opcode == ATI_FRAGMENT_SHADER_PASS_OP) { - if (pass_tex >= GL_TEXTURE0_ARB && pass_tex <= GL_TEXTURE7_ARB) { - prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_TEX0 + pass_tex - GL_TEXTURE0_ARB); - } - } - } - } - for (pass = 0; pass < atifs->NumPasses; pass++) { - for (i = 0; i < atifs->numArithInstr[pass]; i++) { - struct atifs_instruction *inst = &atifs->Instructions[pass][i]; - - for (optype = 0; optype < 2; optype++) { /* color, alpha */ - if (inst->Opcode[optype]) { - for (arg = 0; arg < inst->ArgCount[optype]; arg++) { - GLint index = inst->SrcReg[optype][arg].Index; - if (index == GL_PRIMARY_COLOR_EXT) { - prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_COL0); - } else if (index == GL_SECONDARY_INTERPOLATOR_ATI) { - /* note: ATI_fragment_shader.txt never specifies what - * GL_SECONDARY_INTERPOLATOR_ATI is, swrast uses - * VARYING_SLOT_COL1 for this input */ - prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_COL1); - } - } - } - } - } - } - /* we may need fog */ - prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_FOGC); - - /* we always have the ATI_fs constants, and the fog params */ - for (i = 0; i < MAX_NUM_FRAGMENT_CONSTANTS_ATI; i++) { - _mesa_add_parameter(prog->Parameters, PROGRAM_UNIFORM, - NULL, 4, GL_FLOAT, NULL, NULL, true); - } - _mesa_add_state_reference(prog->Parameters, fog_params_state); - _mesa_add_state_reference(prog->Parameters, fog_color); -} - - -struct tgsi_atifs_transform { - struct tgsi_transform_context base; - struct tgsi_shader_info info; - const struct st_fp_variant_key *key; - bool first_instruction_emitted; - unsigned fog_factor_temp; -}; - -static inline struct tgsi_atifs_transform * -tgsi_atifs_transform(struct tgsi_transform_context *tctx) -{ - return (struct tgsi_atifs_transform *)tctx; -} - -/* copied from st_cb_drawpixels_shader.c */ -static void -set_src(struct tgsi_full_instruction *inst, unsigned i, unsigned file, unsigned index, - unsigned x, unsigned y, unsigned z, unsigned w) -{ - inst->Src[i].Register.File = file; - inst->Src[i].Register.Index = index; - inst->Src[i].Register.SwizzleX = x; - inst->Src[i].Register.SwizzleY = y; - inst->Src[i].Register.SwizzleZ = z; - inst->Src[i].Register.SwizzleW = w; - if (file == TGSI_FILE_CONSTANT) { - inst->Src[i].Register.Dimension = 1; - inst->Src[i].Dimension.Index = 0; - } -} - -#define SET_SRC(inst, i, file, index, x, y, z, w) \ - set_src(inst, i, file, index, TGSI_SWIZZLE_##x, TGSI_SWIZZLE_##y, \ - TGSI_SWIZZLE_##z, TGSI_SWIZZLE_##w) - -static void -transform_decl(struct tgsi_transform_context *tctx, - struct tgsi_full_declaration *decl) -{ - struct tgsi_atifs_transform *ctx = tgsi_atifs_transform(tctx); - - if (decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) { - /* fix texture target */ - unsigned newtarget = ctx->key->texture_targets[decl->Range.First]; - if (newtarget) - decl->SamplerView.Resource = newtarget; - } - - tctx->emit_declaration(tctx, decl); -} - -static void -transform_instr(struct tgsi_transform_context *tctx, - struct tgsi_full_instruction *current_inst) -{ - struct tgsi_atifs_transform *ctx = tgsi_atifs_transform(tctx); - - if (ctx->first_instruction_emitted) - goto transform_inst; - - ctx->first_instruction_emitted = true; - - if (ctx->key->fog) { - /* add a new temp for the fog factor */ - ctx->fog_factor_temp = ctx->info.file_max[TGSI_FILE_TEMPORARY] + 1; - tgsi_transform_temp_decl(tctx, ctx->fog_factor_temp); - } - -transform_inst: - if (current_inst->Instruction.Opcode == TGSI_OPCODE_TEX) { - /* fix texture target */ - unsigned newtarget = ctx->key->texture_targets[current_inst->Src[1].Register.Index]; - if (newtarget) - current_inst->Texture.Texture = newtarget; - - } else if (ctx->key->fog && current_inst->Instruction.Opcode == TGSI_OPCODE_MOV && - current_inst->Dst[0].Register.File == TGSI_FILE_OUTPUT) { - struct tgsi_full_instruction inst; - unsigned i; - int fogc_index = -1; - int reg0_index = current_inst->Src[0].Register.Index; - - /* find FOGC input */ - for (i = 0; i < ctx->info.num_inputs; i++) { - if (ctx->info.input_semantic_name[i] == TGSI_SEMANTIC_FOG) { - fogc_index = i; - break; - } - } - if (fogc_index < 0) { - /* should never be reached, because fog coord input is always declared */ - tctx->emit_instruction(tctx, current_inst); - return; - } - - /* compute the 1 component fog factor f */ - if (ctx->key->fog == FOG_LINEAR) { - /* LINEAR formula: f = (end - z) / (end - start) - * with optimized parameters: - * f = MAD(fogcoord, oparams.x, oparams.y) - */ - inst = tgsi_default_full_instruction(); - inst.Instruction.Opcode = TGSI_OPCODE_MAD; - inst.Instruction.NumDstRegs = 1; - inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY; - inst.Dst[0].Register.Index = ctx->fog_factor_temp; - inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW; - inst.Instruction.NumSrcRegs = 3; - SET_SRC(&inst, 0, TGSI_FILE_INPUT, fogc_index, X, Y, Z, W); - SET_SRC(&inst, 1, TGSI_FILE_CONSTANT, MAX_NUM_FRAGMENT_CONSTANTS_ATI, X, X, X, X); - SET_SRC(&inst, 2, TGSI_FILE_CONSTANT, MAX_NUM_FRAGMENT_CONSTANTS_ATI, Y, Y, Y, Y); - tctx->emit_instruction(tctx, &inst); - } else if (ctx->key->fog == FOG_EXP) { - /* EXP formula: f = exp(-dens * z) - * with optimized parameters: - * f = MUL(fogcoord, oparams.z); f= EX2(-f) - */ - inst = tgsi_default_full_instruction(); - inst.Instruction.Opcode = TGSI_OPCODE_MUL; - inst.Instruction.NumDstRegs = 1; - inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY; - inst.Dst[0].Register.Index = ctx->fog_factor_temp; - inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW; - inst.Instruction.NumSrcRegs = 2; - SET_SRC(&inst, 0, TGSI_FILE_INPUT, fogc_index, X, Y, Z, W); - SET_SRC(&inst, 1, TGSI_FILE_CONSTANT, MAX_NUM_FRAGMENT_CONSTANTS_ATI, Z, Z, Z, Z); - tctx->emit_instruction(tctx, &inst); - - inst = tgsi_default_full_instruction(); - inst.Instruction.Opcode = TGSI_OPCODE_EX2; - inst.Instruction.NumDstRegs = 1; - inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY; - inst.Dst[0].Register.Index = ctx->fog_factor_temp; - inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW; - inst.Instruction.NumSrcRegs = 1; - SET_SRC(&inst, 0, TGSI_FILE_TEMPORARY, ctx->fog_factor_temp, X, Y, Z, W); - inst.Src[0].Register.Negate = 1; - tctx->emit_instruction(tctx, &inst); - } else if (ctx->key->fog == FOG_EXP2) { - /* EXP2 formula: f = exp(-(dens * z)^2) - * with optimized parameters: - * f = MUL(fogcoord, oparams.w); f=MUL(f, f); f= EX2(-f) - */ - inst = tgsi_default_full_instruction(); - inst.Instruction.Opcode = TGSI_OPCODE_MUL; - inst.Instruction.NumDstRegs = 1; - inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY; - inst.Dst[0].Register.Index = ctx->fog_factor_temp; - inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW; - inst.Instruction.NumSrcRegs = 2; - SET_SRC(&inst, 0, TGSI_FILE_INPUT, fogc_index, X, Y, Z, W); - SET_SRC(&inst, 1, TGSI_FILE_CONSTANT, MAX_NUM_FRAGMENT_CONSTANTS_ATI, W, W, W, W); - tctx->emit_instruction(tctx, &inst); - - inst = tgsi_default_full_instruction(); - inst.Instruction.Opcode = TGSI_OPCODE_MUL; - inst.Instruction.NumDstRegs = 1; - inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY; - inst.Dst[0].Register.Index = ctx->fog_factor_temp; - inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW; - inst.Instruction.NumSrcRegs = 2; - SET_SRC(&inst, 0, TGSI_FILE_TEMPORARY, ctx->fog_factor_temp, X, Y, Z, W); - SET_SRC(&inst, 1, TGSI_FILE_TEMPORARY, ctx->fog_factor_temp, X, Y, Z, W); - tctx->emit_instruction(tctx, &inst); - - inst = tgsi_default_full_instruction(); - inst.Instruction.Opcode = TGSI_OPCODE_EX2; - inst.Instruction.NumDstRegs = 1; - inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY; - inst.Dst[0].Register.Index = ctx->fog_factor_temp; - inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW; - inst.Instruction.NumSrcRegs = 1; - SET_SRC(&inst, 0, TGSI_FILE_TEMPORARY, ctx->fog_factor_temp, X, Y, Z, W); - inst.Src[0].Register.Negate ^= 1; - tctx->emit_instruction(tctx, &inst); - } - /* f = saturate(f) */ - inst = tgsi_default_full_instruction(); - inst.Instruction.Opcode = TGSI_OPCODE_MOV; - inst.Instruction.NumDstRegs = 1; - inst.Instruction.Saturate = 1; - inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY; - inst.Dst[0].Register.Index = ctx->fog_factor_temp; - inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW; - inst.Instruction.NumSrcRegs = 1; - SET_SRC(&inst, 0, TGSI_FILE_TEMPORARY, ctx->fog_factor_temp, X, Y, Z, W); - tctx->emit_instruction(tctx, &inst); - - /* REG0 = LRP(f, REG0, fogcolor) */ - inst = tgsi_default_full_instruction(); - inst.Instruction.Opcode = TGSI_OPCODE_LRP; - inst.Instruction.NumDstRegs = 1; - inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY; - inst.Dst[0].Register.Index = reg0_index; - inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW; - inst.Instruction.NumSrcRegs = 3; - SET_SRC(&inst, 0, TGSI_FILE_TEMPORARY, ctx->fog_factor_temp, X, X, X, Y); - SET_SRC(&inst, 1, TGSI_FILE_TEMPORARY, reg0_index, X, Y, Z, W); - SET_SRC(&inst, 2, TGSI_FILE_CONSTANT, MAX_NUM_FRAGMENT_CONSTANTS_ATI + 1, X, Y, Z, W); - tctx->emit_instruction(tctx, &inst); - } - - tctx->emit_instruction(tctx, current_inst); -} - -/* - * A post-process step in the draw call to fix texture targets and - * insert code for fog. - */ -const struct tgsi_token * -st_fixup_atifs(const struct tgsi_token *tokens, - const struct st_fp_variant_key *key) -{ - struct tgsi_atifs_transform ctx; - struct tgsi_token *newtoks; - int newlen; - - memset(&ctx, 0, sizeof(ctx)); - ctx.base.transform_declaration = transform_decl; - ctx.base.transform_instruction = transform_instr; - ctx.key = key; - tgsi_scan_shader(tokens, &ctx.info); - - newlen = tgsi_num_tokens(tokens) + 30; - newtoks = tgsi_alloc_tokens(newlen); - if (!newtoks) - return NULL; - - tgsi_transform_shader(tokens, newtoks, newlen, &ctx.base); - return newtoks; -} - diff --git a/src/mesa/state_tracker/st_atom_shader.c b/src/mesa/state_tracker/st_atom_shader.c index 1049cc6e8ce..7f0a18ab53d 100644 --- a/src/mesa/state_tracker/st_atom_shader.c +++ b/src/mesa/state_tracker/st_atom_shader.c @@ -57,7 +57,7 @@ static unsigned -get_texture_target(struct gl_context *ctx, const unsigned unit) +get_texture_index(struct gl_context *ctx, const unsigned unit) { struct gl_texture_object *texObj = _mesa_get_tex_unit(ctx, unit)->_Current; gl_texture_index index; @@ -69,25 +69,7 @@ get_texture_target(struct gl_context *ctx, const unsigned unit) index = TEXTURE_2D_INDEX; } - /* Map mesa texture target to TGSI texture target. - * Copied from st_mesa_to_tgsi.c, the shadow part is omitted */ - switch(index) { - case TEXTURE_2D_MULTISAMPLE_INDEX: return TGSI_TEXTURE_2D_MSAA; - case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX: return TGSI_TEXTURE_2D_ARRAY_MSAA; - case TEXTURE_BUFFER_INDEX: return TGSI_TEXTURE_BUFFER; - case TEXTURE_1D_INDEX: return TGSI_TEXTURE_1D; - case TEXTURE_2D_INDEX: return TGSI_TEXTURE_2D; - case TEXTURE_3D_INDEX: return TGSI_TEXTURE_3D; - case TEXTURE_CUBE_INDEX: return TGSI_TEXTURE_CUBE; - case TEXTURE_CUBE_ARRAY_INDEX: return TGSI_TEXTURE_CUBE_ARRAY; - case TEXTURE_RECT_INDEX: return TGSI_TEXTURE_RECT; - case TEXTURE_1D_ARRAY_INDEX: return TGSI_TEXTURE_1D_ARRAY; - case TEXTURE_2D_ARRAY_INDEX: return TGSI_TEXTURE_2D_ARRAY; - case TEXTURE_EXTERNAL_INDEX: return TGSI_TEXTURE_2D; - default: - debug_assert(0); - return TGSI_TEXTURE_1D; - } + return index; } @@ -154,7 +136,7 @@ st_update_fp( struct st_context *st ) key.fog = st->ctx->Fog._PackedEnabledMode; for (unsigned u = 0; u < MAX_NUM_FRAGMENT_REGISTERS_ATI; u++) { - key.texture_targets[u] = get_texture_target(st->ctx, u); + key.texture_index[u] = get_texture_index(st->ctx, u); } } diff --git a/src/mesa/state_tracker/st_cb_program.c b/src/mesa/state_tracker/st_cb_program.c index 775ccb336aa..e87ea9b239e 100644 --- a/src/mesa/state_tracker/st_cb_program.c +++ b/src/mesa/state_tracker/st_cb_program.c @@ -45,7 +45,7 @@ #include "st_program.h" #include "st_cb_program.h" #include "st_glsl_to_ir.h" -#include "st_atifs_to_tgsi.h" +#include "st_atifs_to_nir.h" #include "st_util.h" diff --git a/src/mesa/state_tracker/st_program.c b/src/mesa/state_tracker/st_program.c index 5424fc204fd..7e360d3e676 100644 --- a/src/mesa/state_tracker/st_program.c +++ b/src/mesa/state_tracker/st_program.c @@ -63,7 +63,7 @@ #include "st_tgsi_lower_depth_clamp.h" #include "st_tgsi_lower_yuv.h" #include "st_program.h" -#include "st_atifs_to_tgsi.h" +#include "st_atifs_to_nir.h" #include "st_nir.h" #include "st_shader_cache.h" #include "st_util.h" @@ -371,20 +371,13 @@ st_finalize_nir_before_variants(struct nir_shader *nir) st_nir_assign_vs_in_locations(nir); } -/** - * Translate ARB (asm) program to NIR - */ -static nir_shader * -st_translate_prog_to_nir(struct st_context *st, struct gl_program *prog, - gl_shader_stage stage) +static void +st_prog_to_nir_postprocess(struct st_context *st, nir_shader *nir, + struct gl_program *prog) { struct pipe_screen *screen = st->screen; - const struct nir_shader_compiler_options *options = - st_get_nir_compiler_options(st, prog->info.stage); - /* Translate to NIR */ - nir_shader *nir = prog_to_nir(prog, options); - NIR_PASS_V(nir, nir_lower_regs_to_ssa); /* turn registers into SSA */ + NIR_PASS_V(nir, nir_lower_regs_to_ssa); nir_validate_shader(nir, "after st/ptn lower_regs_to_ssa"); NIR_PASS_V(nir, st_nir_lower_wpos_ytransform, prog, screen); @@ -400,6 +393,22 @@ st_translate_prog_to_nir(struct st_context *st, struct gl_program *prog, st_finalize_nir(st, prog, NULL, nir, true); nir_validate_shader(nir, "after st/glsl finalize_nir"); +} + +/** + * Translate ARB (asm) program to NIR + */ +static nir_shader * +st_translate_prog_to_nir(struct st_context *st, struct gl_program *prog, + gl_shader_stage stage) +{ + const struct nir_shader_compiler_options *options = + st_get_nir_compiler_options(st, prog->info.stage); + + /* Translate to NIR */ + nir_shader *nir = prog_to_nir(prog, options); + + st_prog_to_nir_postprocess(st, nir, prog); return nir; } @@ -863,7 +872,7 @@ st_translate_fragment_program(struct st_context *st, ST_NEW_FS_SAMPLERS; } - /* Translate to NIR. */ + /* Translate to NIR. ATI_fs translates at variant time. */ if (!stfp->ati_fs) { nir_shader *nir = st_translate_prog_to_nir(st, &stfp->Base, MESA_SHADER_FRAGMENT); @@ -876,8 +885,9 @@ st_translate_fragment_program(struct st_context *st, } stfp->state.type = PIPE_SHADER_IR_NIR; stfp->Base.nir = nir; - return true; } + + return true; } ubyte outputMapping[2 * FRAG_RESULT_MAX]; @@ -1143,22 +1153,6 @@ st_translate_fragment_program(struct st_context *st, fs_output_semantic_index); free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi); - } else { - assert(stfp->ati_fs); - st_translate_atifs_program(ureg, - stfp->ati_fs, - &stfp->Base, - /* inputs */ - fs_num_inputs, - inputMapping, - input_semantic_name, - input_semantic_index, - interpMode, - /* outputs */ - fs_num_outputs, - outputMapping, - fs_output_semantic_name, - fs_output_semantic_index); } stfp->state.tokens = ureg_get_tokens(ureg, NULL); @@ -1193,11 +1187,26 @@ st_create_fp_variant(struct st_context *st, if (!variant) return NULL; - if (stfp->state.type == PIPE_SHADER_IR_NIR) { - bool finalize = false; + /* Translate ATI_fs to NIR at variant time because that's when we have the + * texture types. + */ + if (stfp->ati_fs) { + const struct nir_shader_compiler_options *options = + st_get_nir_compiler_options(st, MESA_SHADER_FRAGMENT); + + nir_shader *s = st_translate_atifs_program(stfp->ati_fs, key, &stfp->Base, options); + + st_prog_to_nir_postprocess(st, s, &stfp->Base); + state.type = PIPE_SHADER_IR_NIR; + state.ir.nir = s; + } else if (stfp->state.type == PIPE_SHADER_IR_NIR) { state.type = PIPE_SHADER_IR_NIR; state.ir.nir = get_nir_shader(st, stfp); + } + + if (state.type == PIPE_SHADER_IR_NIR) { + bool finalize = false; if (key->clamp_color) { NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs); @@ -1349,16 +1358,6 @@ st_create_fp_variant(struct st_context *st, assert(!(key->bitmap && key->drawpixels)); - /* Fix texture targets and add fog for ATI_fs */ - if (stfp->ati_fs) { - const struct tgsi_token *tokens = st_fixup_atifs(state.tokens, key); - - if (tokens) - state.tokens = tokens; - else - fprintf(stderr, "mesa: cannot post-process ATI_fs\n"); - } - /* Emulate features. */ if (key->clamp_color || key->persample_shading) { const struct tgsi_token *tokens; @@ -1929,6 +1928,10 @@ st_precompile_shader_variant(struct st_context *st, key.st = st->has_shareable_shaders ? NULL : st; key.lower_alpha_func = COMPARE_FUNC_ALWAYS; + if (p->ati_fs) { + for (int i = 0; i < ARRAY_SIZE(key.texture_index); i++) + key.texture_index[i] = TEXTURE_2D_INDEX; + } st_get_fp_variant(st, p, &key); break; } diff --git a/src/mesa/state_tracker/st_program.h b/src/mesa/state_tracker/st_program.h index 5a4099c5f7a..e1bdd1fbb79 100644 --- a/src/mesa/state_tracker/st_program.h +++ b/src/mesa/state_tracker/st_program.h @@ -147,7 +147,7 @@ struct st_fp_variant_key unsigned lower_alpha_func:3; /** needed for ATI_fragment_shader */ - char texture_targets[MAX_NUM_FRAGMENT_REGISTERS_ATI]; + uint8_t texture_index[MAX_NUM_FRAGMENT_REGISTERS_ATI]; struct st_external_sampler_key external; };