Merge branch 'mesa_7_7_branch'
[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 "tgsi/tgsi_dump.h"
26
27 #include "r300_context.h"
28 #include "r300_screen.h"
29 #include "r300_fs.h"
30 #include "r300_tgsi_to_rc.h"
31
32 #include "radeon_code.h"
33 #include "radeon_compiler.h"
34
35 /* Convert info about FS input semantics to r300_shader_semantics. */
36 static void r300_shader_read_fs_inputs(struct tgsi_shader_info* info,
37 struct r300_shader_semantics* fs_inputs)
38 {
39 int i;
40 unsigned index;
41
42 r300_shader_semantics_reset(fs_inputs);
43
44 for (i = 0; i < info->num_inputs; i++) {
45 index = info->input_semantic_index[i];
46
47 switch (info->input_semantic_name[i]) {
48 case TGSI_SEMANTIC_COLOR:
49 assert(index <= ATTR_COLOR_COUNT);
50 fs_inputs->color[index] = i;
51 break;
52
53 case TGSI_SEMANTIC_GENERIC:
54 assert(index <= ATTR_GENERIC_COUNT);
55 fs_inputs->generic[index] = i;
56 break;
57
58 case TGSI_SEMANTIC_FOG:
59 assert(index == 0);
60 fs_inputs->fog = i;
61 break;
62
63 default:
64 assert(0);
65 }
66 }
67 }
68
69
70 static void find_output_registers(struct r300_fragment_program_compiler * compiler,
71 struct r300_fragment_shader * fs)
72 {
73 unsigned i;
74
75 /* Mark the outputs as not present initially */
76 compiler->OutputColor = fs->info.num_outputs;
77 compiler->OutputDepth = fs->info.num_outputs;
78
79 /* Now see where they really are. */
80 for(i = 0; i < fs->info.num_outputs; ++i) {
81 switch(fs->info.output_semantic_name[i]) {
82 case TGSI_SEMANTIC_COLOR:
83 compiler->OutputColor = i;
84 break;
85 case TGSI_SEMANTIC_POSITION:
86 compiler->OutputDepth = i;
87 break;
88 }
89 }
90 }
91
92 static void allocate_hardware_inputs(
93 struct r300_fragment_program_compiler * c,
94 void (*allocate)(void * data, unsigned input, unsigned hwreg),
95 void * mydata)
96 {
97 struct r300_shader_semantics* inputs =
98 &((struct r300_fragment_shader*)c->UserData)->inputs;
99 int i, reg = 0;
100
101 /* Allocate input registers. */
102 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
103 if (inputs->color[i] != ATTR_UNUSED) {
104 allocate(mydata, inputs->color[i], reg++);
105 }
106 }
107 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
108 if (inputs->generic[i] != ATTR_UNUSED) {
109 allocate(mydata, inputs->generic[i], reg++);
110 }
111 }
112 if (inputs->fog != ATTR_UNUSED) {
113 allocate(mydata, inputs->fog, reg++);
114 }
115 }
116
117 void r300_translate_fragment_shader(struct r300_context* r300,
118 struct r300_fragment_shader* fs)
119 {
120 struct r300_fragment_program_compiler compiler;
121 struct tgsi_to_rc ttr;
122
123 /* Initialize. */
124 r300_shader_read_fs_inputs(&fs->info, &fs->inputs);
125
126 /* Setup the compiler. */
127 memset(&compiler, 0, sizeof(compiler));
128 rc_init(&compiler.Base);
129 compiler.Base.Debug = DBG_ON(r300, DBG_FP);
130
131 compiler.code = &fs->code;
132 compiler.is_r500 = r300_screen(r300->context.screen)->caps->is_r500;
133 compiler.AllocateHwInputs = &allocate_hardware_inputs;
134 compiler.UserData = fs;
135
136 /* XXX: Program compilation depends on texture compare modes,
137 * which are sampler state. Therefore, programs need to be recompiled
138 * depending on this state as in the classic Mesa driver.
139 *
140 * This is not yet handled correctly.
141 */
142
143 find_output_registers(&compiler, fs);
144
145 if (compiler.Base.Debug) {
146 debug_printf("r300: Initial fragment program\n");
147 tgsi_dump(fs->state.tokens, 0);
148 }
149
150 /* Translate TGSI to our internal representation */
151 ttr.compiler = &compiler.Base;
152 ttr.info = &fs->info;
153
154 r300_tgsi_to_rc(&ttr, fs->state.tokens);
155
156 /* Invoke the compiler */
157 r3xx_compile_fragment_program(&compiler);
158 if (compiler.Base.Error) {
159 /* XXX failover maybe? */
160 DBG(r300, DBG_FP, "r300: Error compiling fragment program: %s\n",
161 compiler.Base.ErrorMsg);
162 assert(0);
163 }
164
165 /* And, finally... */
166 rc_destroy(&compiler.Base);
167 fs->translated = TRUE;
168 }