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 "util/u_math.h"
26 #include "util/u_memory.h"
27
28 #include "tgsi/tgsi_dump.h"
29
30 #include "r300_context.h"
31 #include "r300_screen.h"
32 #include "r300_fs.h"
33 #include "r300_tgsi_to_rc.h"
34
35 #include "radeon_code.h"
36 #include "radeon_compiler.h"
37
38 /* Convert info about FS input semantics to r300_shader_semantics. */
39 void r300_shader_read_fs_inputs(struct tgsi_shader_info* info,
40 struct r300_shader_semantics* fs_inputs)
41 {
42 int i;
43 unsigned index;
44
45 r300_shader_semantics_reset(fs_inputs);
46
47 for (i = 0; i < info->num_inputs; i++) {
48 index = info->input_semantic_index[i];
49
50 switch (info->input_semantic_name[i]) {
51 case TGSI_SEMANTIC_COLOR:
52 assert(index <= ATTR_COLOR_COUNT);
53 fs_inputs->color[index] = i;
54 break;
55
56 case TGSI_SEMANTIC_GENERIC:
57 assert(index <= ATTR_GENERIC_COUNT);
58 fs_inputs->generic[index] = i;
59 break;
60
61 case TGSI_SEMANTIC_FOG:
62 assert(index == 0);
63 fs_inputs->fog = i;
64 break;
65
66 case TGSI_SEMANTIC_POSITION:
67 assert(index == 0);
68 fs_inputs->wpos = i;
69 break;
70
71 default:
72 assert(0);
73 }
74 }
75 }
76
77 static void find_output_registers(struct r300_fragment_program_compiler * compiler,
78 struct r300_fragment_shader * fs)
79 {
80 unsigned i;
81
82 /* Mark the outputs as not present initially */
83 compiler->OutputColor = fs->info.num_outputs;
84 compiler->OutputDepth = fs->info.num_outputs;
85
86 /* Now see where they really are. */
87 for(i = 0; i < fs->info.num_outputs; ++i) {
88 switch(fs->info.output_semantic_name[i]) {
89 case TGSI_SEMANTIC_COLOR:
90 compiler->OutputColor = i;
91 break;
92 case TGSI_SEMANTIC_POSITION:
93 compiler->OutputDepth = i;
94 break;
95 }
96 }
97 }
98
99 static void allocate_hardware_inputs(
100 struct r300_fragment_program_compiler * c,
101 void (*allocate)(void * data, unsigned input, unsigned hwreg),
102 void * mydata)
103 {
104 struct r300_shader_semantics* inputs =
105 (struct r300_shader_semantics*)c->UserData;
106 int i, reg = 0;
107
108 /* Allocate input registers. */
109 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
110 if (inputs->color[i] != ATTR_UNUSED) {
111 allocate(mydata, inputs->color[i], reg++);
112 }
113 }
114 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
115 if (inputs->generic[i] != ATTR_UNUSED) {
116 allocate(mydata, inputs->generic[i], reg++);
117 }
118 }
119 if (inputs->fog != ATTR_UNUSED) {
120 allocate(mydata, inputs->fog, reg++);
121 }
122 if (inputs->wpos != ATTR_UNUSED) {
123 allocate(mydata, inputs->wpos, reg++);
124 }
125 }
126
127 static void get_compare_state(
128 struct r300_context* r300,
129 struct r300_fragment_program_external_state* state,
130 unsigned shadow_samplers)
131 {
132 memset(state, 0, sizeof(*state));
133
134 for (int i = 0; i < r300->sampler_count; i++) {
135 struct r300_sampler_state* s = r300->sampler_states[i];
136
137 if (s && s->state.compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
138 /* XXX Gallium doesn't provide us with any information regarding
139 * this mode, so we are screwed. I'm setting 0 = LUMINANCE. */
140 state->unit[i].depth_texture_mode = 0;
141
142 /* Fortunately, no need to translate this. */
143 state->unit[i].texture_compare_func = s->state.compare_func;
144 }
145 }
146 }
147
148 static void r300_translate_fragment_shader(
149 struct r300_context* r300,
150 struct r300_fragment_shader_code* shader)
151 {
152 struct r300_fragment_shader* fs = r300->fs;
153 struct r300_fragment_program_compiler compiler;
154 struct tgsi_to_rc ttr;
155 int wpos = fs->inputs.wpos;
156
157 /* Setup the compiler. */
158 memset(&compiler, 0, sizeof(compiler));
159 rc_init(&compiler.Base);
160 compiler.Base.Debug = DBG_ON(r300, DBG_FP);
161
162 compiler.code = &shader->code;
163 compiler.state = shader->compare_state;
164 compiler.is_r500 = r300_screen(r300->context.screen)->caps->is_r500;
165 compiler.AllocateHwInputs = &allocate_hardware_inputs;
166 compiler.UserData = &fs->inputs;
167
168 find_output_registers(&compiler, fs);
169
170 if (compiler.Base.Debug) {
171 debug_printf("r300: Initial fragment program\n");
172 tgsi_dump(fs->state.tokens, 0);
173 }
174
175 /* Translate TGSI to our internal representation */
176 ttr.compiler = &compiler.Base;
177 ttr.info = &fs->info;
178
179 r300_tgsi_to_rc(&ttr, fs->state.tokens);
180
181 fs->shadow_samplers = compiler.Base.Program.ShadowSamplers;
182
183 /**
184 * Transform the program to support WPOS.
185 *
186 * Introduce a small fragment at the start of the program that will be
187 * the only code that directly reads the WPOS input.
188 * All other code pieces that reference that input will be rewritten
189 * to read from a newly allocated temporary. */
190 if (wpos != ATTR_UNUSED) {
191 /* Moving the input to some other reg is not really necessary. */
192 rc_transform_fragment_wpos(&compiler.Base, wpos, wpos, TRUE);
193 }
194
195 /* Invoke the compiler */
196 r3xx_compile_fragment_program(&compiler);
197 if (compiler.Base.Error) {
198 /* XXX failover maybe? */
199 DBG(r300, DBG_FP, "r300: Error compiling fragment program: %s\n",
200 compiler.Base.ErrorMsg);
201 assert(0);
202 }
203
204 /* And, finally... */
205 rc_destroy(&compiler.Base);
206 }
207
208 boolean r300_pick_fragment_shader(struct r300_context* r300)
209 {
210 struct r300_fragment_shader* fs = r300->fs;
211 struct r300_fragment_program_external_state state;
212 struct r300_fragment_shader_code* ptr;
213
214 if (!fs->first) {
215 /* Build the fragment shader for the first time. */
216 fs->first = fs->shader = CALLOC_STRUCT(r300_fragment_shader_code);
217
218 /* BTW shadow samplers will be known after the first translation,
219 * therefore we set ~0, which means it should look at all sampler
220 * states. This choice doesn't have any impact on the correctness. */
221 get_compare_state(r300, &fs->shader->compare_state, ~0);
222 r300_translate_fragment_shader(r300, fs->shader);
223 return TRUE;
224
225 } else if (fs->shadow_samplers) {
226 get_compare_state(r300, &state, fs->shadow_samplers);
227
228 /* Check if the currently-bound shader has been compiled
229 * with the texture-compare state we need. */
230 if (memcmp(&fs->shader->compare_state, &state, sizeof(state)) != 0) {
231 /* Search for the right shader. */
232 ptr = fs->first;
233 while (ptr) {
234 if (memcmp(&ptr->compare_state, &state, sizeof(state)) == 0) {
235 fs->shader = ptr;
236 return TRUE;
237 }
238 ptr = ptr->next;
239 }
240
241 /* Not found, gotta compile a new one. */
242 ptr = CALLOC_STRUCT(r300_fragment_shader_code);
243 ptr->next = fs->first;
244 fs->first = fs->shader = ptr;
245
246 ptr->compare_state = state;
247 r300_translate_fragment_shader(r300, ptr);
248 return TRUE;
249 }
250 }
251
252 return FALSE;
253 }