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