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