r300_fs.c revision 2824d5687a19e42ba0da8fd08e80610c4469a3b3
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. */
43void 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            default:
76                fprintf(stderr, "r300: FP: Unknown input semantic: %i\n",
77                        info->input_semantic_name[i]);
78        }
79    }
80}
81
82static void find_output_registers(struct r300_fragment_program_compiler * compiler,
83                                  struct r300_fragment_shader_code *shader)
84{
85    unsigned i, colorbuf_count = 0;
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[colorbuf_count] = i;
99                colorbuf_count++;
100                break;
101            case TGSI_SEMANTIC_POSITION:
102                compiler->OutputDepth = i;
103                break;
104        }
105    }
106}
107
108static void allocate_hardware_inputs(
109    struct r300_fragment_program_compiler * c,
110    void (*allocate)(void * data, unsigned input, unsigned hwreg),
111    void * mydata)
112{
113    struct r300_shader_semantics* inputs =
114        (struct r300_shader_semantics*)c->UserData;
115    int i, reg = 0;
116
117    /* Allocate input registers. */
118    for (i = 0; i < ATTR_COLOR_COUNT; i++) {
119        if (inputs->color[i] != ATTR_UNUSED) {
120            allocate(mydata, inputs->color[i], reg++);
121        }
122    }
123    for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
124        if (inputs->generic[i] != ATTR_UNUSED) {
125            allocate(mydata, inputs->generic[i], reg++);
126        }
127    }
128    if (inputs->fog != ATTR_UNUSED) {
129        allocate(mydata, inputs->fog, reg++);
130    }
131    if (inputs->wpos != ATTR_UNUSED) {
132        allocate(mydata, inputs->wpos, reg++);
133    }
134}
135
136static void get_external_state(
137    struct r300_context* r300,
138    struct r300_fragment_program_external_state* state)
139{
140    struct r300_textures_state *texstate = r300->textures_state.state;
141    unsigned i;
142    unsigned char *swizzle;
143
144    for (i = 0; i < texstate->sampler_state_count; i++) {
145        struct r300_sampler_state* s = texstate->sampler_states[i];
146
147        if (!s) {
148            continue;
149        }
150
151        if (s->state.compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
152            state->unit[i].compare_mode_enabled = 1;
153
154            /* Pass depth texture swizzling to the compiler. */
155            if (texstate->sampler_views[i]) {
156                swizzle = texstate->sampler_views[i]->swizzle;
157
158                state->unit[i].depth_texture_swizzle =
159                    RC_MAKE_SWIZZLE(swizzle[0], swizzle[1],
160                                    swizzle[2], swizzle[3]);
161            } else {
162                state->unit[i].depth_texture_swizzle = RC_SWIZZLE_XYZW;
163            }
164
165            /* Fortunately, no need to translate this. */
166            state->unit[i].texture_compare_func = s->state.compare_func;
167        }
168
169        state->unit[i].non_normalized_coords = !s->state.normalized_coords;
170
171        if (texstate->sampler_views[i]) {
172            struct r300_texture *t;
173            t = (struct r300_texture*)texstate->sampler_views[i]->base.texture;
174
175            /* XXX this should probably take into account STR, not just S. */
176            if (t->desc.is_npot) {
177                switch (s->state.wrap_s) {
178                    case PIPE_TEX_WRAP_REPEAT:
179                        state->unit[i].wrap_mode = RC_WRAP_REPEAT;
180                        state->unit[i].fake_npot = TRUE;
181                        break;
182
183                    case PIPE_TEX_WRAP_MIRROR_REPEAT:
184                        state->unit[i].wrap_mode = RC_WRAP_MIRRORED_REPEAT;
185                        state->unit[i].fake_npot = TRUE;
186                        break;
187
188                    case PIPE_TEX_WRAP_MIRROR_CLAMP:
189                    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
190                    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
191                        state->unit[i].wrap_mode = RC_WRAP_MIRRORED_CLAMP;
192                        state->unit[i].fake_npot = TRUE;
193                        break;
194
195                    default:
196                        state->unit[i].wrap_mode = RC_WRAP_NONE;
197                        break;
198                }
199            }
200        }
201    }
202}
203
204static void r300_translate_fragment_shader(
205    struct r300_context* r300,
206    struct r300_fragment_shader_code* shader,
207    const struct tgsi_token *tokens);
208
209static void r300_dummy_fragment_shader(
210    struct r300_context* r300,
211    struct r300_fragment_shader_code* shader)
212{
213    struct pipe_shader_state state;
214    struct ureg_program *ureg;
215    struct ureg_dst out;
216    struct ureg_src imm;
217
218    /* Make a simple fragment shader which outputs (0, 0, 0, 1) */
219    ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT);
220    out = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
221    imm = ureg_imm4f(ureg, 0, 0, 0, 1);
222
223    ureg_MOV(ureg, out, imm);
224    ureg_END(ureg);
225
226    state.tokens = ureg_finalize(ureg);
227
228    shader->dummy = TRUE;
229    r300_translate_fragment_shader(r300, shader, state.tokens);
230
231    ureg_destroy(ureg);
232}
233
234static void r300_emit_fs_code_to_buffer(
235    struct r300_context *r300,
236    struct r300_fragment_shader_code *shader)
237{
238    struct rX00_fragment_program_code *generic_code = &shader->code;
239    unsigned imm_count = shader->immediates_count;
240    unsigned imm_first = shader->externals_count;
241    unsigned imm_end = generic_code->constants.Count;
242    struct rc_constant *constants = generic_code->constants.Constants;
243    unsigned i;
244    CB_LOCALS;
245
246    if (r300->screen->caps.is_r500) {
247        struct r500_fragment_program_code *code = &generic_code->code.r500;
248
249        shader->cb_code_size = 19 +
250                               ((code->inst_end + 1) * 6) +
251                               imm_count * 7 +
252			       code->int_constant_count * 2;
253
254        NEW_CB(shader->cb_code, shader->cb_code_size);
255        OUT_CB_REG(R500_US_CONFIG, R500_ZERO_TIMES_ANYTHING_EQUALS_ZERO);
256        OUT_CB_REG(R500_US_PIXSIZE, code->max_temp_idx);
257        OUT_CB_REG(R500_US_FC_CTRL, code->us_fc_ctrl);
258        for(i = 0; i < code->int_constant_count; i++){
259		OUT_CB_REG(R500_US_FC_INT_CONST_0 + (i * 4),
260						code->int_constants[i]);
261	}
262	OUT_CB_REG(R500_US_CODE_RANGE,
263                   R500_US_CODE_RANGE_ADDR(0) | R500_US_CODE_RANGE_SIZE(code->inst_end));
264        OUT_CB_REG(R500_US_CODE_OFFSET, 0);
265        OUT_CB_REG(R500_US_CODE_ADDR,
266                   R500_US_CODE_START_ADDR(0) | R500_US_CODE_END_ADDR(code->inst_end));
267
268        OUT_CB_REG(R500_GA_US_VECTOR_INDEX, R500_GA_US_VECTOR_INDEX_TYPE_INSTR);
269        OUT_CB_ONE_REG(R500_GA_US_VECTOR_DATA, (code->inst_end + 1) * 6);
270        for (i = 0; i <= code->inst_end; i++) {
271            OUT_CB(code->inst[i].inst0);
272            OUT_CB(code->inst[i].inst1);
273            OUT_CB(code->inst[i].inst2);
274            OUT_CB(code->inst[i].inst3);
275            OUT_CB(code->inst[i].inst4);
276            OUT_CB(code->inst[i].inst5);
277        }
278
279        /* Emit immediates. */
280        if (imm_count) {
281            for(i = imm_first; i < imm_end; ++i) {
282                if (constants[i].Type == RC_CONSTANT_IMMEDIATE) {
283                    const float *data = constants[i].u.Immediate;
284
285                    OUT_CB_REG(R500_GA_US_VECTOR_INDEX,
286                               R500_GA_US_VECTOR_INDEX_TYPE_CONST |
287                               (i & R500_GA_US_VECTOR_INDEX_MASK));
288                    OUT_CB_ONE_REG(R500_GA_US_VECTOR_DATA, 4);
289                    OUT_CB_TABLE(data, 4);
290                }
291            }
292        }
293    } else { /* r300 */
294        struct r300_fragment_program_code *code = &generic_code->code.r300;
295
296        shader->cb_code_size = 19 +
297                               (r300->screen->caps.is_r400 ? 2 : 0) +
298                               code->alu.length * 4 +
299                               (code->tex.length ? (1 + code->tex.length) : 0) +
300                               imm_count * 5;
301
302        NEW_CB(shader->cb_code, shader->cb_code_size);
303
304        if (r300->screen->caps.is_r400)
305            OUT_CB_REG(R400_US_CODE_BANK, 0);
306
307        OUT_CB_REG(R300_US_CONFIG, code->config);
308        OUT_CB_REG(R300_US_PIXSIZE, code->pixsize);
309        OUT_CB_REG(R300_US_CODE_OFFSET, code->code_offset);
310
311        OUT_CB_REG_SEQ(R300_US_CODE_ADDR_0, 4);
312        OUT_CB_TABLE(code->code_addr, 4);
313
314        OUT_CB_REG_SEQ(R300_US_ALU_RGB_INST_0, code->alu.length);
315        for (i = 0; i < code->alu.length; i++)
316            OUT_CB(code->alu.inst[i].rgb_inst);
317
318        OUT_CB_REG_SEQ(R300_US_ALU_RGB_ADDR_0, code->alu.length);
319        for (i = 0; i < code->alu.length; i++)
320            OUT_CB(code->alu.inst[i].rgb_addr);
321
322        OUT_CB_REG_SEQ(R300_US_ALU_ALPHA_INST_0, code->alu.length);
323        for (i = 0; i < code->alu.length; i++)
324            OUT_CB(code->alu.inst[i].alpha_inst);
325
326        OUT_CB_REG_SEQ(R300_US_ALU_ALPHA_ADDR_0, code->alu.length);
327        for (i = 0; i < code->alu.length; i++)
328            OUT_CB(code->alu.inst[i].alpha_addr);
329
330        if (code->tex.length) {
331            OUT_CB_REG_SEQ(R300_US_TEX_INST_0, code->tex.length);
332            OUT_CB_TABLE(code->tex.inst, code->tex.length);
333        }
334
335        /* Emit immediates. */
336        if (imm_count) {
337            for(i = imm_first; i < imm_end; ++i) {
338                if (constants[i].Type == RC_CONSTANT_IMMEDIATE) {
339                    const float *data = constants[i].u.Immediate;
340
341                    OUT_CB_REG_SEQ(R300_PFS_PARAM_0_X + i * 16, 4);
342                    OUT_CB(pack_float24(data[0]));
343                    OUT_CB(pack_float24(data[1]));
344                    OUT_CB(pack_float24(data[2]));
345                    OUT_CB(pack_float24(data[3]));
346                }
347            }
348        }
349    }
350
351    OUT_CB_REG(R300_FG_DEPTH_SRC, shader->fg_depth_src);
352    OUT_CB_REG(R300_US_W_FMT, shader->us_out_w);
353    END_CB;
354}
355
356static void r300_translate_fragment_shader(
357    struct r300_context* r300,
358    struct r300_fragment_shader_code* shader,
359    const struct tgsi_token *tokens)
360{
361    struct r300_fragment_program_compiler compiler;
362    struct tgsi_to_rc ttr;
363    int wpos;
364    unsigned i;
365
366    tgsi_scan_shader(tokens, &shader->info);
367    r300_shader_read_fs_inputs(&shader->info, &shader->inputs);
368
369    wpos = shader->inputs.wpos;
370
371    /* Setup the compiler. */
372    memset(&compiler, 0, sizeof(compiler));
373    rc_init(&compiler.Base);
374    compiler.Base.Debug = DBG_ON(r300, DBG_FP);
375
376    compiler.code = &shader->code;
377    compiler.state = shader->compare_state;
378    compiler.Base.is_r500 = r300->screen->caps.is_r500;
379    compiler.Base.max_temp_regs = compiler.Base.is_r500 ? 128 : 32;
380    compiler.AllocateHwInputs = &allocate_hardware_inputs;
381    compiler.UserData = &shader->inputs;
382
383    find_output_registers(&compiler, shader);
384
385    if (compiler.Base.Debug) {
386        debug_printf("r300: Initial fragment program\n");
387        tgsi_dump(tokens, 0);
388    }
389
390    /* Translate TGSI to our internal representation */
391    ttr.compiler = &compiler.Base;
392    ttr.info = &shader->info;
393    ttr.use_half_swizzles = TRUE;
394
395    r300_tgsi_to_rc(&ttr, tokens);
396
397    /**
398     * Transform the program to support WPOS.
399     *
400     * Introduce a small fragment at the start of the program that will be
401     * the only code that directly reads the WPOS input.
402     * All other code pieces that reference that input will be rewritten
403     * to read from a newly allocated temporary. */
404    if (wpos != ATTR_UNUSED) {
405        /* Moving the input to some other reg is not really necessary. */
406        rc_transform_fragment_wpos(&compiler.Base, wpos, wpos, TRUE);
407    }
408
409    /* Invoke the compiler */
410    r3xx_compile_fragment_program(&compiler);
411
412    /* Shaders with zero instructions are invalid,
413     * use the dummy shader instead. */
414    if (shader->code.code.r500.inst_end == -1) {
415        rc_destroy(&compiler.Base);
416        r300_dummy_fragment_shader(r300, shader);
417        return;
418    }
419
420    if (compiler.Base.Error) {
421        fprintf(stderr, "r300 FP: Compiler Error:\n%sUsing a dummy shader"
422                " instead.\nIf there's an 'unknown opcode' message, please"
423                " file a bug report and attach this log.\n", compiler.Base.ErrorMsg);
424
425        if (shader->dummy) {
426            fprintf(stderr, "r300 FP: Cannot compile the dummy shader! "
427                    "Giving up...\n");
428            abort();
429        }
430
431        rc_destroy(&compiler.Base);
432        r300_dummy_fragment_shader(r300, shader);
433        return;
434    }
435
436    /* Initialize numbers of constants for each type. */
437    shader->externals_count = ttr.immediate_offset;
438    shader->immediates_count = 0;
439    shader->rc_state_count = 0;
440
441    for (i = shader->externals_count; i < shader->code.constants.Count; i++) {
442        switch (shader->code.constants.Constants[i].Type) {
443            case RC_CONSTANT_IMMEDIATE:
444                ++shader->immediates_count;
445                break;
446            case RC_CONSTANT_STATE:
447                ++shader->rc_state_count;
448                break;
449            default:
450                assert(0);
451        }
452    }
453
454    /* Setup shader depth output. */
455    if (shader->code.writes_depth) {
456        shader->fg_depth_src = R300_FG_DEPTH_SRC_SHADER;
457        shader->us_out_w = R300_W_FMT_W24 | R300_W_SRC_US;
458    } else {
459        shader->fg_depth_src = R300_FG_DEPTH_SRC_SCAN;
460        shader->us_out_w = R300_W_FMT_W0 | R300_W_SRC_US;
461    }
462
463    /* And, finally... */
464    rc_destroy(&compiler.Base);
465
466    /* Build the command buffer. */
467    r300_emit_fs_code_to_buffer(r300, shader);
468}
469
470boolean r300_pick_fragment_shader(struct r300_context* r300)
471{
472    struct r300_fragment_shader* fs = r300_fs(r300);
473    struct r300_fragment_program_external_state state = {{{ 0 }}};
474    struct r300_fragment_shader_code* ptr;
475
476    get_external_state(r300, &state);
477
478    if (!fs->first) {
479        /* Build the fragment shader for the first time. */
480        fs->first = fs->shader = CALLOC_STRUCT(r300_fragment_shader_code);
481
482        memcpy(&fs->shader->compare_state, &state,
483            sizeof(struct r300_fragment_program_external_state));
484        r300_translate_fragment_shader(r300, fs->shader, fs->state.tokens);
485        return TRUE;
486
487    } else {
488        /* Check if the currently-bound shader has been compiled
489         * with the texture-compare state we need. */
490        if (memcmp(&fs->shader->compare_state, &state, sizeof(state)) != 0) {
491            /* Search for the right shader. */
492            ptr = fs->first;
493            while (ptr) {
494                if (memcmp(&ptr->compare_state, &state, sizeof(state)) == 0) {
495                    if (fs->shader != ptr) {
496                        fs->shader = ptr;
497                        return TRUE;
498                    }
499                    /* The currently-bound one is OK. */
500                    return FALSE;
501                }
502                ptr = ptr->next;
503            }
504
505            /* Not found, gotta compile a new one. */
506            ptr = CALLOC_STRUCT(r300_fragment_shader_code);
507            ptr->next = fs->first;
508            fs->first = fs->shader = ptr;
509
510            ptr->compare_state = state;
511            r300_translate_fragment_shader(r300, ptr, fs->state.tokens);
512            return TRUE;
513        }
514    }
515
516    return FALSE;
517}
518