Merge remote branch 'origin/master' into pipe-video
[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_math.h"
26 #include "util/u_memory.h"
27
28 #include "tgsi/tgsi_dump.h"
29 #include "tgsi/tgsi_ureg.h"
30
31 #include "r300_cb.h"
32 #include "r300_context.h"
33 #include "r300_emit.h"
34 #include "r300_screen.h"
35 #include "r300_fs.h"
36 #include "r300_reg.h"
37 #include "r300_tgsi_to_rc.h"
38
39 #include "radeon_code.h"
40 #include "radeon_compiler.h"
41
42 /* Convert info about FS input semantics to r300_shader_semantics. */
43 void r300_shader_read_fs_inputs(struct tgsi_shader_info* info,
44 struct r300_shader_semantics* fs_inputs)
45 {
46 int i;
47 unsigned index;
48
49 r300_shader_semantics_reset(fs_inputs);
50
51 for (i = 0; i < info->num_inputs; i++) {
52 index = info->input_semantic_index[i];
53
54 switch (info->input_semantic_name[i]) {
55 case TGSI_SEMANTIC_COLOR:
56 assert(index < ATTR_COLOR_COUNT);
57 fs_inputs->color[index] = i;
58 break;
59
60 case TGSI_SEMANTIC_GENERIC:
61 assert(index < ATTR_GENERIC_COUNT);
62 fs_inputs->generic[index] = i;
63 break;
64
65 case TGSI_SEMANTIC_FOG:
66 assert(index == 0);
67 fs_inputs->fog = i;
68 break;
69
70 case TGSI_SEMANTIC_POSITION:
71 assert(index == 0);
72 fs_inputs->wpos = i;
73 break;
74
75 case TGSI_SEMANTIC_FACE:
76 assert(index == 0);
77 fs_inputs->face = i;
78 break;
79
80 default:
81 fprintf(stderr, "r300: FP: Unknown input semantic: %i\n",
82 info->input_semantic_name[i]);
83 }
84 }
85 }
86
87 static void find_output_registers(struct r300_fragment_program_compiler * compiler,
88 struct r300_fragment_shader_code *shader)
89 {
90 unsigned i, colorbuf_count = 0;
91
92 /* Mark the outputs as not present initially */
93 compiler->OutputColor[0] = shader->info.num_outputs;
94 compiler->OutputColor[1] = shader->info.num_outputs;
95 compiler->OutputColor[2] = shader->info.num_outputs;
96 compiler->OutputColor[3] = shader->info.num_outputs;
97 compiler->OutputDepth = shader->info.num_outputs;
98
99 /* Now see where they really are. */
100 for(i = 0; i < shader->info.num_outputs; ++i) {
101 switch(shader->info.output_semantic_name[i]) {
102 case TGSI_SEMANTIC_COLOR:
103 compiler->OutputColor[colorbuf_count] = i;
104 colorbuf_count++;
105 break;
106 case TGSI_SEMANTIC_POSITION:
107 compiler->OutputDepth = i;
108 break;
109 }
110 }
111 }
112
113 static void allocate_hardware_inputs(
114 struct r300_fragment_program_compiler * c,
115 void (*allocate)(void * data, unsigned input, unsigned hwreg),
116 void * mydata)
117 {
118 struct r300_shader_semantics* inputs =
119 (struct r300_shader_semantics*)c->UserData;
120 int i, reg = 0;
121
122 /* Allocate input registers. */
123 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
124 if (inputs->color[i] != ATTR_UNUSED) {
125 allocate(mydata, inputs->color[i], reg++);
126 }
127 }
128 if (inputs->face != ATTR_UNUSED) {
129 allocate(mydata, inputs->face, reg++);
130 }
131 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
132 if (inputs->generic[i] != ATTR_UNUSED) {
133 allocate(mydata, inputs->generic[i], reg++);
134 }
135 }
136 if (inputs->fog != ATTR_UNUSED) {
137 allocate(mydata, inputs->fog, reg++);
138 }
139 if (inputs->wpos != ATTR_UNUSED) {
140 allocate(mydata, inputs->wpos, reg++);
141 }
142 }
143
144 static void get_external_state(
145 struct r300_context* r300,
146 struct r300_fragment_program_external_state* state)
147 {
148 struct r300_textures_state *texstate = r300->textures_state.state;
149 unsigned i;
150 unsigned char *swizzle;
151
152 for (i = 0; i < texstate->sampler_state_count; i++) {
153 struct r300_sampler_state *s = texstate->sampler_states[i];
154 struct r300_sampler_view *v = texstate->sampler_views[i];
155 struct r300_texture *t;
156
157 if (!s || !v) {
158 continue;
159 }
160
161 t = r300_texture(texstate->sampler_views[i]->base.texture);
162
163 if (s->state.compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
164 state->unit[i].compare_mode_enabled = 1;
165
166 /* Pass depth texture swizzling to the compiler. */
167 if (texstate->sampler_views[i]) {
168 swizzle = texstate->sampler_views[i]->swizzle;
169
170 state->unit[i].depth_texture_swizzle =
171 RC_MAKE_SWIZZLE(swizzle[0], swizzle[1],
172 swizzle[2], swizzle[3]);
173 } else {
174 state->unit[i].depth_texture_swizzle = RC_SWIZZLE_XYZW;
175 }
176
177 /* Fortunately, no need to translate this. */
178 state->unit[i].texture_compare_func = s->state.compare_func;
179 }
180
181 state->unit[i].non_normalized_coords = !s->state.normalized_coords;
182
183 /* XXX this should probably take into account STR, not just S. */
184 if (t->desc.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->desc.b.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 const struct tgsi_token *tokens);
214
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(TGSI_PROCESSOR_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.tokens);
236
237 ureg_destroy(ureg);
238 }
239
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 OUT_CB_REG(R500_US_CONFIG, R500_ZERO_TIMES_ANYTHING_EQUALS_ZERO);
262 OUT_CB_REG(R500_US_PIXSIZE, code->max_temp_idx);
263 OUT_CB_REG(R500_US_FC_CTRL, code->us_fc_ctrl);
264 for(i = 0; i < code->int_constant_count; i++){
265 OUT_CB_REG(R500_US_FC_INT_CONST_0 + (i * 4),
266 code->int_constants[i]);
267 }
268 OUT_CB_REG(R500_US_CODE_RANGE,
269 R500_US_CODE_RANGE_ADDR(0) | R500_US_CODE_RANGE_SIZE(code->inst_end));
270 OUT_CB_REG(R500_US_CODE_OFFSET, 0);
271 OUT_CB_REG(R500_US_CODE_ADDR,
272 R500_US_CODE_START_ADDR(0) | R500_US_CODE_END_ADDR(code->inst_end));
273
274 OUT_CB_REG(R500_GA_US_VECTOR_INDEX, R500_GA_US_VECTOR_INDEX_TYPE_INSTR);
275 OUT_CB_ONE_REG(R500_GA_US_VECTOR_DATA, (code->inst_end + 1) * 6);
276 for (i = 0; i <= code->inst_end; i++) {
277 OUT_CB(code->inst[i].inst0);
278 OUT_CB(code->inst[i].inst1);
279 OUT_CB(code->inst[i].inst2);
280 OUT_CB(code->inst[i].inst3);
281 OUT_CB(code->inst[i].inst4);
282 OUT_CB(code->inst[i].inst5);
283 }
284
285 /* Emit immediates. */
286 if (imm_count) {
287 for(i = imm_first; i < imm_end; ++i) {
288 if (constants[i].Type == RC_CONSTANT_IMMEDIATE) {
289 const float *data = constants[i].u.Immediate;
290
291 OUT_CB_REG(R500_GA_US_VECTOR_INDEX,
292 R500_GA_US_VECTOR_INDEX_TYPE_CONST |
293 (i & R500_GA_US_VECTOR_INDEX_MASK));
294 OUT_CB_ONE_REG(R500_GA_US_VECTOR_DATA, 4);
295 OUT_CB_TABLE(data, 4);
296 }
297 }
298 }
299 } else { /* r300 */
300 struct r300_fragment_program_code *code = &generic_code->code.r300;
301
302 shader->cb_code_size = 19 +
303 (r300->screen->caps.is_r400 ? 2 : 0) +
304 code->alu.length * 4 +
305 (code->tex.length ? (1 + code->tex.length) : 0) +
306 imm_count * 5;
307
308 NEW_CB(shader->cb_code, shader->cb_code_size);
309
310 if (r300->screen->caps.is_r400)
311 OUT_CB_REG(R400_US_CODE_BANK, 0);
312
313 OUT_CB_REG(R300_US_CONFIG, code->config);
314 OUT_CB_REG(R300_US_PIXSIZE, code->pixsize);
315 OUT_CB_REG(R300_US_CODE_OFFSET, code->code_offset);
316
317 OUT_CB_REG_SEQ(R300_US_CODE_ADDR_0, 4);
318 OUT_CB_TABLE(code->code_addr, 4);
319
320 OUT_CB_REG_SEQ(R300_US_ALU_RGB_INST_0, code->alu.length);
321 for (i = 0; i < code->alu.length; i++)
322 OUT_CB(code->alu.inst[i].rgb_inst);
323
324 OUT_CB_REG_SEQ(R300_US_ALU_RGB_ADDR_0, code->alu.length);
325 for (i = 0; i < code->alu.length; i++)
326 OUT_CB(code->alu.inst[i].rgb_addr);
327
328 OUT_CB_REG_SEQ(R300_US_ALU_ALPHA_INST_0, code->alu.length);
329 for (i = 0; i < code->alu.length; i++)
330 OUT_CB(code->alu.inst[i].alpha_inst);
331
332 OUT_CB_REG_SEQ(R300_US_ALU_ALPHA_ADDR_0, code->alu.length);
333 for (i = 0; i < code->alu.length; i++)
334 OUT_CB(code->alu.inst[i].alpha_addr);
335
336 if (code->tex.length) {
337 OUT_CB_REG_SEQ(R300_US_TEX_INST_0, code->tex.length);
338 OUT_CB_TABLE(code->tex.inst, code->tex.length);
339 }
340
341 /* Emit immediates. */
342 if (imm_count) {
343 for(i = imm_first; i < imm_end; ++i) {
344 if (constants[i].Type == RC_CONSTANT_IMMEDIATE) {
345 const float *data = constants[i].u.Immediate;
346
347 OUT_CB_REG_SEQ(R300_PFS_PARAM_0_X + i * 16, 4);
348 OUT_CB(pack_float24(data[0]));
349 OUT_CB(pack_float24(data[1]));
350 OUT_CB(pack_float24(data[2]));
351 OUT_CB(pack_float24(data[3]));
352 }
353 }
354 }
355 }
356
357 OUT_CB_REG(R300_FG_DEPTH_SRC, shader->fg_depth_src);
358 OUT_CB_REG(R300_US_W_FMT, shader->us_out_w);
359 END_CB;
360 }
361
362 static void r300_translate_fragment_shader(
363 struct r300_context* r300,
364 struct r300_fragment_shader_code* shader,
365 const struct tgsi_token *tokens)
366 {
367 struct r300_fragment_program_compiler compiler;
368 struct tgsi_to_rc ttr;
369 int wpos, face;
370 unsigned i;
371
372 tgsi_scan_shader(tokens, &shader->info);
373 r300_shader_read_fs_inputs(&shader->info, &shader->inputs);
374
375 wpos = shader->inputs.wpos;
376 face = shader->inputs.face;
377
378 /* Setup the compiler. */
379 memset(&compiler, 0, sizeof(compiler));
380 rc_init(&compiler.Base);
381 DBG_ON(r300, DBG_FP) ? compiler.Base.Debug |= RC_DBG_LOG : 0;
382 DBG_ON(r300, DBG_P_STAT) ? compiler.Base.Debug |= RC_DBG_STATS : 0;
383
384 compiler.code = &shader->code;
385 compiler.state = shader->compare_state;
386 compiler.Base.is_r500 = r300->screen->caps.is_r500;
387 compiler.Base.disable_optimizations = DBG_ON(r300, DBG_NO_OPT);
388 compiler.Base.has_half_swizzles = TRUE;
389 compiler.Base.has_presub = TRUE;
390 compiler.Base.max_temp_regs = compiler.Base.is_r500 ? 128 : 32;
391 compiler.Base.max_constants = compiler.Base.is_r500 ? 256 : 32;
392 compiler.Base.max_alu_insts = compiler.Base.is_r500 ? 512 : 64;
393 compiler.AllocateHwInputs = &allocate_hardware_inputs;
394 compiler.UserData = &shader->inputs;
395
396 find_output_registers(&compiler, shader);
397
398 shader->write_all = FALSE;
399 for (i = 0; i < shader->info.num_properties; i++) {
400 if (shader->info.properties[i].name == TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS) {
401 shader->write_all = TRUE;
402 }
403 }
404
405 if (compiler.Base.Debug & RC_DBG_LOG) {
406 DBG(r300, DBG_FP, "r300: Initial fragment program\n");
407 tgsi_dump(tokens, 0);
408 }
409
410 /* Translate TGSI to our internal representation */
411 ttr.compiler = &compiler.Base;
412 ttr.info = &shader->info;
413 ttr.use_half_swizzles = TRUE;
414
415 r300_tgsi_to_rc(&ttr, tokens);
416
417 if (!r300->screen->caps.is_r500 ||
418 compiler.Base.Program.Constants.Count > 200) {
419 compiler.Base.remove_unused_constants = TRUE;
420 }
421
422 /**
423 * Transform the program to support WPOS.
424 *
425 * Introduce a small fragment at the start of the program that will be
426 * the only code that directly reads the WPOS input.
427 * All other code pieces that reference that input will be rewritten
428 * to read from a newly allocated temporary. */
429 if (wpos != ATTR_UNUSED) {
430 /* Moving the input to some other reg is not really necessary. */
431 rc_transform_fragment_wpos(&compiler.Base, wpos, wpos, TRUE);
432 }
433
434 if (face != ATTR_UNUSED) {
435 rc_transform_fragment_face(&compiler.Base, face);
436 }
437
438 /* Invoke the compiler */
439 r3xx_compile_fragment_program(&compiler);
440
441 if (compiler.Base.Error) {
442 fprintf(stderr, "r300 FP: Compiler Error:\n%sUsing a dummy shader"
443 " instead.\n", compiler.Base.ErrorMsg);
444
445 if (shader->dummy) {
446 fprintf(stderr, "r300 FP: Cannot compile the dummy shader! "
447 "Giving up...\n");
448 abort();
449 }
450
451 rc_destroy(&compiler.Base);
452 r300_dummy_fragment_shader(r300, shader);
453 return;
454 }
455
456 /* Shaders with zero instructions are invalid,
457 * use the dummy shader instead. */
458 if (shader->code.code.r500.inst_end == -1) {
459 rc_destroy(&compiler.Base);
460 r300_dummy_fragment_shader(r300, shader);
461 return;
462 }
463
464 /* Initialize numbers of constants for each type. */
465 shader->externals_count = 0;
466 for (i = 0;
467 i < shader->code.constants.Count &&
468 shader->code.constants.Constants[i].Type == RC_CONSTANT_EXTERNAL; i++) {
469 shader->externals_count = i+1;
470 }
471 shader->immediates_count = 0;
472 shader->rc_state_count = 0;
473
474 for (i = shader->externals_count; i < shader->code.constants.Count; i++) {
475 switch (shader->code.constants.Constants[i].Type) {
476 case RC_CONSTANT_IMMEDIATE:
477 ++shader->immediates_count;
478 break;
479 case RC_CONSTANT_STATE:
480 ++shader->rc_state_count;
481 break;
482 default:
483 assert(0);
484 }
485 }
486
487 /* Setup shader depth output. */
488 if (shader->code.writes_depth) {
489 shader->fg_depth_src = R300_FG_DEPTH_SRC_SHADER;
490 shader->us_out_w = R300_W_FMT_W24 | R300_W_SRC_US;
491 } else {
492 shader->fg_depth_src = R300_FG_DEPTH_SRC_SCAN;
493 shader->us_out_w = R300_W_FMT_W0 | R300_W_SRC_US;
494 }
495
496 /* And, finally... */
497 rc_destroy(&compiler.Base);
498
499 /* Build the command buffer. */
500 r300_emit_fs_code_to_buffer(r300, shader);
501 }
502
503 boolean r300_pick_fragment_shader(struct r300_context* r300)
504 {
505 struct r300_fragment_shader* fs = r300_fs(r300);
506 struct r300_fragment_program_external_state state = {{{ 0 }}};
507 struct r300_fragment_shader_code* ptr;
508
509 get_external_state(r300, &state);
510
511 if (!fs->first) {
512 /* Build the fragment shader for the first time. */
513 fs->first = fs->shader = CALLOC_STRUCT(r300_fragment_shader_code);
514
515 memcpy(&fs->shader->compare_state, &state,
516 sizeof(struct r300_fragment_program_external_state));
517 r300_translate_fragment_shader(r300, fs->shader, fs->state.tokens);
518 return TRUE;
519
520 } else {
521 /* Check if the currently-bound shader has been compiled
522 * with the texture-compare state we need. */
523 if (memcmp(&fs->shader->compare_state, &state, sizeof(state)) != 0) {
524 /* Search for the right shader. */
525 ptr = fs->first;
526 while (ptr) {
527 if (memcmp(&ptr->compare_state, &state, sizeof(state)) == 0) {
528 if (fs->shader != ptr) {
529 fs->shader = ptr;
530 return TRUE;
531 }
532 /* The currently-bound one is OK. */
533 return FALSE;
534 }
535 ptr = ptr->next;
536 }
537
538 /* Not found, gotta compile a new one. */
539 ptr = CALLOC_STRUCT(r300_fragment_shader_code);
540 ptr->next = fs->first;
541 fs->first = fs->shader = ptr;
542
543 ptr->compare_state = state;
544 r300_translate_fragment_shader(r300, ptr, fs->state.tokens);
545 return TRUE;
546 }
547 }
548
549 return FALSE;
550 }