Home | History | Annotate | Download | only in r300
      1 /*
      2  * Copyright 2008 Corbin Simpson <MostAwesomeDude (at) gmail.com>
      3  *                Joakim Sindholt <opensource (at) zhasha.com>
      4  * Copyright 2009 Marek Olk <maraeo (at) gmail.com>
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * on the rights to use, copy, modify, merge, publish, distribute, sub
     10  * license, and/or sell copies of the Software, and to permit persons to whom
     11  * the Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     20  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
     21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     23  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
     24 
     25 #include "util/u_format.h"
     26 #include "util/u_math.h"
     27 #include "util/u_memory.h"
     28 
     29 #include "tgsi/tgsi_dump.h"
     30 #include "tgsi/tgsi_ureg.h"
     31 
     32 #include "r300_cb.h"
     33 #include "r300_context.h"
     34 #include "r300_emit.h"
     35 #include "r300_screen.h"
     36 #include "r300_fs.h"
     37 #include "r300_reg.h"
     38 #include "r300_texture.h"
     39 #include "r300_tgsi_to_rc.h"
     40 
     41 #include "compiler/radeon_compiler.h"
     42 
     43 /* Convert info about FS input semantics to r300_shader_semantics. */
     44 void r300_shader_read_fs_inputs(struct tgsi_shader_info* info,
     45                                 struct r300_shader_semantics* fs_inputs)
     46 {
     47     int i;
     48     unsigned index;
     49 
     50     r300_shader_semantics_reset(fs_inputs);
     51 
     52     for (i = 0; i < info->num_inputs; i++) {
     53         index = info->input_semantic_index[i];
     54 
     55         switch (info->input_semantic_name[i]) {
     56             case TGSI_SEMANTIC_COLOR:
     57                 assert(index < ATTR_COLOR_COUNT);
     58                 fs_inputs->color[index] = i;
     59                 break;
     60 
     61             case TGSI_SEMANTIC_GENERIC:
     62                 assert(index < ATTR_GENERIC_COUNT);
     63                 fs_inputs->generic[index] = i;
     64                 break;
     65 
     66             case TGSI_SEMANTIC_FOG:
     67                 assert(index == 0);
     68                 fs_inputs->fog = i;
     69                 break;
     70 
     71             case TGSI_SEMANTIC_POSITION:
     72                 assert(index == 0);
     73                 fs_inputs->wpos = i;
     74                 break;
     75 
     76             case TGSI_SEMANTIC_FACE:
     77                 assert(index == 0);
     78                 fs_inputs->face = i;
     79                 break;
     80 
     81             default:
     82                 fprintf(stderr, "r300: FP: Unknown input semantic: %i\n",
     83                         info->input_semantic_name[i]);
     84         }
     85     }
     86 }
     87 
     88 static void find_output_registers(struct r300_fragment_program_compiler * compiler,
     89                                   struct r300_fragment_shader_code *shader)
     90 {
     91     unsigned i, colorbuf_count = 0;
     92 
     93     /* Mark the outputs as not present initially */
     94     compiler->OutputColor[0] = shader->info.num_outputs;
     95     compiler->OutputColor[1] = shader->info.num_outputs;
     96     compiler->OutputColor[2] = shader->info.num_outputs;
     97     compiler->OutputColor[3] = shader->info.num_outputs;
     98     compiler->OutputDepth = shader->info.num_outputs;
     99 
    100     /* Now see where they really are. */
    101     for(i = 0; i < shader->info.num_outputs; ++i) {
    102         switch(shader->info.output_semantic_name[i]) {
    103             case TGSI_SEMANTIC_COLOR:
    104                 compiler->OutputColor[colorbuf_count] = i;
    105                 colorbuf_count++;
    106                 break;
    107             case TGSI_SEMANTIC_POSITION:
    108                 compiler->OutputDepth = i;
    109                 break;
    110         }
    111     }
    112 }
    113 
    114 static void allocate_hardware_inputs(
    115     struct r300_fragment_program_compiler * c,
    116     void (*allocate)(void * data, unsigned input, unsigned hwreg),
    117     void * mydata)
    118 {
    119     struct r300_shader_semantics* inputs =
    120         (struct r300_shader_semantics*)c->UserData;
    121     int i, reg = 0;
    122 
    123     /* Allocate input registers. */
    124     for (i = 0; i < ATTR_COLOR_COUNT; i++) {
    125         if (inputs->color[i] != ATTR_UNUSED) {
    126             allocate(mydata, inputs->color[i], reg++);
    127         }
    128     }
    129     if (inputs->face != ATTR_UNUSED) {
    130         allocate(mydata, inputs->face, reg++);
    131     }
    132     for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
    133         if (inputs->generic[i] != ATTR_UNUSED) {
    134             allocate(mydata, inputs->generic[i], reg++);
    135         }
    136     }
    137     if (inputs->fog != ATTR_UNUSED) {
    138         allocate(mydata, inputs->fog, reg++);
    139     }
    140     if (inputs->wpos != ATTR_UNUSED) {
    141         allocate(mydata, inputs->wpos, reg++);
    142     }
    143 }
    144 
    145 static void get_external_state(
    146     struct r300_context* r300,
    147     struct r300_fragment_program_external_state* state)
    148 {
    149     struct r300_textures_state *texstate = r300->textures_state.state;
    150     unsigned i;
    151 
    152     for (i = 0; i < texstate->sampler_state_count; i++) {
    153         struct r300_sampler_state *s = texstate->sampler_states[i];
    154         struct r300_sampler_view *v = texstate->sampler_views[i];
    155         struct r300_resource *t;
    156 
    157         if (!s || !v) {
    158             continue;
    159         }
    160 
    161         t = r300_resource(v->base.texture);
    162 
    163         if (s->state.compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
    164             state->unit[i].compare_mode_enabled = 1;
    165 
    166             /* Fortunately, no need to translate this. */
    167             state->unit[i].texture_compare_func = s->state.compare_func;
    168         }
    169 
    170         state->unit[i].non_normalized_coords = !s->state.normalized_coords;
    171         state->unit[i].convert_unorm_to_snorm =
    172                 v->base.format == PIPE_FORMAT_RGTC1_SNORM ||
    173                 v->base.format == PIPE_FORMAT_LATC1_SNORM;
    174 
    175         /* Pass texture swizzling to the compiler, some lowering passes need it. */
    176         if (v->base.format == PIPE_FORMAT_RGTC1_SNORM ||
    177             v->base.format == PIPE_FORMAT_LATC1_SNORM) {
    178             unsigned char swizzle[4];
    179 
    180             util_format_compose_swizzles(
    181                             util_format_description(v->base.format)->swizzle,
    182                             v->swizzle,
    183                             swizzle);
    184 
    185             state->unit[i].texture_swizzle =
    186                     RC_MAKE_SWIZZLE(swizzle[0], swizzle[1],
    187                                     swizzle[2], swizzle[3]);
    188         } else if (state->unit[i].compare_mode_enabled) {
    189             state->unit[i].texture_swizzle =
    190                 RC_MAKE_SWIZZLE(v->swizzle[0], v->swizzle[1],
    191                                 v->swizzle[2], v->swizzle[3]);
    192         }
    193 
    194         /* XXX this should probably take into account STR, not just S. */
    195         if (t->tex.is_npot) {
    196             switch (s->state.wrap_s) {
    197             case PIPE_TEX_WRAP_REPEAT:
    198                 state->unit[i].wrap_mode = RC_WRAP_REPEAT;
    199                 break;
    200 
    201             case PIPE_TEX_WRAP_MIRROR_REPEAT:
    202                 state->unit[i].wrap_mode = RC_WRAP_MIRRORED_REPEAT;
    203                 break;
    204 
    205             case PIPE_TEX_WRAP_MIRROR_CLAMP:
    206             case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
    207             case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
    208                 state->unit[i].wrap_mode = RC_WRAP_MIRRORED_CLAMP;
    209                 break;
    210 
    211             default:
    212                 state->unit[i].wrap_mode = RC_WRAP_NONE;
    213             }
    214 
    215             if (t->b.b.target == PIPE_TEXTURE_3D)
    216                 state->unit[i].clamp_and_scale_before_fetch = TRUE;
    217         }
    218     }
    219 }
    220 
    221 static void r300_translate_fragment_shader(
    222     struct r300_context* r300,
    223     struct r300_fragment_shader_code* shader,
    224     const struct tgsi_token *tokens);
    225 
    226 static void r300_dummy_fragment_shader(
    227     struct r300_context* r300,
    228     struct r300_fragment_shader_code* shader)
    229 {
    230     struct pipe_shader_state state;
    231     struct ureg_program *ureg;
    232     struct ureg_dst out;
    233     struct ureg_src imm;
    234 
    235     /* Make a simple fragment shader which outputs (0, 0, 0, 1) */
    236     ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT);
    237     out = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
    238     imm = ureg_imm4f(ureg, 0, 0, 0, 1);
    239 
    240     ureg_MOV(ureg, out, imm);
    241     ureg_END(ureg);
    242 
    243     state.tokens = ureg_finalize(ureg);
    244 
    245     shader->dummy = TRUE;
    246     r300_translate_fragment_shader(r300, shader, state.tokens);
    247 
    248     ureg_destroy(ureg);
    249 }
    250 
    251 static void r300_emit_fs_code_to_buffer(
    252     struct r300_context *r300,
    253     struct r300_fragment_shader_code *shader)
    254 {
    255     struct rX00_fragment_program_code *generic_code = &shader->code;
    256     unsigned imm_count = shader->immediates_count;
    257     unsigned imm_first = shader->externals_count;
    258     unsigned imm_end = generic_code->constants.Count;
    259     struct rc_constant *constants = generic_code->constants.Constants;
    260     unsigned i;
    261     CB_LOCALS;
    262 
    263     if (r300->screen->caps.is_r500) {
    264         struct r500_fragment_program_code *code = &generic_code->code.r500;
    265 
    266         shader->cb_code_size = 19 +
    267                                ((code->inst_end + 1) * 6) +
    268                                imm_count * 7 +
    269                                code->int_constant_count * 2;
    270 
    271         NEW_CB(shader->cb_code, shader->cb_code_size);
    272         OUT_CB_REG(R500_US_CONFIG, R500_ZERO_TIMES_ANYTHING_EQUALS_ZERO);
    273         OUT_CB_REG(R500_US_PIXSIZE, code->max_temp_idx);
    274         OUT_CB_REG(R500_US_FC_CTRL, code->us_fc_ctrl);
    275         for(i = 0; i < code->int_constant_count; i++){
    276                 OUT_CB_REG(R500_US_FC_INT_CONST_0 + (i * 4),
    277                                                 code->int_constants[i]);
    278         }
    279         OUT_CB_REG(R500_US_CODE_RANGE,
    280                    R500_US_CODE_RANGE_ADDR(0) | R500_US_CODE_RANGE_SIZE(code->inst_end));
    281         OUT_CB_REG(R500_US_CODE_OFFSET, 0);
    282         OUT_CB_REG(R500_US_CODE_ADDR,
    283                    R500_US_CODE_START_ADDR(0) | R500_US_CODE_END_ADDR(code->inst_end));
    284 
    285         OUT_CB_REG(R500_GA_US_VECTOR_INDEX, R500_GA_US_VECTOR_INDEX_TYPE_INSTR);
    286         OUT_CB_ONE_REG(R500_GA_US_VECTOR_DATA, (code->inst_end + 1) * 6);
    287         for (i = 0; i <= code->inst_end; i++) {
    288             OUT_CB(code->inst[i].inst0);
    289             OUT_CB(code->inst[i].inst1);
    290             OUT_CB(code->inst[i].inst2);
    291             OUT_CB(code->inst[i].inst3);
    292             OUT_CB(code->inst[i].inst4);
    293             OUT_CB(code->inst[i].inst5);
    294         }
    295 
    296         /* Emit immediates. */
    297         if (imm_count) {
    298             for(i = imm_first; i < imm_end; ++i) {
    299                 if (constants[i].Type == RC_CONSTANT_IMMEDIATE) {
    300                     const float *data = constants[i].u.Immediate;
    301 
    302                     OUT_CB_REG(R500_GA_US_VECTOR_INDEX,
    303                                R500_GA_US_VECTOR_INDEX_TYPE_CONST |
    304                                (i & R500_GA_US_VECTOR_INDEX_MASK));
    305                     OUT_CB_ONE_REG(R500_GA_US_VECTOR_DATA, 4);
    306                     OUT_CB_TABLE(data, 4);
    307                 }
    308             }
    309         }
    310     } else { /* r300 */
    311         struct r300_fragment_program_code *code = &generic_code->code.r300;
    312         unsigned int alu_length = code->alu.length;
    313         unsigned int alu_iterations = ((alu_length - 1) / 64) + 1;
    314         unsigned int tex_length = code->tex.length;
    315         unsigned int tex_iterations =
    316             tex_length > 0 ? ((tex_length - 1) / 32) + 1 : 0;
    317         unsigned int iterations =
    318             alu_iterations > tex_iterations ? alu_iterations : tex_iterations;
    319         unsigned int bank = 0;
    320 
    321         shader->cb_code_size = 15 +
    322             /* R400_US_CODE_BANK */
    323             (r300->screen->caps.is_r400 ? 2 * (iterations + 1): 0) +
    324             /* R400_US_CODE_EXT */
    325             (r300->screen->caps.is_r400 ? 2 : 0) +
    326             /* R300_US_ALU_{RGB,ALPHA}_{INST,ADDR}_0, R400_US_ALU_EXT_ADDR_0 */
    327             (code->r390_mode ? (5 * alu_iterations) : 4) +
    328             /* R400_US_ALU_EXT_ADDR_[0-63] */
    329             (code->r390_mode ? (code->alu.length) : 0) +
    330             /* R300_US_ALU_{RGB,ALPHA}_{INST,ADDR}_0 */
    331             code->alu.length * 4 +
    332             /* R300_US_TEX_INST_0, R300_US_TEX_INST_[0-31] */
    333             (code->tex.length > 0 ? code->tex.length + tex_iterations : 0) +
    334             imm_count * 5;
    335 
    336         NEW_CB(shader->cb_code, shader->cb_code_size);
    337 
    338         OUT_CB_REG(R300_US_CONFIG, code->config);
    339         OUT_CB_REG(R300_US_PIXSIZE, code->pixsize);
    340         OUT_CB_REG(R300_US_CODE_OFFSET, code->code_offset);
    341 
    342         if (code->r390_mode) {
    343             OUT_CB_REG(R400_US_CODE_EXT, code->r400_code_offset_ext);
    344         } else if (r300->screen->caps.is_r400) {
    345             /* This register appears to affect shaders even if r390_mode is
    346              * disabled, so it needs to be set to 0 for shaders that
    347              * don't use r390_mode. */
    348             OUT_CB_REG(R400_US_CODE_EXT, 0);
    349         }
    350 
    351         OUT_CB_REG_SEQ(R300_US_CODE_ADDR_0, 4);
    352         OUT_CB_TABLE(code->code_addr, 4);
    353 
    354         do {
    355             unsigned int bank_alu_length = (alu_length < 64 ? alu_length : 64);
    356             unsigned int bank_alu_offset = bank * 64;
    357             unsigned int bank_tex_length = (tex_length < 32 ? tex_length : 32);
    358             unsigned int bank_tex_offset = bank * 32;
    359 
    360             if (r300->screen->caps.is_r400) {
    361                 OUT_CB_REG(R400_US_CODE_BANK, code->r390_mode ?
    362                                 (bank << R400_BANK_SHIFT) | R400_R390_MODE_ENABLE : 0);//2
    363             }
    364 
    365             if (bank_alu_length > 0) {
    366                 OUT_CB_REG_SEQ(R300_US_ALU_RGB_INST_0, bank_alu_length);
    367                 for (i = 0; i < bank_alu_length; i++)
    368                     OUT_CB(code->alu.inst[i + bank_alu_offset].rgb_inst);
    369 
    370                 OUT_CB_REG_SEQ(R300_US_ALU_RGB_ADDR_0, bank_alu_length);
    371                 for (i = 0; i < bank_alu_length; i++)
    372                     OUT_CB(code->alu.inst[i + bank_alu_offset].rgb_addr);
    373 
    374                 OUT_CB_REG_SEQ(R300_US_ALU_ALPHA_INST_0, bank_alu_length);
    375                 for (i = 0; i < bank_alu_length; i++)
    376                     OUT_CB(code->alu.inst[i + bank_alu_offset].alpha_inst);
    377 
    378                 OUT_CB_REG_SEQ(R300_US_ALU_ALPHA_ADDR_0, bank_alu_length);
    379                 for (i = 0; i < bank_alu_length; i++)
    380                     OUT_CB(code->alu.inst[i + bank_alu_offset].alpha_addr);
    381 
    382                 if (code->r390_mode) {
    383                     OUT_CB_REG_SEQ(R400_US_ALU_EXT_ADDR_0, bank_alu_length);
    384                     for (i = 0; i < bank_alu_length; i++)
    385                         OUT_CB(code->alu.inst[i + bank_alu_offset].r400_ext_addr);
    386                 }
    387             }
    388 
    389             if (bank_tex_length > 0) {
    390                 OUT_CB_REG_SEQ(R300_US_TEX_INST_0, bank_tex_length);
    391                 OUT_CB_TABLE(code->tex.inst + bank_tex_offset, bank_tex_length);
    392             }
    393 
    394             alu_length -= bank_alu_length;
    395             tex_length -= bank_tex_length;
    396             bank++;
    397         } while(code->r390_mode && (alu_length > 0 || tex_length > 0));
    398 
    399         /* R400_US_CODE_BANK needs to be reset to 0, otherwise some shaders
    400          * will be rendered incorrectly. */
    401         if (r300->screen->caps.is_r400) {
    402             OUT_CB_REG(R400_US_CODE_BANK,
    403                 code->r390_mode ? R400_R390_MODE_ENABLE : 0);
    404         }
    405 
    406         /* Emit immediates. */
    407         if (imm_count) {
    408             for(i = imm_first; i < imm_end; ++i) {
    409                 if (constants[i].Type == RC_CONSTANT_IMMEDIATE) {
    410                     const float *data = constants[i].u.Immediate;
    411 
    412                     OUT_CB_REG_SEQ(R300_PFS_PARAM_0_X + i * 16, 4);
    413                     OUT_CB(pack_float24(data[0]));
    414                     OUT_CB(pack_float24(data[1]));
    415                     OUT_CB(pack_float24(data[2]));
    416                     OUT_CB(pack_float24(data[3]));
    417                 }
    418             }
    419         }
    420     }
    421 
    422     OUT_CB_REG(R300_FG_DEPTH_SRC, shader->fg_depth_src);
    423     OUT_CB_REG(R300_US_W_FMT, shader->us_out_w);
    424     END_CB;
    425 }
    426 
    427 static void r300_translate_fragment_shader(
    428     struct r300_context* r300,
    429     struct r300_fragment_shader_code* shader,
    430     const struct tgsi_token *tokens)
    431 {
    432     struct r300_fragment_program_compiler compiler;
    433     struct tgsi_to_rc ttr;
    434     int wpos, face;
    435     unsigned i;
    436 
    437     tgsi_scan_shader(tokens, &shader->info);
    438     r300_shader_read_fs_inputs(&shader->info, &shader->inputs);
    439 
    440     wpos = shader->inputs.wpos;
    441     face = shader->inputs.face;
    442 
    443     /* Setup the compiler. */
    444     memset(&compiler, 0, sizeof(compiler));
    445     rc_init(&compiler.Base);
    446     DBG_ON(r300, DBG_FP) ? compiler.Base.Debug |= RC_DBG_LOG : 0;
    447     DBG_ON(r300, DBG_P_STAT) ? compiler.Base.Debug |= RC_DBG_STATS : 0;
    448 
    449     compiler.code = &shader->code;
    450     compiler.state = shader->compare_state;
    451     compiler.Base.is_r500 = r300->screen->caps.is_r500;
    452     compiler.Base.is_r400 = r300->screen->caps.is_r400;
    453     compiler.Base.disable_optimizations = DBG_ON(r300, DBG_NO_OPT);
    454     compiler.Base.has_half_swizzles = TRUE;
    455     compiler.Base.has_presub = TRUE;
    456     compiler.Base.has_omod = TRUE;
    457     compiler.Base.max_temp_regs =
    458         compiler.Base.is_r500 ? 128 : (compiler.Base.is_r400 ? 64 : 32);
    459     compiler.Base.max_constants = compiler.Base.is_r500 ? 256 : 32;
    460     compiler.Base.max_alu_insts =
    461         (compiler.Base.is_r500 || compiler.Base.is_r400) ? 512 : 64;
    462     compiler.Base.max_tex_insts =
    463         (compiler.Base.is_r500 || compiler.Base.is_r400) ? 512 : 32;
    464     compiler.AllocateHwInputs = &allocate_hardware_inputs;
    465     compiler.UserData = &shader->inputs;
    466 
    467     find_output_registers(&compiler, shader);
    468 
    469     shader->write_all = FALSE;
    470     for (i = 0; i < shader->info.num_properties; i++) {
    471         if (shader->info.properties[i].name == TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS) {
    472             shader->write_all = TRUE;
    473         }
    474     }
    475 
    476     if (compiler.Base.Debug & RC_DBG_LOG) {
    477         DBG(r300, DBG_FP, "r300: Initial fragment program\n");
    478         tgsi_dump(tokens, 0);
    479     }
    480 
    481     /* Translate TGSI to our internal representation */
    482     ttr.compiler = &compiler.Base;
    483     ttr.info = &shader->info;
    484     ttr.use_half_swizzles = TRUE;
    485 
    486     r300_tgsi_to_rc(&ttr, tokens);
    487 
    488     if (ttr.error) {
    489         fprintf(stderr, "r300 FP: Cannot translate a shader. "
    490                 "Using a dummy shader instead.\n");
    491         r300_dummy_fragment_shader(r300, shader);
    492         return;
    493     }
    494 
    495     if (!r300->screen->caps.is_r500 ||
    496         compiler.Base.Program.Constants.Count > 200) {
    497         compiler.Base.remove_unused_constants = TRUE;
    498     }
    499 
    500     /**
    501      * Transform the program to support WPOS.
    502      *
    503      * Introduce a small fragment at the start of the program that will be
    504      * the only code that directly reads the WPOS input.
    505      * All other code pieces that reference that input will be rewritten
    506      * to read from a newly allocated temporary. */
    507     if (wpos != ATTR_UNUSED) {
    508         /* Moving the input to some other reg is not really necessary. */
    509         rc_transform_fragment_wpos(&compiler.Base, wpos, wpos, TRUE);
    510     }
    511 
    512     if (face != ATTR_UNUSED) {
    513         rc_transform_fragment_face(&compiler.Base, face);
    514     }
    515 
    516     /* Invoke the compiler */
    517     r3xx_compile_fragment_program(&compiler);
    518 
    519     if (compiler.Base.Error) {
    520         fprintf(stderr, "r300 FP: Compiler Error:\n%sUsing a dummy shader"
    521                 " instead.\n", compiler.Base.ErrorMsg);
    522 
    523         if (shader->dummy) {
    524             fprintf(stderr, "r300 FP: Cannot compile the dummy shader! "
    525                     "Giving up...\n");
    526             abort();
    527         }
    528 
    529         rc_destroy(&compiler.Base);
    530         r300_dummy_fragment_shader(r300, shader);
    531         return;
    532     }
    533 
    534     /* Shaders with zero instructions are invalid,
    535      * use the dummy shader instead. */
    536     if (shader->code.code.r500.inst_end == -1) {
    537         rc_destroy(&compiler.Base);
    538         r300_dummy_fragment_shader(r300, shader);
    539         return;
    540     }
    541 
    542     /* Initialize numbers of constants for each type. */
    543     shader->externals_count = 0;
    544     for (i = 0;
    545          i < shader->code.constants.Count &&
    546          shader->code.constants.Constants[i].Type == RC_CONSTANT_EXTERNAL; i++) {
    547         shader->externals_count = i+1;
    548     }
    549     shader->immediates_count = 0;
    550     shader->rc_state_count = 0;
    551 
    552     for (i = shader->externals_count; i < shader->code.constants.Count; i++) {
    553         switch (shader->code.constants.Constants[i].Type) {
    554             case RC_CONSTANT_IMMEDIATE:
    555                 ++shader->immediates_count;
    556                 break;
    557             case RC_CONSTANT_STATE:
    558                 ++shader->rc_state_count;
    559                 break;
    560             default:
    561                 assert(0);
    562         }
    563     }
    564 
    565     /* Setup shader depth output. */
    566     if (shader->code.writes_depth) {
    567         shader->fg_depth_src = R300_FG_DEPTH_SRC_SHADER;
    568         shader->us_out_w = R300_W_FMT_W24 | R300_W_SRC_US;
    569     } else {
    570         shader->fg_depth_src = R300_FG_DEPTH_SRC_SCAN;
    571         shader->us_out_w = R300_W_FMT_W0 | R300_W_SRC_US;
    572     }
    573 
    574     /* And, finally... */
    575     rc_destroy(&compiler.Base);
    576 
    577     /* Build the command buffer. */
    578     r300_emit_fs_code_to_buffer(r300, shader);
    579 }
    580 
    581 boolean r300_pick_fragment_shader(struct r300_context* r300)
    582 {
    583     struct r300_fragment_shader* fs = r300_fs(r300);
    584     struct r300_fragment_program_external_state state = {{{ 0 }}};
    585     struct r300_fragment_shader_code* ptr;
    586 
    587     get_external_state(r300, &state);
    588 
    589     if (!fs->first) {
    590         /* Build the fragment shader for the first time. */
    591         fs->first = fs->shader = CALLOC_STRUCT(r300_fragment_shader_code);
    592 
    593         memcpy(&fs->shader->compare_state, &state,
    594             sizeof(struct r300_fragment_program_external_state));
    595         r300_translate_fragment_shader(r300, fs->shader, fs->state.tokens);
    596         return TRUE;
    597 
    598     } else {
    599         /* Check if the currently-bound shader has been compiled
    600          * with the texture-compare state we need. */
    601         if (memcmp(&fs->shader->compare_state, &state, sizeof(state)) != 0) {
    602             /* Search for the right shader. */
    603             ptr = fs->first;
    604             while (ptr) {
    605                 if (memcmp(&ptr->compare_state, &state, sizeof(state)) == 0) {
    606                     if (fs->shader != ptr) {
    607                         fs->shader = ptr;
    608                         return TRUE;
    609                     }
    610                     /* The currently-bound one is OK. */
    611                     return FALSE;
    612                 }
    613                 ptr = ptr->next;
    614             }
    615 
    616             /* Not found, gotta compile a new one. */
    617             ptr = CALLOC_STRUCT(r300_fragment_shader_code);
    618             ptr->next = fs->first;
    619             fs->first = fs->shader = ptr;
    620 
    621             ptr->compare_state = state;
    622             r300_translate_fragment_shader(r300, ptr, fs->state.tokens);
    623             return TRUE;
    624         }
    625     }
    626 
    627     return FALSE;
    628 }
    629