r300_fs.c revision 81e5188f66248424d54fcf1d85a81510694bd472
1/*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *                Joakim Sindholt <opensource@zhasha.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24#include "r300_fs.h"
25
26#include "r300_tgsi_to_rc.h"
27
28#include "radeon_compiler.h"
29
30static void find_output_registers(struct r300_fragment_program_compiler * compiler,
31                                  struct r300_fragment_shader * fs)
32{
33    unsigned i;
34
35    /* Mark the outputs as not present initially */
36    compiler->OutputColor = fs->info.num_outputs;
37    compiler->OutputDepth = fs->info.num_outputs;
38
39    /* Now see where they really are. */
40    for(i = 0; i < fs->info.num_outputs; ++i) {
41        switch(fs->info.output_semantic_name[i]) {
42            case TGSI_SEMANTIC_COLOR:
43                compiler->OutputColor = i;
44                break;
45            case TGSI_SEMANTIC_POSITION:
46                compiler->OutputDepth = i;
47                break;
48        }
49    }
50}
51
52static void allocate_hardware_inputs(
53    struct r300_fragment_program_compiler * c,
54    void (*allocate)(void * data, unsigned input, unsigned hwreg),
55    void * mydata)
56{
57    struct tgsi_shader_info* info = &((struct r300_fragment_shader*)c->UserData)->info;
58    int total_colors = 0;
59    int colors = 0;
60    int total_generic = 0;
61    int generic = 0;
62    int i;
63
64    for (i = 0; i < info->num_inputs; i++) {
65        switch (info->input_semantic_name[i]) {
66            case TGSI_SEMANTIC_COLOR:
67                total_colors++;
68                break;
69            case TGSI_SEMANTIC_FOG:
70            case TGSI_SEMANTIC_GENERIC:
71                total_generic++;
72                break;
73        }
74    }
75
76    for(i = 0; i < info->num_inputs; i++) {
77        switch (info->input_semantic_name[i]) {
78            case TGSI_SEMANTIC_COLOR:
79                allocate(mydata, i, colors);
80                colors++;
81                break;
82            case TGSI_SEMANTIC_FOG:
83            case TGSI_SEMANTIC_GENERIC:
84                allocate(mydata, i, total_colors + generic);
85                generic++;
86                break;
87        }
88    }
89}
90
91void r300_translate_fragment_shader(struct r300_context* r300,
92                                    struct r300_fragment_shader* fs)
93{
94    struct r300_fragment_program_compiler compiler;
95    struct tgsi_to_rc ttr;
96
97    memset(&compiler, 0, sizeof(compiler));
98    rc_init(&compiler.Base);
99    compiler.Base.Debug = DBG_ON(r300, DBG_FP);
100
101    compiler.code = &fs->code;
102    compiler.is_r500 = r300_screen(r300->context.screen)->caps->is_r500;
103    compiler.AllocateHwInputs = &allocate_hardware_inputs;
104    compiler.UserData = fs;
105
106    /* TODO: Program compilation depends on texture compare modes,
107     * which are sampler state. Therefore, programs need to be recompiled
108     * depending on this state as in the classic Mesa driver.
109     *
110     * This is not yet handled correctly.
111     */
112
113    find_output_registers(&compiler, fs);
114
115    if (compiler.Base.Debug) {
116        debug_printf("r300: Initial fragment program\n");
117        tgsi_dump(fs->state.tokens, 0);
118    }
119
120    /* Translate TGSI to our internal representation */
121    ttr.compiler = &compiler.Base;
122    ttr.info = &fs->info;
123
124    r300_tgsi_to_rc(&ttr, fs->state.tokens);
125
126    /* Invoke the compiler */
127    r3xx_compile_fragment_program(&compiler);
128    if (compiler.Base.Error) {
129        /* Todo: Fallback to software rendering gracefully? */
130        fprintf(stderr, "r300 FP: Compiler error: %s\n", compiler.Base.ErrorMsg);
131
132        if (compiler.is_r500) {
133            memcpy(compiler.code, &r5xx_passthrough_fragment_shader, sizeof(r5xx_passthrough_fragment_shader));
134        } else {
135            memcpy(compiler.code, &r3xx_passthrough_fragment_shader, sizeof(r3xx_passthrough_fragment_shader));
136        }
137    }
138
139    /* And, finally... */
140    rc_destroy(&compiler.Base);
141    fs->translated = TRUE;
142}
143