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