fd3019b234a1263bbc63687370e6caa56126bd05
[mesa.git] / src / mesa / drivers / dri / i915 / i915_program.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <strings.h>
29
30 #include "main/glheader.h"
31 #include "main/macros.h"
32 #include "main/enums.h"
33
34 #include "tnl/t_context.h"
35 #include "intel_batchbuffer.h"
36
37 #include "i915_reg.h"
38 #include "i915_context.h"
39 #include "i915_program.h"
40
41
42 #define A0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
43 #define D0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
44 #define T0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
45 #define A0_SRC0( reg ) (((reg)&UREG_MASK)>>UREG_A0_SRC0_SHIFT_LEFT)
46 #define A1_SRC0( reg ) (((reg)&UREG_MASK)<<UREG_A1_SRC0_SHIFT_RIGHT)
47 #define A1_SRC1( reg ) (((reg)&UREG_MASK)>>UREG_A1_SRC1_SHIFT_LEFT)
48 #define A2_SRC1( reg ) (((reg)&UREG_MASK)<<UREG_A2_SRC1_SHIFT_RIGHT)
49 #define A2_SRC2( reg ) (((reg)&UREG_MASK)>>UREG_A2_SRC2_SHIFT_LEFT)
50
51 /* These are special, and don't have swizzle/negate bits.
52 */
53 #define T0_SAMPLER( reg ) (GET_UREG_NR(reg)<<T0_SAMPLER_NR_SHIFT)
54 #define T1_ADDRESS_REG( reg ) ((GET_UREG_NR(reg)<<T1_ADDRESS_REG_NR_SHIFT) | \
55 (GET_UREG_TYPE(reg)<<T1_ADDRESS_REG_TYPE_SHIFT))
56
57
58 /* Macros for translating UREG's into the various register fields used
59 * by the I915 programmable unit.
60 */
61 #define UREG_A0_DEST_SHIFT_LEFT (UREG_TYPE_SHIFT - A0_DEST_TYPE_SHIFT)
62 #define UREG_A0_SRC0_SHIFT_LEFT (UREG_TYPE_SHIFT - A0_SRC0_TYPE_SHIFT)
63 #define UREG_A1_SRC0_SHIFT_RIGHT (A1_SRC0_CHANNEL_W_SHIFT - UREG_CHANNEL_W_SHIFT)
64 #define UREG_A1_SRC1_SHIFT_LEFT (UREG_TYPE_SHIFT - A1_SRC1_TYPE_SHIFT)
65 #define UREG_A2_SRC1_SHIFT_RIGHT (A2_SRC1_CHANNEL_W_SHIFT - UREG_CHANNEL_W_SHIFT)
66 #define UREG_A2_SRC2_SHIFT_LEFT (UREG_TYPE_SHIFT - A2_SRC2_TYPE_SHIFT)
67
68 #define UREG_MASK 0xffffff00
69 #define UREG_TYPE_NR_MASK ((REG_TYPE_MASK << UREG_TYPE_SHIFT) | \
70 (REG_NR_MASK << UREG_NR_SHIFT))
71
72
73 #define I915_CONSTFLAG_PARAM 0x1f
74
75 GLuint
76 i915_get_temp(struct i915_fragment_program *p)
77 {
78 int bit = ffs(~p->temp_flag);
79 if (!bit) {
80 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
81 exit(1);
82 }
83
84 p->temp_flag |= 1 << (bit - 1);
85 return UREG(REG_TYPE_R, (bit - 1));
86 }
87
88
89 GLuint
90 i915_get_utemp(struct i915_fragment_program * p)
91 {
92 int bit = ffs(~p->utemp_flag);
93 if (!bit) {
94 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
95 exit(1);
96 }
97
98 p->utemp_flag |= 1 << (bit - 1);
99 return UREG(REG_TYPE_U, (bit - 1));
100 }
101
102 void
103 i915_release_utemps(struct i915_fragment_program *p)
104 {
105 p->utemp_flag = ~0x7;
106 }
107
108
109 GLuint
110 i915_emit_decl(struct i915_fragment_program *p,
111 GLuint type, GLuint nr, GLuint d0_flags)
112 {
113 GLuint reg = UREG(type, nr);
114
115 if (type == REG_TYPE_T) {
116 if (p->decl_t & (1 << nr))
117 return reg;
118
119 p->decl_t |= (1 << nr);
120 }
121 else if (type == REG_TYPE_S) {
122 if (p->decl_s & (1 << nr))
123 return reg;
124
125 p->decl_s |= (1 << nr);
126 }
127 else
128 return reg;
129
130 *(p->decl++) = (D0_DCL | D0_DEST(reg) | d0_flags);
131 *(p->decl++) = D1_MBZ;
132 *(p->decl++) = D2_MBZ;
133
134 p->nr_decl_insn++;
135 return reg;
136 }
137
138 GLuint
139 i915_emit_arith(struct i915_fragment_program * p,
140 GLuint op,
141 GLuint dest,
142 GLuint mask,
143 GLuint saturate, GLuint src0, GLuint src1, GLuint src2)
144 {
145 GLuint c[3];
146 GLuint nr_const = 0;
147
148 assert(GET_UREG_TYPE(dest) != REG_TYPE_CONST);
149 dest = UREG(GET_UREG_TYPE(dest), GET_UREG_NR(dest));
150 assert(dest);
151
152 if (GET_UREG_TYPE(src0) == REG_TYPE_CONST)
153 c[nr_const++] = 0;
154 if (GET_UREG_TYPE(src1) == REG_TYPE_CONST)
155 c[nr_const++] = 1;
156 if (GET_UREG_TYPE(src2) == REG_TYPE_CONST)
157 c[nr_const++] = 2;
158
159 /* Recursively call this function to MOV additional const values
160 * into temporary registers. Use utemp registers for this -
161 * currently shouldn't be possible to run out, but keep an eye on
162 * this.
163 */
164 if (nr_const > 1) {
165 GLuint s[3], first, i, old_utemp_flag;
166
167 s[0] = src0;
168 s[1] = src1;
169 s[2] = src2;
170 old_utemp_flag = p->utemp_flag;
171
172 first = GET_UREG_NR(s[c[0]]);
173 for (i = 1; i < nr_const; i++) {
174 if (GET_UREG_NR(s[c[i]]) != first) {
175 GLuint tmp = i915_get_utemp(p);
176
177 i915_emit_arith(p, A0_MOV, tmp, A0_DEST_CHANNEL_ALL, 0,
178 s[c[i]], 0, 0);
179 s[c[i]] = tmp;
180 }
181 }
182
183 src0 = s[0];
184 src1 = s[1];
185 src2 = s[2];
186 p->utemp_flag = old_utemp_flag; /* restore */
187 }
188
189 if (p->csr >= p->program + I915_PROGRAM_SIZE) {
190 i915_program_error(p, "Program contains too many instructions");
191 return UREG_BAD;
192 }
193
194 *(p->csr++) = (op | A0_DEST(dest) | mask | saturate | A0_SRC0(src0));
195 *(p->csr++) = (A1_SRC0(src0) | A1_SRC1(src1));
196 *(p->csr++) = (A2_SRC1(src1) | A2_SRC2(src2));
197
198 if (GET_UREG_TYPE(dest) == REG_TYPE_R)
199 p->register_phases[GET_UREG_NR(dest)] = p->nr_tex_indirect;
200
201 p->nr_alu_insn++;
202 return dest;
203 }
204
205 static GLuint get_free_rreg (struct i915_fragment_program *p,
206 GLuint live_regs)
207 {
208 int bit = ffs(~live_regs);
209 if (!bit) {
210 i915_program_error(p, "Can't find free R reg");
211 return UREG_BAD;
212 }
213 return UREG(REG_TYPE_R, bit - 1);
214 }
215
216 GLuint i915_emit_texld( struct i915_fragment_program *p,
217 GLuint live_regs,
218 GLuint dest,
219 GLuint destmask,
220 GLuint sampler,
221 GLuint coord,
222 GLuint op )
223 {
224 if (coord != UREG(GET_UREG_TYPE(coord), GET_UREG_NR(coord))) {
225 /* With the help of the "needed registers" table created earlier, pick
226 * a register we can MOV the swizzled TC to (since TEX doesn't support
227 * swizzled sources) */
228 GLuint swizCoord = get_free_rreg(p, live_regs);
229 if (swizCoord == UREG_BAD)
230 return 0;
231
232 i915_emit_arith( p, A0_MOV, swizCoord, A0_DEST_CHANNEL_ALL, 0, coord, 0, 0 );
233 coord = swizCoord;
234 }
235
236 /* Don't worry about saturate as we only support texture formats
237 * that are always in the 0..1 range.
238 */
239 if (destmask != A0_DEST_CHANNEL_ALL) {
240 GLuint tmp = i915_get_utemp(p);
241 i915_emit_texld( p, 0, tmp, A0_DEST_CHANNEL_ALL, sampler, coord, op );
242 i915_emit_arith( p, A0_MOV, dest, destmask, 0, tmp, 0, 0 );
243 return dest;
244 }
245 else {
246 assert(GET_UREG_TYPE(dest) != REG_TYPE_CONST);
247 assert(dest = UREG(GET_UREG_TYPE(dest), GET_UREG_NR(dest)));
248 /* Can't use unsaved temps for coords, as the phase boundary would result
249 * in the contents becoming undefined.
250 */
251 assert(GET_UREG_TYPE(coord) != REG_TYPE_U);
252
253 if ((GET_UREG_TYPE(coord) != REG_TYPE_R) &&
254 (GET_UREG_TYPE(coord) != REG_TYPE_OC) &&
255 (GET_UREG_TYPE(coord) != REG_TYPE_OD) &&
256 (GET_UREG_TYPE(coord) != REG_TYPE_T)) {
257 GLuint tmpCoord = get_free_rreg(p, live_regs);
258
259 if (tmpCoord == UREG_BAD)
260 return 0;
261
262 i915_emit_arith(p, A0_MOV, tmpCoord, A0_DEST_CHANNEL_ALL, 0, coord, 0, 0);
263 coord = tmpCoord;
264 }
265
266 /* Output register being oC or oD defines a phase boundary */
267 if (GET_UREG_TYPE(dest) == REG_TYPE_OC ||
268 GET_UREG_TYPE(dest) == REG_TYPE_OD)
269 p->nr_tex_indirect++;
270
271 /* Reading from an r# register whose contents depend on output of the
272 * current phase defines a phase boundary.
273 */
274 if (GET_UREG_TYPE(coord) == REG_TYPE_R &&
275 p->register_phases[GET_UREG_NR(coord)] == p->nr_tex_indirect)
276 p->nr_tex_indirect++;
277
278 if (p->csr >= p->program + I915_PROGRAM_SIZE) {
279 i915_program_error(p, "Program contains too many instructions");
280 return UREG_BAD;
281 }
282
283 *(p->csr++) = (op |
284 T0_DEST( dest ) |
285 T0_SAMPLER( sampler ));
286
287 *(p->csr++) = T1_ADDRESS_REG( coord );
288 *(p->csr++) = T2_MBZ;
289
290 if (GET_UREG_TYPE(dest) == REG_TYPE_R)
291 p->register_phases[GET_UREG_NR(dest)] = p->nr_tex_indirect;
292
293 p->nr_tex_insn++;
294 return dest;
295 }
296 }
297
298
299 GLuint
300 i915_emit_const1f(struct i915_fragment_program * p, GLfloat c0)
301 {
302 GLint reg, idx;
303
304 if (c0 == 0.0)
305 return swizzle(UREG(REG_TYPE_R, 0), ZERO, ZERO, ZERO, ZERO);
306 if (c0 == 1.0)
307 return swizzle(UREG(REG_TYPE_R, 0), ONE, ONE, ONE, ONE);
308
309 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
310 if (p->constant_flags[reg] == I915_CONSTFLAG_PARAM)
311 continue;
312 for (idx = 0; idx < 4; idx++) {
313 if (!(p->constant_flags[reg] & (1 << idx)) ||
314 p->constant[reg][idx] == c0) {
315 p->constant[reg][idx] = c0;
316 p->constant_flags[reg] |= 1 << idx;
317 if (reg + 1 > p->nr_constants)
318 p->nr_constants = reg + 1;
319 return swizzle(UREG(REG_TYPE_CONST, reg), idx, ZERO, ZERO, ONE);
320 }
321 }
322 }
323
324 fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
325 p->error = 1;
326 return 0;
327 }
328
329 GLuint
330 i915_emit_const2f(struct i915_fragment_program * p, GLfloat c0, GLfloat c1)
331 {
332 GLint reg, idx;
333
334 if (c0 == 0.0)
335 return swizzle(i915_emit_const1f(p, c1), ZERO, X, Z, W);
336 if (c0 == 1.0)
337 return swizzle(i915_emit_const1f(p, c1), ONE, X, Z, W);
338
339 if (c1 == 0.0)
340 return swizzle(i915_emit_const1f(p, c0), X, ZERO, Z, W);
341 if (c1 == 1.0)
342 return swizzle(i915_emit_const1f(p, c0), X, ONE, Z, W);
343
344 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
345 if (p->constant_flags[reg] == 0xf ||
346 p->constant_flags[reg] == I915_CONSTFLAG_PARAM)
347 continue;
348 for (idx = 0; idx < 3; idx++) {
349 if (!(p->constant_flags[reg] & (3 << idx))) {
350 p->constant[reg][idx] = c0;
351 p->constant[reg][idx + 1] = c1;
352 p->constant_flags[reg] |= 3 << idx;
353 if (reg + 1 > p->nr_constants)
354 p->nr_constants = reg + 1;
355 return swizzle(UREG(REG_TYPE_CONST, reg), idx, idx + 1, ZERO,
356 ONE);
357 }
358 }
359 }
360
361 fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
362 p->error = 1;
363 return 0;
364 }
365
366
367
368 GLuint
369 i915_emit_const4f(struct i915_fragment_program * p,
370 GLfloat c0, GLfloat c1, GLfloat c2, GLfloat c3)
371 {
372 GLint reg;
373
374 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
375 if (p->constant_flags[reg] == 0xf &&
376 p->constant[reg][0] == c0 &&
377 p->constant[reg][1] == c1 &&
378 p->constant[reg][2] == c2 && p->constant[reg][3] == c3) {
379 return UREG(REG_TYPE_CONST, reg);
380 }
381 else if (p->constant_flags[reg] == 0) {
382 p->constant[reg][0] = c0;
383 p->constant[reg][1] = c1;
384 p->constant[reg][2] = c2;
385 p->constant[reg][3] = c3;
386 p->constant_flags[reg] = 0xf;
387 if (reg + 1 > p->nr_constants)
388 p->nr_constants = reg + 1;
389 return UREG(REG_TYPE_CONST, reg);
390 }
391 }
392
393 fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
394 p->error = 1;
395 return 0;
396 }
397
398
399 GLuint
400 i915_emit_const4fv(struct i915_fragment_program * p, const GLfloat * c)
401 {
402 return i915_emit_const4f(p, c[0], c[1], c[2], c[3]);
403 }
404
405
406 GLuint
407 i915_emit_param4fv(struct i915_fragment_program * p, const GLfloat * values)
408 {
409 GLint reg, i;
410
411 for (i = 0; i < p->nr_params; i++) {
412 if (p->param[i].values == values)
413 return UREG(REG_TYPE_CONST, p->param[i].reg);
414 }
415
416
417 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
418 if (p->constant_flags[reg] == 0) {
419 p->constant_flags[reg] = I915_CONSTFLAG_PARAM;
420 i = p->nr_params++;
421
422 p->param[i].values = values;
423 p->param[i].reg = reg;
424 p->params_uptodate = 0;
425
426 if (reg + 1 > p->nr_constants)
427 p->nr_constants = reg + 1;
428 return UREG(REG_TYPE_CONST, reg);
429 }
430 }
431
432 fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
433 p->error = 1;
434 return 0;
435 }
436
437 /* Warning the user about program errors seems to be quite valuable, from
438 * our bug reports. It unfortunately means piglit reporting errors
439 * when we fall back to software due to an unsupportable program, though.
440 */
441 void
442 i915_program_error(struct i915_fragment_program *p, const char *fmt, ...)
443 {
444 va_list args;
445
446 fprintf(stderr, "i915_program_error: ");
447 va_start(args, fmt);
448 vfprintf(stderr, fmt, args);
449 va_end(args);
450
451 fprintf(stderr, "\n");
452 p->error = 1;
453 }
454
455
456 void
457 i915_init_program(struct i915_context *i915, struct i915_fragment_program *p)
458 {
459 GLcontext *ctx = &i915->intel.ctx;
460
461 p->translated = 0;
462 p->params_uptodate = 0;
463 p->on_hardware = 0;
464 p->error = 0;
465
466 memset(&p->register_phases, 0, sizeof(p->register_phases));
467 p->nr_tex_indirect = 1;
468 p->nr_tex_insn = 0;
469 p->nr_alu_insn = 0;
470 p->nr_decl_insn = 0;
471
472 p->ctx = ctx;
473 memset(p->constant_flags, 0, sizeof(p->constant_flags));
474
475 p->nr_constants = 0;
476 p->csr = p->program;
477 p->decl = p->declarations;
478 p->decl_s = 0;
479 p->decl_t = 0;
480 p->temp_flag = 0xffff000;
481 p->utemp_flag = ~0x7;
482 p->wpos_tex = -1;
483 p->depth_written = 0;
484 p->nr_params = 0;
485
486 *(p->decl++) = _3DSTATE_PIXEL_SHADER_PROGRAM;
487 }
488
489
490 void
491 i915_fini_program(struct i915_fragment_program *p)
492 {
493 GLuint program_size = p->csr - p->program;
494 GLuint decl_size = p->decl - p->declarations;
495
496 if (p->nr_tex_indirect > I915_MAX_TEX_INDIRECT)
497 i915_program_error(p, "Exceeded max nr indirect texture lookups");
498
499 if (p->nr_tex_insn > I915_MAX_TEX_INSN)
500 i915_program_error(p, "Exceeded max TEX instructions");
501
502 if (p->nr_alu_insn > I915_MAX_ALU_INSN)
503 i915_program_error(p, "Exceeded max ALU instructions");
504
505 if (p->nr_decl_insn > I915_MAX_DECL_INSN)
506 i915_program_error(p, "Exceeded max DECL instructions");
507
508 if (p->error) {
509 p->FragProg.Base.NumNativeInstructions = 0;
510 p->FragProg.Base.NumNativeAluInstructions = 0;
511 p->FragProg.Base.NumNativeTexInstructions = 0;
512 p->FragProg.Base.NumNativeTexIndirections = 0;
513 }
514 else {
515 p->FragProg.Base.NumNativeInstructions = (p->nr_alu_insn +
516 p->nr_tex_insn +
517 p->nr_decl_insn);
518 p->FragProg.Base.NumNativeAluInstructions = p->nr_alu_insn;
519 p->FragProg.Base.NumNativeTexInstructions = p->nr_tex_insn;
520 p->FragProg.Base.NumNativeTexIndirections = p->nr_tex_indirect;
521 }
522
523 p->declarations[0] |= program_size + decl_size - 2;
524 }
525
526 void
527 i915_upload_program(struct i915_context *i915,
528 struct i915_fragment_program *p)
529 {
530 GLuint program_size = p->csr - p->program;
531 GLuint decl_size = p->decl - p->declarations;
532
533 /* Could just go straight to the batchbuffer from here:
534 */
535 if (i915->state.ProgramSize != (program_size + decl_size) ||
536 memcmp(i915->state.Program + decl_size, p->program,
537 program_size * sizeof(int)) != 0) {
538 I915_STATECHANGE(i915, I915_UPLOAD_PROGRAM);
539 memcpy(i915->state.Program, p->declarations, decl_size * sizeof(int));
540 memcpy(i915->state.Program + decl_size, p->program,
541 program_size * sizeof(int));
542 i915->state.ProgramSize = decl_size + program_size;
543 }
544
545 /* Always seemed to get a failure if I used memcmp() to
546 * shortcircuit this state upload. Needs further investigation?
547 */
548 if (p->nr_constants) {
549 GLuint nr = p->nr_constants;
550
551 I915_ACTIVESTATE(i915, I915_UPLOAD_CONSTANTS, 1);
552 I915_STATECHANGE(i915, I915_UPLOAD_CONSTANTS);
553
554 i915->state.Constant[0] = _3DSTATE_PIXEL_SHADER_CONSTANTS | ((nr) * 4);
555 i915->state.Constant[1] = (1 << (nr - 1)) | ((1 << (nr - 1)) - 1);
556
557 memcpy(&i915->state.Constant[2], p->constant, 4 * sizeof(int) * (nr));
558 i915->state.ConstantSize = 2 + (nr) * 4;
559
560 if (0) {
561 GLuint i;
562 for (i = 0; i < nr; i++) {
563 fprintf(stderr, "const[%d]: %f %f %f %f\n", i,
564 p->constant[i][0],
565 p->constant[i][1], p->constant[i][2], p->constant[i][3]);
566 }
567 }
568 }
569 else {
570 I915_ACTIVESTATE(i915, I915_UPLOAD_CONSTANTS, 0);
571 }
572
573 p->on_hardware = 1;
574 }