merge current trunk into vbo branch
[mesa.git] / src / mesa / drivers / dri / i915tex / 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 "glheader.h"
31 #include "macros.h"
32 #include "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 *(p->csr++) = (op | A0_DEST(dest) | mask | saturate | A0_SRC0(src0));
190 *(p->csr++) = (A1_SRC0(src0) | A1_SRC1(src1));
191 *(p->csr++) = (A2_SRC1(src1) | A2_SRC2(src2));
192
193 p->nr_alu_insn++;
194 return dest;
195 }
196
197 GLuint i915_emit_texld( struct i915_fragment_program *p,
198 GLuint dest,
199 GLuint destmask,
200 GLuint sampler,
201 GLuint coord,
202 GLuint op )
203 {
204 if (coord != UREG(GET_UREG_TYPE(coord), GET_UREG_NR(coord))) {
205 /* No real way to work around this in the general case - need to
206 * allocate and declare a new temporary register (a utemp won't
207 * do). Will fallback for now.
208 */
209 i915_program_error(p, "Can't (yet) swizzle TEX arguments");
210 return 0;
211 }
212
213 /* Don't worry about saturate as we only support
214 */
215 if (destmask != A0_DEST_CHANNEL_ALL) {
216 GLuint tmp = i915_get_utemp(p);
217 i915_emit_texld( p, tmp, A0_DEST_CHANNEL_ALL, sampler, coord, op );
218 i915_emit_arith( p, A0_MOV, dest, destmask, 0, tmp, 0, 0 );
219 return dest;
220 }
221 else {
222 assert(GET_UREG_TYPE(dest) != REG_TYPE_CONST);
223 assert(dest = UREG(GET_UREG_TYPE(dest), GET_UREG_NR(dest)));
224
225 if (GET_UREG_TYPE(coord) != REG_TYPE_T) {
226 p->nr_tex_indirect++;
227 }
228
229 *(p->csr++) = (op |
230 T0_DEST( dest ) |
231 T0_SAMPLER( sampler ));
232
233 *(p->csr++) = T1_ADDRESS_REG( coord );
234 *(p->csr++) = T2_MBZ;
235
236 p->nr_tex_insn++;
237 return dest;
238 }
239 }
240
241
242 GLuint
243 i915_emit_const1f(struct i915_fragment_program * p, GLfloat c0)
244 {
245 GLint reg, idx;
246
247 if (c0 == 0.0)
248 return swizzle(UREG(REG_TYPE_R, 0), ZERO, ZERO, ZERO, ZERO);
249 if (c0 == 1.0)
250 return swizzle(UREG(REG_TYPE_R, 0), ONE, ONE, ONE, ONE);
251
252 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
253 if (p->constant_flags[reg] == I915_CONSTFLAG_PARAM)
254 continue;
255 for (idx = 0; idx < 4; idx++) {
256 if (!(p->constant_flags[reg] & (1 << idx)) ||
257 p->constant[reg][idx] == c0) {
258 p->constant[reg][idx] = c0;
259 p->constant_flags[reg] |= 1 << idx;
260 if (reg + 1 > p->nr_constants)
261 p->nr_constants = reg + 1;
262 return swizzle(UREG(REG_TYPE_CONST, reg), idx, ZERO, ZERO, ONE);
263 }
264 }
265 }
266
267 fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
268 p->error = 1;
269 return 0;
270 }
271
272 GLuint
273 i915_emit_const2f(struct i915_fragment_program * p, GLfloat c0, GLfloat c1)
274 {
275 GLint reg, idx;
276
277 if (c0 == 0.0)
278 return swizzle(i915_emit_const1f(p, c1), ZERO, X, Z, W);
279 if (c0 == 1.0)
280 return swizzle(i915_emit_const1f(p, c1), ONE, X, Z, W);
281
282 if (c1 == 0.0)
283 return swizzle(i915_emit_const1f(p, c0), X, ZERO, Z, W);
284 if (c1 == 1.0)
285 return swizzle(i915_emit_const1f(p, c0), X, ONE, Z, W);
286
287 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
288 if (p->constant_flags[reg] == 0xf ||
289 p->constant_flags[reg] == I915_CONSTFLAG_PARAM)
290 continue;
291 for (idx = 0; idx < 3; idx++) {
292 if (!(p->constant_flags[reg] & (3 << idx))) {
293 p->constant[reg][idx] = c0;
294 p->constant[reg][idx + 1] = c1;
295 p->constant_flags[reg] |= 3 << idx;
296 if (reg + 1 > p->nr_constants)
297 p->nr_constants = reg + 1;
298 return swizzle(UREG(REG_TYPE_CONST, reg), idx, idx + 1, ZERO,
299 ONE);
300 }
301 }
302 }
303
304 fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
305 p->error = 1;
306 return 0;
307 }
308
309
310
311 GLuint
312 i915_emit_const4f(struct i915_fragment_program * p,
313 GLfloat c0, GLfloat c1, GLfloat c2, GLfloat c3)
314 {
315 GLint reg;
316
317 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
318 if (p->constant_flags[reg] == 0xf &&
319 p->constant[reg][0] == c0 &&
320 p->constant[reg][1] == c1 &&
321 p->constant[reg][2] == c2 && p->constant[reg][3] == c3) {
322 return UREG(REG_TYPE_CONST, reg);
323 }
324 else if (p->constant_flags[reg] == 0) {
325 p->constant[reg][0] = c0;
326 p->constant[reg][1] = c1;
327 p->constant[reg][2] = c2;
328 p->constant[reg][3] = c3;
329 p->constant_flags[reg] = 0xf;
330 if (reg + 1 > p->nr_constants)
331 p->nr_constants = reg + 1;
332 return UREG(REG_TYPE_CONST, reg);
333 }
334 }
335
336 fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
337 p->error = 1;
338 return 0;
339 }
340
341
342 GLuint
343 i915_emit_const4fv(struct i915_fragment_program * p, const GLfloat * c)
344 {
345 return i915_emit_const4f(p, c[0], c[1], c[2], c[3]);
346 }
347
348
349 GLuint
350 i915_emit_param4fv(struct i915_fragment_program * p, const GLfloat * values)
351 {
352 GLint reg, i;
353
354 for (i = 0; i < p->nr_params; i++) {
355 if (p->param[i].values == values)
356 return UREG(REG_TYPE_CONST, p->param[i].reg);
357 }
358
359
360 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
361 if (p->constant_flags[reg] == 0) {
362 p->constant_flags[reg] = I915_CONSTFLAG_PARAM;
363 i = p->nr_params++;
364
365 p->param[i].values = values;
366 p->param[i].reg = reg;
367 p->params_uptodate = 0;
368
369 if (reg + 1 > p->nr_constants)
370 p->nr_constants = reg + 1;
371 return UREG(REG_TYPE_CONST, reg);
372 }
373 }
374
375 fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
376 p->error = 1;
377 return 0;
378 }
379
380
381
382
383 void
384 i915_program_error(struct i915_fragment_program *p, const char *msg)
385 {
386 /* XXX we shouldn't print anything to stdout, record GL error or
387 * call _mesa_problem()
388 */
389 fprintf(stderr, "%s\n", msg);
390 p->error = 1;
391 }
392
393 void
394 i915_init_program(struct i915_context *i915, struct i915_fragment_program *p)
395 {
396 GLcontext *ctx = &i915->intel.ctx;
397 TNLcontext *tnl = TNL_CONTEXT(ctx);
398
399 p->translated = 0;
400 p->params_uptodate = 0;
401 p->on_hardware = 0;
402 p->error = 0;
403
404 p->nr_tex_indirect = 1; /* correct? */
405 p->nr_tex_insn = 0;
406 p->nr_alu_insn = 0;
407 p->nr_decl_insn = 0;
408
409 p->ctx = ctx;
410 memset(p->constant_flags, 0, sizeof(p->constant_flags));
411
412 p->nr_constants = 0;
413 p->csr = p->program;
414 p->decl = p->declarations;
415 p->decl_s = 0;
416 p->decl_t = 0;
417 p->temp_flag = 0xffff000;
418 p->utemp_flag = ~0x7;
419 p->wpos_tex = -1;
420 p->depth_written = 0;
421 p->nr_params = 0;
422
423 p->src_texture = UREG_BAD;
424 p->src_previous = UREG(REG_TYPE_T, T_DIFFUSE);
425 p->last_tex_stage = 0;
426 p->VB = &tnl->vb;
427
428 *(p->decl++) = _3DSTATE_PIXEL_SHADER_PROGRAM;
429 }
430
431
432 void
433 i915_fini_program(struct i915_fragment_program *p)
434 {
435 GLuint program_size = p->csr - p->program;
436 GLuint decl_size = p->decl - p->declarations;
437
438 if (p->nr_tex_indirect > I915_MAX_TEX_INDIRECT)
439 i915_program_error(p, "Exceeded max nr indirect texture lookups");
440
441 if (p->nr_tex_insn > I915_MAX_TEX_INSN)
442 i915_program_error(p, "Exceeded max TEX instructions");
443
444 if (p->nr_alu_insn > I915_MAX_ALU_INSN)
445 i915_program_error(p, "Exceeded max ALU instructions");
446
447 if (p->nr_decl_insn > I915_MAX_DECL_INSN)
448 i915_program_error(p, "Exceeded max DECL instructions");
449
450 if (p->error) {
451 p->FragProg.Base.NumNativeInstructions = 0;
452 p->FragProg.NumNativeAluInstructions = 0;
453 p->FragProg.NumNativeTexInstructions = 0;
454 p->FragProg.NumNativeTexIndirections = 0;
455 }
456 else {
457 p->FragProg.Base.NumNativeInstructions = (p->nr_alu_insn +
458 p->nr_tex_insn +
459 p->nr_decl_insn);
460 p->FragProg.NumNativeAluInstructions = p->nr_alu_insn;
461 p->FragProg.NumNativeTexInstructions = p->nr_tex_insn;
462 p->FragProg.NumNativeTexIndirections = p->nr_tex_indirect;
463 }
464
465 p->declarations[0] |= program_size + decl_size - 2;
466 }
467
468 void
469 i915_upload_program(struct i915_context *i915,
470 struct i915_fragment_program *p)
471 {
472 GLuint program_size = p->csr - p->program;
473 GLuint decl_size = p->decl - p->declarations;
474
475 FALLBACK(&i915->intel, I915_FALLBACK_PROGRAM, p->error);
476
477 /* Could just go straight to the batchbuffer from here:
478 */
479 if (i915->state.ProgramSize != (program_size + decl_size) ||
480 memcmp(i915->state.Program + decl_size, p->program,
481 program_size * sizeof(int)) != 0) {
482 I915_STATECHANGE(i915, I915_UPLOAD_PROGRAM);
483 memcpy(i915->state.Program, p->declarations, decl_size * sizeof(int));
484 memcpy(i915->state.Program + decl_size, p->program,
485 program_size * sizeof(int));
486 i915->state.ProgramSize = decl_size + program_size;
487 }
488
489 /* Always seemed to get a failure if I used memcmp() to
490 * shortcircuit this state upload. Needs further investigation?
491 */
492 if (p->nr_constants) {
493 GLuint nr = p->nr_constants;
494
495 I915_ACTIVESTATE(i915, I915_UPLOAD_CONSTANTS, 1);
496 I915_STATECHANGE(i915, I915_UPLOAD_CONSTANTS);
497
498 i915->state.Constant[0] = _3DSTATE_PIXEL_SHADER_CONSTANTS | ((nr) * 4);
499 i915->state.Constant[1] = (1 << (nr - 1)) | ((1 << (nr - 1)) - 1);
500
501 memcpy(&i915->state.Constant[2], p->constant, 4 * sizeof(int) * (nr));
502 i915->state.ConstantSize = 2 + (nr) * 4;
503
504 if (0) {
505 GLuint i;
506 for (i = 0; i < nr; i++) {
507 fprintf(stderr, "const[%d]: %f %f %f %f\n", i,
508 p->constant[i][0],
509 p->constant[i][1], p->constant[i][2], p->constant[i][3]);
510 }
511 }
512 }
513 else {
514 I915_ACTIVESTATE(i915, I915_UPLOAD_CONSTANTS, 0);
515 }
516
517 p->on_hardware = 1;
518 }