r300g: extend compile error message
[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_context.h"
32 #include "r300_screen.h"
33 #include "r300_fs.h"
34 #include "r300_reg.h"
35 #include "r300_tgsi_to_rc.h"
36
37 #include "radeon_code.h"
38 #include "radeon_compiler.h"
39
40 /* Convert info about FS input semantics to r300_shader_semantics. */
41 void r300_shader_read_fs_inputs(struct tgsi_shader_info* info,
42 struct r300_shader_semantics* fs_inputs)
43 {
44 int i;
45 unsigned index;
46
47 r300_shader_semantics_reset(fs_inputs);
48
49 for (i = 0; i < info->num_inputs; i++) {
50 index = info->input_semantic_index[i];
51
52 switch (info->input_semantic_name[i]) {
53 case TGSI_SEMANTIC_COLOR:
54 assert(index < ATTR_COLOR_COUNT);
55 fs_inputs->color[index] = i;
56 break;
57
58 case TGSI_SEMANTIC_GENERIC:
59 assert(index < ATTR_GENERIC_COUNT);
60 fs_inputs->generic[index] = i;
61 break;
62
63 case TGSI_SEMANTIC_FOG:
64 assert(index == 0);
65 fs_inputs->fog = i;
66 break;
67
68 case TGSI_SEMANTIC_POSITION:
69 assert(index == 0);
70 fs_inputs->wpos = i;
71 break;
72
73 default:
74 fprintf(stderr, "r300: FP: Unknown input semantic: %i\n",
75 info->input_semantic_name[i]);
76 }
77 }
78 }
79
80 static void find_output_registers(struct r300_fragment_program_compiler * compiler,
81 struct r300_fragment_shader_code *shader)
82 {
83 unsigned i, colorbuf_count = 0;
84
85 /* Mark the outputs as not present initially */
86 compiler->OutputColor[0] = shader->info.num_outputs;
87 compiler->OutputColor[1] = shader->info.num_outputs;
88 compiler->OutputColor[2] = shader->info.num_outputs;
89 compiler->OutputColor[3] = shader->info.num_outputs;
90 compiler->OutputDepth = shader->info.num_outputs;
91
92 /* Now see where they really are. */
93 for(i = 0; i < shader->info.num_outputs; ++i) {
94 switch(shader->info.output_semantic_name[i]) {
95 case TGSI_SEMANTIC_COLOR:
96 compiler->OutputColor[colorbuf_count] = i;
97 colorbuf_count++;
98 break;
99 case TGSI_SEMANTIC_POSITION:
100 compiler->OutputDepth = i;
101 break;
102 }
103 }
104 }
105
106 static void allocate_hardware_inputs(
107 struct r300_fragment_program_compiler * c,
108 void (*allocate)(void * data, unsigned input, unsigned hwreg),
109 void * mydata)
110 {
111 struct r300_shader_semantics* inputs =
112 (struct r300_shader_semantics*)c->UserData;
113 int i, reg = 0;
114
115 /* Allocate input registers. */
116 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
117 if (inputs->color[i] != ATTR_UNUSED) {
118 allocate(mydata, inputs->color[i], reg++);
119 }
120 }
121 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
122 if (inputs->generic[i] != ATTR_UNUSED) {
123 allocate(mydata, inputs->generic[i], reg++);
124 }
125 }
126 if (inputs->fog != ATTR_UNUSED) {
127 allocate(mydata, inputs->fog, reg++);
128 }
129 if (inputs->wpos != ATTR_UNUSED) {
130 allocate(mydata, inputs->wpos, reg++);
131 }
132 }
133
134 static void get_external_state(
135 struct r300_context* r300,
136 struct r300_fragment_program_external_state* state)
137 {
138 struct r300_textures_state *texstate = r300->textures_state.state;
139 unsigned i;
140 unsigned char *swizzle;
141
142 for (i = 0; i < texstate->sampler_state_count; i++) {
143 struct r300_sampler_state* s = texstate->sampler_states[i];
144
145 if (!s) {
146 continue;
147 }
148
149 if (s->state.compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
150 state->unit[i].compare_mode_enabled = 1;
151
152 /* Pass depth texture swizzling to the compiler. */
153 if (texstate->sampler_views[i]) {
154 swizzle = texstate->sampler_views[i]->swizzle;
155
156 state->unit[i].depth_texture_swizzle =
157 RC_MAKE_SWIZZLE(swizzle[0], swizzle[1],
158 swizzle[2], swizzle[3]);
159 } else {
160 state->unit[i].depth_texture_swizzle = RC_SWIZZLE_XYZW;
161 }
162
163 /* Fortunately, no need to translate this. */
164 state->unit[i].texture_compare_func = s->state.compare_func;
165 }
166
167 state->unit[i].non_normalized_coords = !s->state.normalized_coords;
168
169 if (texstate->sampler_views[i]) {
170 struct r300_texture *t;
171 t = (struct r300_texture*)texstate->sampler_views[i]->base.texture;
172
173 /* XXX this should probably take into account STR, not just S. */
174 if (t->uses_pitch) {
175 switch (s->state.wrap_s) {
176 case PIPE_TEX_WRAP_REPEAT:
177 state->unit[i].wrap_mode = RC_WRAP_REPEAT;
178 state->unit[i].fake_npot = TRUE;
179 break;
180
181 case PIPE_TEX_WRAP_MIRROR_REPEAT:
182 state->unit[i].wrap_mode = RC_WRAP_MIRRORED_REPEAT;
183 state->unit[i].fake_npot = TRUE;
184 break;
185
186 case PIPE_TEX_WRAP_MIRROR_CLAMP:
187 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
188 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
189 state->unit[i].wrap_mode = RC_WRAP_MIRRORED_CLAMP;
190 state->unit[i].fake_npot = TRUE;
191 break;
192
193 default:
194 state->unit[i].wrap_mode = RC_WRAP_NONE;
195 break;
196 }
197 }
198 }
199 }
200 }
201
202 static void r300_translate_fragment_shader(
203 struct r300_context* r300,
204 struct r300_fragment_shader_code* shader,
205 const struct tgsi_token *tokens);
206
207 static void r300_dummy_fragment_shader(
208 struct r300_context* r300,
209 struct r300_fragment_shader_code* shader)
210 {
211 struct pipe_shader_state state;
212 struct ureg_program *ureg;
213 struct ureg_dst out;
214 struct ureg_src imm;
215
216 /* Make a simple fragment shader which outputs (0, 0, 0, 1) */
217 ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT);
218 out = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
219 imm = ureg_imm4f(ureg, 0, 0, 0, 1);
220
221 ureg_MOV(ureg, out, imm);
222 ureg_END(ureg);
223
224 state.tokens = ureg_finalize(ureg);
225
226 shader->dummy = TRUE;
227 r300_translate_fragment_shader(r300, shader, state.tokens);
228
229 ureg_destroy(ureg);
230 }
231
232 static void r300_translate_fragment_shader(
233 struct r300_context* r300,
234 struct r300_fragment_shader_code* shader,
235 const struct tgsi_token *tokens)
236 {
237 struct r300_fragment_program_compiler compiler;
238 struct tgsi_to_rc ttr;
239 int wpos;
240 unsigned i;
241
242 tgsi_scan_shader(tokens, &shader->info);
243 r300_shader_read_fs_inputs(&shader->info, &shader->inputs);
244
245 wpos = shader->inputs.wpos;
246
247 /* Setup the compiler. */
248 memset(&compiler, 0, sizeof(compiler));
249 rc_init(&compiler.Base);
250 compiler.Base.Debug = DBG_ON(r300, DBG_FP);
251
252 compiler.code = &shader->code;
253 compiler.state = shader->compare_state;
254 compiler.is_r500 = r300->screen->caps.is_r500;
255 compiler.max_temp_regs = compiler.is_r500 ? 128 : 32;
256 compiler.AllocateHwInputs = &allocate_hardware_inputs;
257 compiler.UserData = &shader->inputs;
258
259 find_output_registers(&compiler, shader);
260
261 if (compiler.Base.Debug) {
262 debug_printf("r300: Initial fragment program\n");
263 tgsi_dump(tokens, 0);
264 }
265
266 /* Translate TGSI to our internal representation */
267 ttr.compiler = &compiler.Base;
268 ttr.info = &shader->info;
269 ttr.use_half_swizzles = TRUE;
270
271 r300_tgsi_to_rc(&ttr, tokens);
272
273 /**
274 * Transform the program to support WPOS.
275 *
276 * Introduce a small fragment at the start of the program that will be
277 * the only code that directly reads the WPOS input.
278 * All other code pieces that reference that input will be rewritten
279 * to read from a newly allocated temporary. */
280 if (wpos != ATTR_UNUSED) {
281 /* Moving the input to some other reg is not really necessary. */
282 rc_transform_fragment_wpos(&compiler.Base, wpos, wpos, TRUE);
283 }
284
285 /* Invoke the compiler */
286 r3xx_compile_fragment_program(&compiler);
287
288 /* Shaders with zero instructions are invalid,
289 * use the dummy shader instead. */
290 if (shader->code.code.r500.inst_end == -1) {
291 rc_destroy(&compiler.Base);
292 r300_dummy_fragment_shader(r300, shader);
293 return;
294 }
295
296 if (compiler.Base.Error) {
297 fprintf(stderr, "r300 FP: Compiler Error:\n%sUsing a dummy shader"
298 " instead.\nIf there's an 'unknown opcode' message, please"
299 " file a bug report and attach this log.\n", compiler.Base.ErrorMsg);
300
301 if (shader->dummy) {
302 fprintf(stderr, "r300 FP: Cannot compile the dummy shader! "
303 "Giving up...\n");
304 abort();
305 }
306
307 rc_destroy(&compiler.Base);
308 r300_dummy_fragment_shader(r300, shader);
309 return;
310 }
311
312 /* Initialize numbers of constants for each type. */
313 shader->externals_count = ttr.immediate_offset;
314 shader->immediates_count = 0;
315 shader->rc_state_count = 0;
316
317 for (i = shader->externals_count; i < shader->code.constants.Count; i++) {
318 switch (shader->code.constants.Constants[i].Type) {
319 case RC_CONSTANT_IMMEDIATE:
320 ++shader->immediates_count;
321 break;
322 case RC_CONSTANT_STATE:
323 ++shader->rc_state_count;
324 break;
325 default:
326 assert(0);
327 }
328 }
329
330 /* Setup shader depth output. */
331 if (shader->code.writes_depth) {
332 shader->fg_depth_src = R300_FG_DEPTH_SRC_SHADER;
333 shader->us_out_w = R300_W_FMT_W24 | R300_W_SRC_US;
334 } else {
335 shader->fg_depth_src = R300_FG_DEPTH_SRC_SCAN;
336 shader->us_out_w = R300_W_FMT_W0 | R300_W_SRC_US;
337 }
338
339 /* And, finally... */
340 rc_destroy(&compiler.Base);
341 }
342
343 boolean r300_pick_fragment_shader(struct r300_context* r300)
344 {
345 struct r300_fragment_shader* fs = r300_fs(r300);
346 struct r300_fragment_program_external_state state = {{{ 0 }}};
347 struct r300_fragment_shader_code* ptr;
348
349 get_external_state(r300, &state);
350
351 if (!fs->first) {
352 /* Build the fragment shader for the first time. */
353 fs->first = fs->shader = CALLOC_STRUCT(r300_fragment_shader_code);
354
355 memcpy(&fs->shader->compare_state, &state,
356 sizeof(struct r300_fragment_program_external_state));
357 r300_translate_fragment_shader(r300, fs->shader, fs->state.tokens);
358 return TRUE;
359
360 } else {
361 /* Check if the currently-bound shader has been compiled
362 * with the texture-compare state we need. */
363 if (memcmp(&fs->shader->compare_state, &state, sizeof(state)) != 0) {
364 /* Search for the right shader. */
365 ptr = fs->first;
366 while (ptr) {
367 if (memcmp(&ptr->compare_state, &state, sizeof(state)) == 0) {
368 if (fs->shader != ptr) {
369 fs->shader = ptr;
370 return TRUE;
371 }
372 /* The currently-bound one is OK. */
373 return FALSE;
374 }
375 ptr = ptr->next;
376 }
377
378 /* Not found, gotta compile a new one. */
379 ptr = CALLOC_STRUCT(r300_fragment_shader_code);
380 ptr->next = fs->first;
381 fs->first = fs->shader = ptr;
382
383 ptr->compare_state = state;
384 r300_translate_fragment_shader(r300, ptr, fs->state.tokens);
385 return TRUE;
386 }
387 }
388
389 return FALSE;
390 }