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