1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "main/glheader.h"
29 #include "main/macros.h"
30 #include "main/enums.h"
32 #include "program/prog_instruction.h"
33 #include "program/prog_parameter.h"
34 #include "program/program.h"
35 #include "program/programopt.h"
36 #include "program/prog_print.h"
39 #include "tnl/t_context.h"
41 #include "intel_batchbuffer.h"
44 #include "i915_context.h"
45 #include "i915_program.h"
47 static const GLfloat sin_quad_constants[2][4] = {
62 static const GLfloat sin_constants[4] = { 1.0,
64 1.0 / (5 * 4 * 3 * 2 * 1),
65 -1.0 / (7 * 6 * 5 * 4 * 3 * 2 * 1)
68 /* 1, -1/2!, 1/4!, -1/6! */
69 static const GLfloat cos_constants[4] = { 1.0,
71 1.0 / (4 * 3 * 2 * 1),
72 -1.0 / (6 * 5 * 4 * 3 * 2 * 1)
76 * Retrieve a ureg for the given source register. Will emit
77 * constants, apply swizzling and negation as needed.
80 src_vector(struct i915_fragment_program *p,
81 const struct prog_src_register *source,
82 const struct gl_fragment_program *program)
86 switch (source->File) {
90 case PROGRAM_TEMPORARY:
91 if (source->Index >= I915_MAX_TEMPORARY) {
92 i915_program_error(p, "Exceeded max temporary reg: %d/%d",
93 source->Index, I915_MAX_TEMPORARY);
96 src = UREG(REG_TYPE_R, source->Index);
99 switch (source->Index) {
100 case FRAG_ATTRIB_WPOS:
101 src = i915_emit_decl(p, REG_TYPE_T, p->wpos_tex, D0_CHANNEL_ALL);
103 case FRAG_ATTRIB_COL0:
104 src = i915_emit_decl(p, REG_TYPE_T, T_DIFFUSE, D0_CHANNEL_ALL);
106 case FRAG_ATTRIB_COL1:
107 src = i915_emit_decl(p, REG_TYPE_T, T_SPECULAR, D0_CHANNEL_XYZ);
108 src = swizzle(src, X, Y, Z, ONE);
110 case FRAG_ATTRIB_FOGC:
111 src = i915_emit_decl(p, REG_TYPE_T, T_FOG_W, D0_CHANNEL_W);
112 src = swizzle(src, W, ZERO, ZERO, ONE);
114 case FRAG_ATTRIB_TEX0:
115 case FRAG_ATTRIB_TEX1:
116 case FRAG_ATTRIB_TEX2:
117 case FRAG_ATTRIB_TEX3:
118 case FRAG_ATTRIB_TEX4:
119 case FRAG_ATTRIB_TEX5:
120 case FRAG_ATTRIB_TEX6:
121 case FRAG_ATTRIB_TEX7:
122 src = i915_emit_decl(p, REG_TYPE_T,
123 T_TEX0 + (source->Index - FRAG_ATTRIB_TEX0),
127 case FRAG_ATTRIB_VAR0:
128 case FRAG_ATTRIB_VAR0 + 1:
129 case FRAG_ATTRIB_VAR0 + 2:
130 case FRAG_ATTRIB_VAR0 + 3:
131 case FRAG_ATTRIB_VAR0 + 4:
132 case FRAG_ATTRIB_VAR0 + 5:
133 case FRAG_ATTRIB_VAR0 + 6:
134 case FRAG_ATTRIB_VAR0 + 7:
135 src = i915_emit_decl(p, REG_TYPE_T,
136 T_TEX0 + (source->Index - FRAG_ATTRIB_VAR0),
141 i915_program_error(p, "Bad source->Index: %d", source->Index);
146 /* Various paramters and env values. All emitted to
147 * hardware as program constants.
149 case PROGRAM_LOCAL_PARAM:
150 src = i915_emit_param4fv(p, program->Base.LocalParams[source->Index]);
153 case PROGRAM_ENV_PARAM:
155 i915_emit_param4fv(p,
156 p->ctx->FragmentProgram.Parameters[source->
160 case PROGRAM_CONSTANT:
161 case PROGRAM_STATE_VAR:
162 case PROGRAM_NAMED_PARAM:
163 case PROGRAM_UNIFORM:
165 i915_emit_param4fv(p,
166 program->Base.Parameters->ParameterValues[source->
171 i915_program_error(p, "Bad source->File: %d", source->File);
176 GET_SWZ(source->Swizzle, 0),
177 GET_SWZ(source->Swizzle, 1),
178 GET_SWZ(source->Swizzle, 2), GET_SWZ(source->Swizzle, 3));
182 GET_BIT(source->Negate, 0),
183 GET_BIT(source->Negate, 1),
184 GET_BIT(source->Negate, 2),
185 GET_BIT(source->Negate, 3));
192 get_result_vector(struct i915_fragment_program *p,
193 const struct prog_instruction *inst)
195 switch (inst->DstReg.File) {
197 switch (inst->DstReg.Index) {
198 case FRAG_RESULT_COLOR:
199 return UREG(REG_TYPE_OC, 0);
200 case FRAG_RESULT_DEPTH:
201 p->depth_written = 1;
202 return UREG(REG_TYPE_OD, 0);
204 i915_program_error(p, "Bad inst->DstReg.Index: %d",
208 case PROGRAM_TEMPORARY:
209 return UREG(REG_TYPE_R, inst->DstReg.Index);
211 i915_program_error(p, "Bad inst->DstReg.File: %d", inst->DstReg.File);
217 get_result_flags(const struct prog_instruction *inst)
221 if (inst->SaturateMode == SATURATE_ZERO_ONE)
222 flags |= A0_DEST_SATURATE;
223 if (inst->DstReg.WriteMask & WRITEMASK_X)
224 flags |= A0_DEST_CHANNEL_X;
225 if (inst->DstReg.WriteMask & WRITEMASK_Y)
226 flags |= A0_DEST_CHANNEL_Y;
227 if (inst->DstReg.WriteMask & WRITEMASK_Z)
228 flags |= A0_DEST_CHANNEL_Z;
229 if (inst->DstReg.WriteMask & WRITEMASK_W)
230 flags |= A0_DEST_CHANNEL_W;
236 translate_tex_src_target(struct i915_fragment_program *p, GLubyte bit)
239 case TEXTURE_1D_INDEX:
240 return D0_SAMPLE_TYPE_2D;
241 case TEXTURE_2D_INDEX:
242 return D0_SAMPLE_TYPE_2D;
243 case TEXTURE_RECT_INDEX:
244 return D0_SAMPLE_TYPE_2D;
245 case TEXTURE_3D_INDEX:
246 return D0_SAMPLE_TYPE_VOLUME;
247 case TEXTURE_CUBE_INDEX:
248 return D0_SAMPLE_TYPE_CUBE;
250 i915_program_error(p, "TexSrcBit: %d", bit);
255 #define EMIT_TEX( OP ) \
257 GLuint dim = translate_tex_src_target( p, inst->TexSrcTarget ); \
258 const struct gl_fragment_program *program = p->ctx->FragmentProgram._Current; \
259 GLuint unit = program->Base.SamplerUnits[inst->TexSrcUnit]; \
260 GLuint sampler = i915_emit_decl(p, REG_TYPE_S, \
262 GLuint coord = src_vector( p, &inst->SrcReg[0], program); \
265 i915_emit_texld( p, get_live_regs(p, inst), \
266 get_result_vector( p, inst ), \
267 get_result_flags( inst ), \
273 #define EMIT_ARITH( OP, N ) \
275 i915_emit_arith( p, \
277 get_result_vector( p, inst ), \
278 get_result_flags( inst ), 0, \
279 (N<1)?0:src_vector( p, &inst->SrcReg[0], program), \
280 (N<2)?0:src_vector( p, &inst->SrcReg[1], program), \
281 (N<3)?0:src_vector( p, &inst->SrcReg[2], program)); \
284 #define EMIT_1ARG_ARITH( OP ) EMIT_ARITH( OP, 1 )
285 #define EMIT_2ARG_ARITH( OP ) EMIT_ARITH( OP, 2 )
286 #define EMIT_3ARG_ARITH( OP ) EMIT_ARITH( OP, 3 )
289 * TODO: consider moving this into core
291 static void calc_live_regs( struct i915_fragment_program *p )
293 const struct gl_fragment_program *program = p->ctx->FragmentProgram._Current;
294 GLuint regsUsed = 0xffff0000;
297 for (i = program->Base.NumInstructions - 1; i >= 0; i--) {
298 struct prog_instruction *inst = &program->Base.Instructions[i];
299 int opArgs = _mesa_num_inst_src_regs(inst->Opcode);
302 /* Register is written to: unmark as live for this and preceeding ops */
303 if (inst->DstReg.File == PROGRAM_TEMPORARY)
304 regsUsed &= ~(1 << inst->DstReg.Index);
306 for (a = 0; a < opArgs; a++) {
307 /* Register is read from: mark as live for this and preceeding ops */
308 if (inst->SrcReg[a].File == PROGRAM_TEMPORARY)
309 regsUsed |= 1 << inst->SrcReg[a].Index;
312 p->usedRegs[i] = regsUsed;
316 static GLuint get_live_regs( struct i915_fragment_program *p,
317 const struct prog_instruction *inst )
319 const struct gl_fragment_program *program = p->ctx->FragmentProgram._Current;
320 GLuint nr = inst - program->Base.Instructions;
322 return p->usedRegs[nr];
326 /* Possible concerns:
328 * SIN, COS -- could use another taylor step?
329 * LIT -- results seem a little different to sw mesa
330 * LOG -- different to mesa on negative numbers, but this is conformant.
332 * Parse failures -- Mesa doesn't currently give a good indication
333 * internally whether a particular program string parsed or not. This
334 * can lead to confusion -- hopefully we cope with it ok now.
338 upload_program(struct i915_fragment_program *p)
340 const struct gl_fragment_program *program =
341 p->ctx->FragmentProgram._Current;
342 const struct prog_instruction *inst = program->Base.Instructions;
344 if (INTEL_DEBUG & DEBUG_WM)
345 _mesa_print_program(&program->Base);
347 /* Is this a parse-failed program? Ensure a valid program is
348 * loaded, as the flagging of an error isn't sufficient to stop
349 * this being uploaded to hardware.
351 if (inst[0].Opcode == OPCODE_END) {
352 GLuint tmp = i915_get_utemp(p);
355 UREG(REG_TYPE_OC, 0),
356 A0_DEST_CHANNEL_ALL, 0,
357 swizzle(tmp, ONE, ZERO, ONE, ONE), 0, 0);
361 if (program->Base.NumInstructions > I915_MAX_INSN) {
362 i915_program_error(p, "Exceeded max instructions (%d out of %d)",
363 program->Base.NumInstructions, I915_MAX_INSN);
367 /* Not always needed:
372 GLuint src0, src1, src2, flags;
373 GLuint tmp = 0, dst, consts0 = 0, consts1 = 0;
375 switch (inst->Opcode) {
377 src0 = src_vector(p, &inst->SrcReg[0], program);
380 get_result_vector(p, inst),
381 get_result_flags(inst), 0,
382 src0, negate(src0, 1, 1, 1, 1), 0);
386 EMIT_2ARG_ARITH(A0_ADD);
390 src0 = src_vector(p, &inst->SrcReg[0], program);
391 src1 = src_vector(p, &inst->SrcReg[1], program);
392 src2 = src_vector(p, &inst->SrcReg[2], program);
393 i915_emit_arith(p, A0_CMP, get_result_vector(p, inst), get_result_flags(inst), 0, src0, src2, src1); /* NOTE: order of src2, src1 */
397 src0 = src_vector(p, &inst->SrcReg[0], program);
398 tmp = i915_get_utemp(p);
399 consts0 = i915_emit_const4fv(p, sin_quad_constants[0]);
400 consts1 = i915_emit_const4fv(p, sin_quad_constants[1]);
402 /* Reduce range from repeating about [-pi,pi] to [-1,1] */
405 tmp, A0_DEST_CHANNEL_X, 0,
407 swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */
408 swizzle(consts0, W, ZERO, ZERO, ZERO)); /* .75 */
410 i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0);
414 tmp, A0_DEST_CHANNEL_X, 0,
416 swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */
417 swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */
419 /* Compute COS with the same calculation used for SIN, but a
420 * different source range has been mapped to [-1,1] this time.
423 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */
426 tmp, A0_DEST_CHANNEL_Y, 0,
427 swizzle(tmp, ZERO, X, ZERO, ZERO),
428 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
431 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */
434 tmp, A0_DEST_CHANNEL_Y, 0,
435 swizzle(tmp, ZERO, X, ZERO, ZERO),
439 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */
442 tmp, A0_DEST_CHANNEL_X, 0,
444 swizzle(consts1, X, Y, ZERO, ZERO),
447 /* tmp.x now contains a first approximation (y). Now, weight it
448 * against tmp.y**2 to get closer.
452 tmp, A0_DEST_CHANNEL_Y, 0,
453 swizzle(tmp, ZERO, X, ZERO, ZERO),
454 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
457 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */
460 tmp, A0_DEST_CHANNEL_Y, 0,
461 swizzle(tmp, ZERO, X, ZERO, ZERO),
462 swizzle(tmp, ZERO, Y, ZERO, ZERO),
463 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0));
465 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */
468 get_result_vector(p, inst),
469 get_result_flags(inst), 0,
470 swizzle(consts1, W, W, W, W),
471 swizzle(tmp, Y, Y, Y, Y),
472 swizzle(tmp, X, X, X, X));
476 EMIT_2ARG_ARITH(A0_DP3);
480 EMIT_2ARG_ARITH(A0_DP4);
484 src0 = src_vector(p, &inst->SrcReg[0], program);
485 src1 = src_vector(p, &inst->SrcReg[1], program);
489 get_result_vector(p, inst),
490 get_result_flags(inst), 0,
491 swizzle(src0, X, Y, Z, ONE), src1, 0);
495 src0 = src_vector(p, &inst->SrcReg[0], program);
496 src1 = src_vector(p, &inst->SrcReg[1], program);
498 /* result[0] = 1 * 1;
499 * result[1] = a[1] * b[1];
500 * result[2] = a[2] * 1;
501 * result[3] = 1 * b[3];
505 get_result_vector(p, inst),
506 get_result_flags(inst), 0,
507 swizzle(src0, ONE, Y, Z, ONE),
508 swizzle(src1, ONE, Y, ONE, W), 0);
512 src0 = src_vector(p, &inst->SrcReg[0], program);
516 get_result_vector(p, inst),
517 get_result_flags(inst), 0,
518 swizzle(src0, X, X, X, X), 0, 0);
522 EMIT_1ARG_ARITH(A0_FLR);
526 EMIT_1ARG_ARITH(A0_TRC);
530 EMIT_1ARG_ARITH(A0_FRC);
534 src0 = src_vector(p, &inst->SrcReg[0], program);
535 tmp = i915_get_utemp(p);
537 i915_emit_texld(p, get_live_regs(p, inst),
538 tmp, A0_DEST_CHANNEL_ALL, /* use a dummy dest reg */
539 0, src0, T0_TEXKILL);
543 if (inst->DstReg.CondMask == COND_TR) {
544 tmp = i915_get_utemp(p);
546 i915_emit_texld(p, get_live_regs(p, inst),
547 tmp, A0_DEST_CHANNEL_ALL,
548 0, /* use a dummy dest reg */
549 swizzle(tmp, ONE, ONE, ONE, ONE), /* always */
553 i915_program_error(p, "Unsupported KIL_NV condition code: %d",
554 inst->DstReg.CondMask);
559 src0 = src_vector(p, &inst->SrcReg[0], program);
563 get_result_vector(p, inst),
564 get_result_flags(inst), 0,
565 swizzle(src0, X, X, X, X), 0, 0);
569 src0 = src_vector(p, &inst->SrcReg[0], program);
570 tmp = i915_get_utemp(p);
572 /* tmp = max( a.xyzw, a.00zw )
573 * XXX: Clamp tmp.w to -128..128
575 * tmp.y = tmp.w * tmp.y
577 * result = cmp (a.11-x1, a.1x01, a.1xy1 )
579 i915_emit_arith(p, A0_MAX, tmp, A0_DEST_CHANNEL_ALL, 0,
580 src0, swizzle(src0, ZERO, ZERO, Z, W), 0);
582 i915_emit_arith(p, A0_LOG, tmp, A0_DEST_CHANNEL_Y, 0,
583 swizzle(tmp, Y, Y, Y, Y), 0, 0);
585 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_Y, 0,
586 swizzle(tmp, ZERO, Y, ZERO, ZERO),
587 swizzle(tmp, ZERO, W, ZERO, ZERO), 0);
589 i915_emit_arith(p, A0_EXP, tmp, A0_DEST_CHANNEL_Y, 0,
590 swizzle(tmp, Y, Y, Y, Y), 0, 0);
592 i915_emit_arith(p, A0_CMP,
593 get_result_vector(p, inst),
594 get_result_flags(inst), 0,
595 negate(swizzle(tmp, ONE, ONE, X, ONE), 0, 0, 1, 0),
596 swizzle(tmp, ONE, X, ZERO, ONE),
597 swizzle(tmp, ONE, X, Y, ONE));
602 src0 = src_vector(p, &inst->SrcReg[0], program);
603 src1 = src_vector(p, &inst->SrcReg[1], program);
604 src2 = src_vector(p, &inst->SrcReg[2], program);
605 flags = get_result_flags(inst);
606 tmp = i915_get_utemp(p);
613 * result = (-c)*a + tmp
615 i915_emit_arith(p, A0_MAD, tmp,
616 flags & A0_DEST_CHANNEL_ALL, 0, src1, src0, src2);
618 i915_emit_arith(p, A0_MAD,
619 get_result_vector(p, inst),
620 flags, 0, negate(src2, 1, 1, 1, 1), src0, tmp);
624 EMIT_3ARG_ARITH(A0_MAD);
628 EMIT_2ARG_ARITH(A0_MAX);
632 src0 = src_vector(p, &inst->SrcReg[0], program);
633 src1 = src_vector(p, &inst->SrcReg[1], program);
634 tmp = i915_get_utemp(p);
635 flags = get_result_flags(inst);
639 tmp, flags & A0_DEST_CHANNEL_ALL, 0,
640 negate(src0, 1, 1, 1, 1),
641 negate(src1, 1, 1, 1, 1), 0);
645 get_result_vector(p, inst),
646 flags, 0, negate(tmp, 1, 1, 1, 1), 0, 0);
650 EMIT_1ARG_ARITH(A0_MOV);
654 EMIT_2ARG_ARITH(A0_MUL);
661 /* Don't implement noise because we just don't have the instructions
662 * to spare. We aren't the first vendor to do so.
664 i915_program_error(p, "Stubbed-out noise functions");
667 get_result_vector(p, inst),
668 get_result_flags(inst), 0,
669 swizzle(tmp, ZERO, ZERO, ZERO, ZERO), 0, 0);
673 src0 = src_vector(p, &inst->SrcReg[0], program);
674 src1 = src_vector(p, &inst->SrcReg[1], program);
675 tmp = i915_get_utemp(p);
676 flags = get_result_flags(inst);
678 /* XXX: masking on intermediate values, here and elsewhere.
682 tmp, A0_DEST_CHANNEL_X, 0,
683 swizzle(src0, X, X, X, X), 0, 0);
685 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_X, 0, tmp, src1, 0);
690 get_result_vector(p, inst),
691 flags, 0, swizzle(tmp, X, X, X, X), 0, 0);
696 src0 = src_vector(p, &inst->SrcReg[0], program);
700 get_result_vector(p, inst),
701 get_result_flags(inst), 0,
702 swizzle(src0, X, X, X, X), 0, 0);
707 src0 = src_vector(p, &inst->SrcReg[0], program);
711 get_result_vector(p, inst),
712 get_result_flags(inst), 0,
713 swizzle(src0, X, X, X, X), 0, 0);
717 src0 = src_vector(p, &inst->SrcReg[0], program);
718 tmp = i915_get_utemp(p);
721 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
722 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
723 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
724 * scs.x = DP4 t1, sin_constants
725 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
726 * scs.y = DP4 t1, cos_constants
730 tmp, A0_DEST_CHANNEL_XY, 0,
731 swizzle(src0, X, X, ONE, ONE),
732 swizzle(src0, X, ONE, ONE, ONE), 0);
736 tmp, A0_DEST_CHANNEL_ALL, 0,
737 swizzle(tmp, X, Y, X, Y),
738 swizzle(tmp, X, X, ONE, ONE), 0);
740 if (inst->DstReg.WriteMask & WRITEMASK_Y) {
743 if (inst->DstReg.WriteMask & WRITEMASK_X)
744 tmp1 = i915_get_utemp(p);
750 tmp1, A0_DEST_CHANNEL_ALL, 0,
751 swizzle(tmp, X, Y, Y, W),
752 swizzle(tmp, X, Z, ONE, ONE), 0);
756 get_result_vector(p, inst),
757 A0_DEST_CHANNEL_Y, 0,
758 swizzle(tmp1, W, Z, Y, X),
759 i915_emit_const4fv(p, sin_constants), 0);
762 if (inst->DstReg.WriteMask & WRITEMASK_X) {
765 tmp, A0_DEST_CHANNEL_XYZ, 0,
766 swizzle(tmp, X, X, Z, ONE),
767 swizzle(tmp, Z, ONE, ONE, ONE), 0);
771 get_result_vector(p, inst),
772 A0_DEST_CHANNEL_X, 0,
773 swizzle(tmp, ONE, Z, Y, X),
774 i915_emit_const4fv(p, cos_constants), 0);
779 tmp = i915_get_utemp(p);
780 flags = get_result_flags(inst);
781 dst = get_result_vector(p, inst);
783 /* dst = src1 >= src2 */
788 src_vector(p, &inst->SrcReg[0], program),
789 src_vector(p, &inst->SrcReg[1], program),
791 /* tmp = src1 <= src2 */
796 negate(src_vector(p, &inst->SrcReg[0], program),
798 negate(src_vector(p, &inst->SrcReg[1], program),
801 /* dst = tmp && dst */
812 src0 = src_vector(p, &inst->SrcReg[0], program);
813 tmp = i915_get_utemp(p);
814 consts0 = i915_emit_const4fv(p, sin_quad_constants[0]);
815 consts1 = i915_emit_const4fv(p, sin_quad_constants[1]);
817 /* Reduce range from repeating about [-pi,pi] to [-1,1] */
820 tmp, A0_DEST_CHANNEL_X, 0,
822 swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */
823 swizzle(consts0, Z, ZERO, ZERO, ZERO)); /* .5 */
825 i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0);
829 tmp, A0_DEST_CHANNEL_X, 0,
831 swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */
832 swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */
834 /* Compute sin using a quadratic and quartic. It gives continuity
835 * that repeating the Taylor series lacks every 2*pi, and has
838 * The idea was described at:
839 * http://www.devmaster.net/forums/showthread.php?t=5784
842 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */
845 tmp, A0_DEST_CHANNEL_Y, 0,
846 swizzle(tmp, ZERO, X, ZERO, ZERO),
847 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
850 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */
853 tmp, A0_DEST_CHANNEL_Y, 0,
854 swizzle(tmp, ZERO, X, ZERO, ZERO),
858 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */
861 tmp, A0_DEST_CHANNEL_X, 0,
863 swizzle(consts1, X, Y, ZERO, ZERO),
866 /* tmp.x now contains a first approximation (y). Now, weight it
867 * against tmp.y**2 to get closer.
871 tmp, A0_DEST_CHANNEL_Y, 0,
872 swizzle(tmp, ZERO, X, ZERO, ZERO),
873 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
876 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */
879 tmp, A0_DEST_CHANNEL_Y, 0,
880 swizzle(tmp, ZERO, X, ZERO, ZERO),
881 swizzle(tmp, ZERO, Y, ZERO, ZERO),
882 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0));
884 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */
887 get_result_vector(p, inst),
888 get_result_flags(inst), 0,
889 swizzle(consts1, W, W, W, W),
890 swizzle(tmp, Y, Y, Y, Y),
891 swizzle(tmp, X, X, X, X));
896 EMIT_2ARG_ARITH(A0_SGE);
902 get_result_vector( p, inst ),
903 get_result_flags( inst ), 0,
904 negate(src_vector( p, &inst->SrcReg[0], program),
906 negate(src_vector( p, &inst->SrcReg[1], program),
914 get_result_vector( p, inst ),
915 get_result_flags( inst ), 0,
916 negate(src_vector( p, &inst->SrcReg[0], program),
918 negate(src_vector( p, &inst->SrcReg[1], program),
924 EMIT_2ARG_ARITH(A0_SLT);
928 tmp = i915_get_utemp(p);
929 flags = get_result_flags(inst);
930 dst = get_result_vector(p, inst);
932 /* dst = src1 < src2 */
937 src_vector(p, &inst->SrcReg[0], program),
938 src_vector(p, &inst->SrcReg[1], program),
940 /* tmp = src1 > src2 */
945 negate(src_vector(p, &inst->SrcReg[0], program),
947 negate(src_vector(p, &inst->SrcReg[1], program),
950 /* dst = tmp || dst */
954 flags | A0_DEST_SATURATE, 0,
961 src0 = src_vector(p, &inst->SrcReg[0], program);
962 src1 = src_vector(p, &inst->SrcReg[1], program);
966 get_result_vector(p, inst),
967 get_result_flags(inst), 0,
968 src0, negate(src1, 1, 1, 1, 1), 0);
972 EMIT_1ARG_ARITH(A0_MOV); /* extended swizzle handled natively */
989 * result.x = src0.y * src1.z - src0.z * src1.y;
990 * result.y = src0.z * src1.x - src0.x * src1.z;
991 * result.z = src0.x * src1.y - src0.y * src1.x;
994 src0 = src_vector(p, &inst->SrcReg[0], program);
995 src1 = src_vector(p, &inst->SrcReg[1], program);
996 tmp = i915_get_utemp(p);
1000 tmp, A0_DEST_CHANNEL_ALL, 0,
1001 swizzle(src0, Z, X, Y, ONE),
1002 swizzle(src1, Y, Z, X, ONE), 0);
1006 get_result_vector(p, inst),
1007 get_result_flags(inst), 0,
1008 swizzle(src0, Y, Z, X, ONE),
1009 swizzle(src1, Z, X, Y, ONE),
1010 negate(tmp, 1, 1, 1, 0));
1016 case OPCODE_BGNLOOP:
1026 case OPCODE_ENDLOOP:
1031 i915_program_error(p, "Unsupported opcode: %s",
1032 _mesa_opcode_string(inst->Opcode));
1037 /* These opcodes are claimed as GLSL, NV_vp, and ARB_vp in
1038 * prog_instruction.h, but apparently GLSL doesn't ever emit them.
1039 * Instead, it translates to EX2 or LG2.
1043 /* These opcodes are claimed by GLSL in prog_instruction.h, but
1044 * only NV_vp/fp appears to emit them.
1047 i915_program_error(p, "bad opcode: %s",
1048 _mesa_opcode_string(inst->Opcode));
1053 i915_release_utemps(p);
1057 /* Rather than trying to intercept and jiggle depth writes during
1058 * emit, just move the value into its correct position at the end of
1062 fixup_depth_write(struct i915_fragment_program *p)
1064 if (p->depth_written) {
1065 GLuint depth = UREG(REG_TYPE_OD, 0);
1069 depth, A0_DEST_CHANNEL_W, 0,
1070 swizzle(depth, X, Y, Z, Z), 0, 0);
1076 check_wpos(struct i915_fragment_program *p)
1078 GLuint inputs = p->FragProg.Base.InputsRead;
1083 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) {
1084 if (inputs & (FRAG_BIT_TEX(i) | FRAG_BIT_VAR(i)))
1086 else if (inputs & FRAG_BIT_WPOS) {
1088 inputs &= ~FRAG_BIT_WPOS;
1092 if (inputs & FRAG_BIT_WPOS) {
1093 i915_program_error(p, "No free texcoord for wpos value");
1099 translate_program(struct i915_fragment_program *p)
1101 struct i915_context *i915 = I915_CONTEXT(p->ctx);
1103 if (INTEL_DEBUG & DEBUG_WM) {
1105 _mesa_print_program(&p->ctx->FragmentProgram._Current->Base);
1109 i915_init_program(i915, p);
1112 fixup_depth_write(p);
1113 i915_fini_program(p);
1115 if (INTEL_DEBUG & DEBUG_WM) {
1117 i915_disassemble_program(i915->state.Program, i915->state.ProgramSize);
1125 track_params(struct i915_fragment_program *p)
1130 _mesa_load_state_parameters(p->ctx, p->FragProg.Base.Parameters);
1132 for (i = 0; i < p->nr_params; i++) {
1133 GLint reg = p->param[i].reg;
1134 COPY_4V(p->constant[reg], p->param[i].values);
1137 p->params_uptodate = 1;
1138 p->on_hardware = 0; /* overkill */
1143 i915BindProgram(GLcontext * ctx, GLenum target, struct gl_program *prog)
1145 if (target == GL_FRAGMENT_PROGRAM_ARB) {
1146 struct i915_context *i915 = I915_CONTEXT(ctx);
1147 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1149 if (i915->current_program == p)
1152 if (i915->current_program) {
1153 i915->current_program->on_hardware = 0;
1154 i915->current_program->params_uptodate = 0;
1157 i915->current_program = p;
1159 assert(p->on_hardware == 0);
1160 assert(p->params_uptodate == 0);
1165 static struct gl_program *
1166 i915NewProgram(GLcontext * ctx, GLenum target, GLuint id)
1169 case GL_VERTEX_PROGRAM_ARB:
1170 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
1173 case GL_FRAGMENT_PROGRAM_ARB:{
1174 struct i915_fragment_program *prog =
1175 CALLOC_STRUCT(i915_fragment_program);
1177 i915_init_program(I915_CONTEXT(ctx), prog);
1179 return _mesa_init_fragment_program(ctx, &prog->FragProg,
1189 return _mesa_new_program(ctx, target, id);
1194 i915DeleteProgram(GLcontext * ctx, struct gl_program *prog)
1196 if (prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1197 struct i915_context *i915 = I915_CONTEXT(ctx);
1198 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1200 if (i915->current_program == p)
1201 i915->current_program = 0;
1204 _mesa_delete_program(ctx, prog);
1209 i915IsProgramNative(GLcontext * ctx, GLenum target, struct gl_program *prog)
1211 if (target == GL_FRAGMENT_PROGRAM_ARB) {
1212 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1215 translate_program(p);
1224 i915ProgramStringNotify(GLcontext * ctx,
1225 GLenum target, struct gl_program *prog)
1227 if (target == GL_FRAGMENT_PROGRAM_ARB) {
1228 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1231 /* Hack: make sure fog is correctly enabled according to this
1232 * fragment program's fog options.
1234 if (p->FragProg.FogOption) {
1235 /* add extra instructions to do fog, then turn off FogOption field */
1236 _mesa_append_fog_code(ctx, &p->FragProg);
1237 p->FragProg.FogOption = GL_NONE;
1241 (void) _tnl_program_string(ctx, target, prog);
1243 /* XXX check if program is legal, within limits */
1248 i915_update_program(GLcontext *ctx)
1250 struct intel_context *intel = intel_context(ctx);
1251 struct i915_context *i915 = i915_context(&intel->ctx);
1252 struct i915_fragment_program *fp =
1253 (struct i915_fragment_program *) ctx->FragmentProgram._Current;
1255 if (i915->current_program != fp) {
1256 if (i915->current_program) {
1257 i915->current_program->on_hardware = 0;
1258 i915->current_program->params_uptodate = 0;
1261 i915->current_program = fp;
1264 if (!fp->translated)
1265 translate_program(fp);
1267 FALLBACK(&i915->intel, I915_FALLBACK_PROGRAM, fp->error);
1271 i915ValidateFragmentProgram(struct i915_context *i915)
1273 GLcontext *ctx = &i915->intel.ctx;
1274 struct intel_context *intel = intel_context(ctx);
1275 TNLcontext *tnl = TNL_CONTEXT(ctx);
1276 struct vertex_buffer *VB = &tnl->vb;
1278 struct i915_fragment_program *p =
1279 (struct i915_fragment_program *) ctx->FragmentProgram._Current;
1281 const GLuint inputsRead = p->FragProg.Base.InputsRead;
1282 GLuint s4 = i915->state.Ctx[I915_CTXREG_LIS4] & ~S4_VFMT_MASK;
1283 GLuint s2 = S2_TEXCOORD_NONE;
1288 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;
1291 translate_program(p);
1293 intel->vertex_attr_count = 0;
1294 intel->wpos_offset = 0;
1295 intel->wpos_size = 0;
1296 intel->coloroffset = 0;
1297 intel->specoffset = 0;
1299 if (inputsRead & FRAG_BITS_TEX_ANY) {
1300 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, S4_VFMT_XYZW, 16);
1303 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_3F_VIEWPORT, S4_VFMT_XYZ, 12);
1306 if (inputsRead & FRAG_BIT_COL0) {
1307 intel->coloroffset = offset / 4;
1308 EMIT_ATTR(_TNL_ATTRIB_COLOR0, EMIT_4UB_4F_BGRA, S4_VFMT_COLOR, 4);
1311 if (inputsRead & FRAG_BIT_COL1) {
1312 intel->specoffset = offset / 4;
1313 EMIT_ATTR(_TNL_ATTRIB_COLOR1, EMIT_4UB_4F_BGRA, S4_VFMT_SPEC_FOG, 4);
1316 if ((inputsRead & FRAG_BIT_FOGC) || i915->vertex_fog != I915_FOG_NONE) {
1317 EMIT_ATTR(_TNL_ATTRIB_FOG, EMIT_1F, S4_VFMT_FOG_PARAM, 4);
1320 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) {
1321 if (inputsRead & FRAG_BIT_TEX(i)) {
1322 int sz = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]->size;
1324 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
1325 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz));
1327 EMIT_ATTR(_TNL_ATTRIB_TEX0 + i, EMIT_SZ(sz), 0, sz * 4);
1329 else if (inputsRead & FRAG_BIT_VAR(i)) {
1330 int sz = VB->AttribPtr[_TNL_ATTRIB_GENERIC0 + i]->size;
1332 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
1333 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz));
1335 EMIT_ATTR(_TNL_ATTRIB_GENERIC0 + i, EMIT_SZ(sz), 0, sz * 4);
1337 else if (i == p->wpos_tex) {
1339 /* If WPOS is required, duplicate the XYZ position data in an
1340 * unused texture coordinate:
1342 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
1343 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(3));
1345 intel->wpos_offset = offset;
1346 intel->wpos_size = 3 * sizeof(GLuint);
1348 EMIT_PAD(intel->wpos_size);
1352 if (s2 != i915->state.Ctx[I915_CTXREG_LIS2] ||
1353 s4 != i915->state.Ctx[I915_CTXREG_LIS4]) {
1356 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
1358 /* Must do this *after* statechange, so as not to affect
1359 * buffered vertices reliant on the old state:
1361 intel->vertex_size = _tnl_install_attrs(&intel->ctx,
1362 intel->vertex_attrs,
1363 intel->vertex_attr_count,
1364 intel->ViewportMatrix.m, 0);
1366 intel->vertex_size >>= 2;
1368 i915->state.Ctx[I915_CTXREG_LIS2] = s2;
1369 i915->state.Ctx[I915_CTXREG_LIS4] = s4;
1371 k = intel->vtbl.check_vertex_size(intel, intel->vertex_size);
1375 if (!p->params_uptodate)
1378 if (!p->on_hardware)
1379 i915_upload_program(i915, p);
1383 i915InitFragProgFuncs(struct dd_function_table *functions)
1385 functions->BindProgram = i915BindProgram;
1386 functions->NewProgram = i915NewProgram;
1387 functions->DeleteProgram = i915DeleteProgram;
1388 functions->IsProgramNative = i915IsProgramNative;
1389 functions->ProgramStringNotify = i915ProgramStringNotify;