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