1 /**************************************************************************
3 * Copyright 2007 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 **************************************************************************/
32 #include "i915_context.h"
35 #include "pipe/p_shader_tokens.h"
36 #include "util/u_string.h"
37 #include "tgsi/tgsi_parse.h"
38 #include "tgsi/tgsi_dump.h"
40 #include "draw/draw_vertex.h"
44 * Simple pass-through fragment shader to use when we don't have
45 * a real shader (or it fails to compile for some reason).
47 static unsigned passthrough
[] =
49 _3DSTATE_PIXEL_SHADER_PROGRAM
| ((2*3)-1),
51 /* declare input color:
54 (REG_TYPE_T
<< D0_TYPE_SHIFT
) |
55 (T_DIFFUSE
<< D0_NR_SHIFT
) |
60 /* move to output color:
63 (REG_TYPE_OC
<< A0_DEST_TYPE_SHIFT
) |
65 (REG_TYPE_T
<< A0_SRC0_TYPE_SHIFT
) |
66 (T_DIFFUSE
<< A0_SRC0_NR_SHIFT
)),
67 0x01230000, /* .xyzw */
72 /* 1, -1/3!, 1/5!, -1/7! */
73 static const float sin_constants
[4] = { 1.0,
75 1.0f
/ (5 * 4 * 3 * 2 * 1),
76 -1.0f
/ (7 * 6 * 5 * 4 * 3 * 2 * 1)
79 /* 1, -1/2!, 1/4!, -1/6! */
80 static const float cos_constants
[4] = { 1.0,
82 1.0f
/ (4 * 3 * 2 * 1),
83 -1.0f
/ (6 * 5 * 4 * 3 * 2 * 1)
89 * component-wise negation of ureg
92 negate(int reg
, int x
, int y
, int z
, int w
)
94 /* Another neat thing about the UREG representation */
95 return reg
^ (((x
& 1) << UREG_CHANNEL_X_NEGATE_SHIFT
) |
96 ((y
& 1) << UREG_CHANNEL_Y_NEGATE_SHIFT
) |
97 ((z
& 1) << UREG_CHANNEL_Z_NEGATE_SHIFT
) |
98 ((w
& 1) << UREG_CHANNEL_W_NEGATE_SHIFT
));
103 * In the event of a translation failure, we'll generate a simple color
104 * pass-through program.
107 i915_use_passthrough_shader(struct i915_fragment_shader
*fs
)
109 fs
->program
= (uint
*) MALLOC(sizeof(passthrough
));
111 memcpy(fs
->program
, passthrough
, sizeof(passthrough
));
112 fs
->program_len
= Elements(passthrough
);
114 fs
->num_constants
= 0;
119 i915_program_error(struct i915_fp_compile
*p
, const char *msg
, ...)
124 debug_printf("i915_program_error: ");
125 va_start( args
, msg
);
126 util_vsnprintf( buffer
, sizeof(buffer
), msg
, args
);
128 debug_printf(buffer
);
137 * Construct a ureg for the given source register. Will emit
138 * constants, apply swizzling and negation as needed.
141 src_vector(struct i915_fp_compile
*p
,
142 const struct tgsi_full_src_register
*source
)
144 uint index
= source
->SrcRegister
.Index
;
145 uint src
, sem_name
, sem_ind
;
147 switch (source
->SrcRegister
.File
) {
148 case TGSI_FILE_TEMPORARY
:
149 if (source
->SrcRegister
.Index
>= I915_MAX_TEMPORARY
) {
150 i915_program_error(p
, "Exceeded max temporary reg");
153 src
= UREG(REG_TYPE_R
, index
);
155 case TGSI_FILE_INPUT
:
156 /* XXX: Packing COL1, FOGC into a single attribute works for
157 * texenv programs, but will fail for real fragment programs
158 * that use these attributes and expect them to be a full 4
159 * components wide. Could use a texcoord to pass these
160 * attributes if necessary, but that won't work in the general
163 * We also use a texture coordinate to pass wpos when possible.
166 sem_name
= p
->shader
->info
.input_semantic_name
[index
];
167 sem_ind
= p
->shader
->info
.input_semantic_index
[index
];
170 case TGSI_SEMANTIC_POSITION
:
171 debug_printf("SKIP SEM POS\n");
173 assert(p->wpos_tex != -1);
174 src = i915_emit_decl(p, REG_TYPE_T, p->wpos_tex, D0_CHANNEL_ALL);
177 case TGSI_SEMANTIC_COLOR
:
179 src
= i915_emit_decl(p
, REG_TYPE_T
, T_DIFFUSE
, D0_CHANNEL_ALL
);
182 /* secondary color */
183 assert(sem_ind
== 1);
184 src
= i915_emit_decl(p
, REG_TYPE_T
, T_SPECULAR
, D0_CHANNEL_XYZ
);
185 src
= swizzle(src
, X
, Y
, Z
, ONE
);
188 case TGSI_SEMANTIC_FOG
:
189 src
= i915_emit_decl(p
, REG_TYPE_T
, T_FOG_W
, D0_CHANNEL_W
);
190 src
= swizzle(src
, W
, W
, W
, W
);
192 case TGSI_SEMANTIC_GENERIC
:
193 /* usually a texcoord */
194 src
= i915_emit_decl(p
, REG_TYPE_T
, T_TEX0
+ sem_ind
, D0_CHANNEL_ALL
);
197 i915_program_error(p
, "Bad source->Index");
202 case TGSI_FILE_IMMEDIATE
:
203 assert(index
< p
->num_immediates
);
204 index
= p
->immediates_map
[index
];
206 case TGSI_FILE_CONSTANT
:
207 src
= UREG(REG_TYPE_CONST
, index
);
211 i915_program_error(p
, "Bad source->File");
215 if (source
->SrcRegister
.Extended
) {
217 source
->SrcRegisterExtSwz
.ExtSwizzleX
,
218 source
->SrcRegisterExtSwz
.ExtSwizzleY
,
219 source
->SrcRegisterExtSwz
.ExtSwizzleZ
,
220 source
->SrcRegisterExtSwz
.ExtSwizzleW
);
224 source
->SrcRegister
.SwizzleX
,
225 source
->SrcRegister
.SwizzleY
,
226 source
->SrcRegister
.SwizzleZ
,
227 source
->SrcRegister
.SwizzleW
);
231 /* There's both negate-all-components and per-component negation.
232 * Try to handle both here.
235 int nx
= source
->SrcRegisterExtSwz
.NegateX
;
236 int ny
= source
->SrcRegisterExtSwz
.NegateY
;
237 int nz
= source
->SrcRegisterExtSwz
.NegateZ
;
238 int nw
= source
->SrcRegisterExtSwz
.NegateW
;
239 if (source
->SrcRegister
.Negate
) {
245 src
= negate(src
, nx
, ny
, nz
, nw
);
248 /* no abs() or post-abs negation */
250 /* XXX assertions disabled to allow arbfplight.c to run */
251 /* XXX enable these assertions, or fix things */
252 assert(!source
->SrcRegisterExtMod
.Absolute
);
253 assert(!source
->SrcRegisterExtMod
.Negate
);
260 * Construct a ureg for a destination register.
263 get_result_vector(struct i915_fp_compile
*p
,
264 const struct tgsi_full_dst_register
*dest
)
266 switch (dest
->DstRegister
.File
) {
267 case TGSI_FILE_OUTPUT
:
269 uint sem_name
= p
->shader
->info
.output_semantic_name
[dest
->DstRegister
.Index
];
271 case TGSI_SEMANTIC_POSITION
:
272 return UREG(REG_TYPE_OD
, 0);
273 case TGSI_SEMANTIC_COLOR
:
274 return UREG(REG_TYPE_OC
, 0);
276 i915_program_error(p
, "Bad inst->DstReg.Index/semantics");
280 case TGSI_FILE_TEMPORARY
:
281 return UREG(REG_TYPE_R
, dest
->DstRegister
.Index
);
283 i915_program_error(p
, "Bad inst->DstReg.File");
290 * Compute flags for saturation and writemask.
293 get_result_flags(const struct tgsi_full_instruction
*inst
)
296 = inst
->FullDstRegisters
[0].DstRegister
.WriteMask
;
299 if (inst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
)
300 flags
|= A0_DEST_SATURATE
;
302 if (writeMask
& TGSI_WRITEMASK_X
)
303 flags
|= A0_DEST_CHANNEL_X
;
304 if (writeMask
& TGSI_WRITEMASK_Y
)
305 flags
|= A0_DEST_CHANNEL_Y
;
306 if (writeMask
& TGSI_WRITEMASK_Z
)
307 flags
|= A0_DEST_CHANNEL_Z
;
308 if (writeMask
& TGSI_WRITEMASK_W
)
309 flags
|= A0_DEST_CHANNEL_W
;
316 * Convert TGSI_TEXTURE_x token to DO_SAMPLE_TYPE_x token
319 translate_tex_src_target(struct i915_fp_compile
*p
, uint tex
)
322 case TGSI_TEXTURE_1D
:
323 return D0_SAMPLE_TYPE_2D
;
324 case TGSI_TEXTURE_2D
:
325 return D0_SAMPLE_TYPE_2D
;
326 case TGSI_TEXTURE_RECT
:
327 return D0_SAMPLE_TYPE_2D
;
328 case TGSI_TEXTURE_3D
:
329 return D0_SAMPLE_TYPE_VOLUME
;
330 case TGSI_TEXTURE_CUBE
:
331 return D0_SAMPLE_TYPE_CUBE
;
333 i915_program_error(p
, "TexSrc type");
340 * Generate texel lookup instruction.
343 emit_tex(struct i915_fp_compile
*p
,
344 const struct tgsi_full_instruction
*inst
,
347 uint texture
= inst
->InstructionExtTexture
.Texture
;
348 uint unit
= inst
->FullSrcRegisters
[1].SrcRegister
.Index
;
349 uint tex
= translate_tex_src_target( p
, texture
);
350 uint sampler
= i915_emit_decl(p
, REG_TYPE_S
, unit
, tex
);
351 uint coord
= src_vector( p
, &inst
->FullSrcRegisters
[0]);
354 get_result_vector( p
, &inst
->FullDstRegisters
[0] ),
355 get_result_flags( inst
),
363 * Generate a simple arithmetic instruction
364 * \param opcode the i915 opcode
365 * \param numArgs the number of input/src arguments
368 emit_simple_arith(struct i915_fp_compile
*p
,
369 const struct tgsi_full_instruction
*inst
,
370 uint opcode
, uint numArgs
)
372 uint arg1
, arg2
, arg3
;
374 assert(numArgs
<= 3);
376 arg1
= (numArgs
< 1) ? 0 : src_vector( p
, &inst
->FullSrcRegisters
[0] );
377 arg2
= (numArgs
< 2) ? 0 : src_vector( p
, &inst
->FullSrcRegisters
[1] );
378 arg3
= (numArgs
< 3) ? 0 : src_vector( p
, &inst
->FullSrcRegisters
[2] );
382 get_result_vector( p
, &inst
->FullDstRegisters
[0]),
383 get_result_flags( inst
), 0,
390 /** As above, but swap the first two src regs */
392 emit_simple_arith_swap2(struct i915_fp_compile
*p
,
393 const struct tgsi_full_instruction
*inst
,
394 uint opcode
, uint numArgs
)
396 struct tgsi_full_instruction inst2
;
398 assert(numArgs
== 2);
400 /* transpose first two registers */
402 inst2
.FullSrcRegisters
[0] = inst
->FullSrcRegisters
[1];
403 inst2
.FullSrcRegisters
[1] = inst
->FullSrcRegisters
[0];
405 emit_simple_arith(p
, &inst2
, opcode
, numArgs
);
410 #define M_PI 3.14159265358979323846
414 * Translate TGSI instruction to i915 instruction.
418 * SIN, COS -- could use another taylor step?
419 * LIT -- results seem a little different to sw mesa
420 * LOG -- different to mesa on negative numbers, but this is conformant.
423 i915_translate_instruction(struct i915_fp_compile
*p
,
424 const struct tgsi_full_instruction
*inst
)
427 uint src0
, src1
, src2
, flags
;
430 switch (inst
->Instruction
.Opcode
) {
431 case TGSI_OPCODE_ABS
:
432 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
435 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
436 get_result_flags(inst
), 0,
437 src0
, negate(src0
, 1, 1, 1, 1), 0);
440 case TGSI_OPCODE_ADD
:
441 emit_simple_arith(p
, inst
, A0_ADD
, 2);
444 case TGSI_OPCODE_CMP
:
445 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
446 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
447 src2
= src_vector(p
, &inst
->FullSrcRegisters
[2]);
448 i915_emit_arith(p
, A0_CMP
,
449 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
450 get_result_flags(inst
),
451 0, src0
, src2
, src1
); /* NOTE: order of src2, src1 */
454 case TGSI_OPCODE_COS
:
455 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
456 tmp
= i915_get_utemp(p
);
460 tmp
, A0_DEST_CHANNEL_X
, 0,
461 src0
, i915_emit_const1f(p
, 1.0f
/ (float) (M_PI
* 2.0)), 0);
463 i915_emit_arith(p
, A0_MOD
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
465 /* By choosing different taylor constants, could get rid of this mul:
469 tmp
, A0_DEST_CHANNEL_X
, 0,
470 tmp
, i915_emit_const1f(p
, (float) (M_PI
* 2.0)), 0);
473 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
474 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, 1
475 * t0 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
476 * result = DP4 t0, cos_constants
480 tmp
, A0_DEST_CHANNEL_XY
, 0,
481 swizzle(tmp
, X
, X
, ONE
, ONE
),
482 swizzle(tmp
, X
, ONE
, ONE
, ONE
), 0);
486 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
487 swizzle(tmp
, X
, Y
, X
, ONE
),
488 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
492 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
493 swizzle(tmp
, X
, X
, Z
, ONE
),
494 swizzle(tmp
, Z
, ONE
, ONE
, ONE
), 0);
498 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
499 get_result_flags(inst
), 0,
500 swizzle(tmp
, ONE
, Z
, Y
, X
),
501 i915_emit_const4fv(p
, cos_constants
), 0);
504 case TGSI_OPCODE_DP3
:
505 emit_simple_arith(p
, inst
, A0_DP3
, 2);
508 case TGSI_OPCODE_DP4
:
509 emit_simple_arith(p
, inst
, A0_DP4
, 2);
512 case TGSI_OPCODE_DPH
:
513 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
514 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
518 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
519 get_result_flags(inst
), 0,
520 swizzle(src0
, X
, Y
, Z
, ONE
), src1
, 0);
523 case TGSI_OPCODE_DST
:
524 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
525 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
527 /* result[0] = 1 * 1;
528 * result[1] = a[1] * b[1];
529 * result[2] = a[2] * 1;
530 * result[3] = 1 * b[3];
534 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
535 get_result_flags(inst
), 0,
536 swizzle(src0
, ONE
, Y
, Z
, ONE
),
537 swizzle(src1
, ONE
, Y
, ONE
, W
), 0);
540 case TGSI_OPCODE_END
:
544 case TGSI_OPCODE_EX2
:
545 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
549 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
550 get_result_flags(inst
), 0,
551 swizzle(src0
, X
, X
, X
, X
), 0, 0);
554 case TGSI_OPCODE_FLR
:
555 emit_simple_arith(p
, inst
, A0_FLR
, 1);
558 case TGSI_OPCODE_FRC
:
559 emit_simple_arith(p
, inst
, A0_FRC
, 1);
562 case TGSI_OPCODE_KIL
:
563 /* unconditional kill */
564 assert(0); /* not tested yet */
566 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
567 tmp
= i915_get_utemp(p
);
569 i915_emit_texld(p
, tmp
, A0_DEST_CHANNEL_ALL
, /* use a dummy dest reg */
570 0, src0
, T0_TEXKILL
);
574 case TGSI_OPCODE_KILP
:
575 /* kill if src[0].x < 0 || src[0].y < 0 ... */
576 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
577 tmp
= i915_get_utemp(p
);
580 tmp
, /* dest reg: a dummy reg */
581 A0_DEST_CHANNEL_ALL
, /* dest writemask */
584 T0_TEXKILL
); /* opcode */
587 case TGSI_OPCODE_LG2
:
588 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
592 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
593 get_result_flags(inst
), 0,
594 swizzle(src0
, X
, X
, X
, X
), 0, 0);
597 case TGSI_OPCODE_LIT
:
598 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
599 tmp
= i915_get_utemp(p
);
601 /* tmp = max( a.xyzw, a.00zw )
602 * XXX: Clamp tmp.w to -128..128
604 * tmp.y = tmp.w * tmp.y
606 * result = cmp (a.11-x1, a.1x01, a.1xy1 )
608 i915_emit_arith(p
, A0_MAX
, tmp
, A0_DEST_CHANNEL_ALL
, 0,
609 src0
, swizzle(src0
, ZERO
, ZERO
, Z
, W
), 0);
611 i915_emit_arith(p
, A0_LOG
, tmp
, A0_DEST_CHANNEL_Y
, 0,
612 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
614 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_Y
, 0,
615 swizzle(tmp
, ZERO
, Y
, ZERO
, ZERO
),
616 swizzle(tmp
, ZERO
, W
, ZERO
, ZERO
), 0);
618 i915_emit_arith(p
, A0_EXP
, tmp
, A0_DEST_CHANNEL_Y
, 0,
619 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
621 i915_emit_arith(p
, A0_CMP
,
622 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
623 get_result_flags(inst
), 0,
624 negate(swizzle(tmp
, ONE
, ONE
, X
, ONE
), 0, 0, 1, 0),
625 swizzle(tmp
, ONE
, X
, ZERO
, ONE
),
626 swizzle(tmp
, ONE
, X
, Y
, ONE
));
630 case TGSI_OPCODE_LRP
:
631 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
632 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
633 src2
= src_vector(p
, &inst
->FullSrcRegisters
[2]);
634 flags
= get_result_flags(inst
);
635 tmp
= i915_get_utemp(p
);
642 * result = (-c)*a + tmp
644 i915_emit_arith(p
, A0_MAD
, tmp
,
645 flags
& A0_DEST_CHANNEL_ALL
, 0, src1
, src0
, src2
);
647 i915_emit_arith(p
, A0_MAD
,
648 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
649 flags
, 0, negate(src2
, 1, 1, 1, 1), src0
, tmp
);
652 case TGSI_OPCODE_MAD
:
653 emit_simple_arith(p
, inst
, A0_MAD
, 3);
656 case TGSI_OPCODE_MAX
:
657 emit_simple_arith(p
, inst
, A0_MAX
, 2);
660 case TGSI_OPCODE_MIN
:
661 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
662 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
663 tmp
= i915_get_utemp(p
);
664 flags
= get_result_flags(inst
);
668 tmp
, flags
& A0_DEST_CHANNEL_ALL
, 0,
669 negate(src0
, 1, 1, 1, 1),
670 negate(src1
, 1, 1, 1, 1), 0);
674 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
675 flags
, 0, negate(tmp
, 1, 1, 1, 1), 0, 0);
678 case TGSI_OPCODE_MOV
:
679 case TGSI_OPCODE_SWZ
:
680 emit_simple_arith(p
, inst
, A0_MOV
, 1);
683 case TGSI_OPCODE_MUL
:
684 emit_simple_arith(p
, inst
, A0_MUL
, 2);
687 case TGSI_OPCODE_POW
:
688 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
689 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
690 tmp
= i915_get_utemp(p
);
691 flags
= get_result_flags(inst
);
693 /* XXX: masking on intermediate values, here and elsewhere.
697 tmp
, A0_DEST_CHANNEL_X
, 0,
698 swizzle(src0
, X
, X
, X
, X
), 0, 0);
700 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, src1
, 0);
704 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
705 flags
, 0, swizzle(tmp
, X
, X
, X
, X
), 0, 0);
708 case TGSI_OPCODE_RET
:
712 case TGSI_OPCODE_RCP
:
713 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
717 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
718 get_result_flags(inst
), 0,
719 swizzle(src0
, X
, X
, X
, X
), 0, 0);
722 case TGSI_OPCODE_RSQ
:
723 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
727 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
728 get_result_flags(inst
), 0,
729 swizzle(src0
, X
, X
, X
, X
), 0, 0);
732 case TGSI_OPCODE_SCS
:
733 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
734 tmp
= i915_get_utemp(p
);
737 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
738 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
739 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
740 * scs.x = DP4 t1, sin_constants
741 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
742 * scs.y = DP4 t1, cos_constants
746 tmp
, A0_DEST_CHANNEL_XY
, 0,
747 swizzle(src0
, X
, X
, ONE
, ONE
),
748 swizzle(src0
, X
, ONE
, ONE
, ONE
), 0);
752 tmp
, A0_DEST_CHANNEL_ALL
, 0,
753 swizzle(tmp
, X
, Y
, X
, Y
),
754 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
756 writemask
= inst
->FullDstRegisters
[0].DstRegister
.WriteMask
;
758 if (writemask
& TGSI_WRITEMASK_Y
) {
761 if (writemask
& TGSI_WRITEMASK_X
)
762 tmp1
= i915_get_utemp(p
);
768 tmp1
, A0_DEST_CHANNEL_ALL
, 0,
769 swizzle(tmp
, X
, Y
, Y
, W
),
770 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
774 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
775 A0_DEST_CHANNEL_Y
, 0,
776 swizzle(tmp1
, W
, Z
, Y
, X
),
777 i915_emit_const4fv(p
, sin_constants
), 0);
780 if (writemask
& TGSI_WRITEMASK_X
) {
783 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
784 swizzle(tmp
, X
, X
, Z
, ONE
),
785 swizzle(tmp
, Z
, ONE
, ONE
, ONE
), 0);
789 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
790 A0_DEST_CHANNEL_X
, 0,
791 swizzle(tmp
, ONE
, Z
, Y
, X
),
792 i915_emit_const4fv(p
, cos_constants
), 0);
796 case TGSI_OPCODE_SGE
:
797 emit_simple_arith(p
, inst
, A0_SGE
, 2);
800 case TGSI_OPCODE_SLE
:
801 /* like SGE, but swap reg0, reg1 */
802 emit_simple_arith_swap2(p
, inst
, A0_SGE
, 2);
805 case TGSI_OPCODE_SIN
:
806 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
807 tmp
= i915_get_utemp(p
);
811 tmp
, A0_DEST_CHANNEL_X
, 0,
812 src0
, i915_emit_const1f(p
, 1.0f
/ (float) (M_PI
* 2.0)), 0);
814 i915_emit_arith(p
, A0_MOD
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
816 /* By choosing different taylor constants, could get rid of this mul:
820 tmp
, A0_DEST_CHANNEL_X
, 0,
821 tmp
, i915_emit_const1f(p
, (float) (M_PI
* 2.0)), 0);
824 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
825 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
826 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
827 * result = DP4 t1.wzyx, sin_constants
831 tmp
, A0_DEST_CHANNEL_XY
, 0,
832 swizzle(tmp
, X
, X
, ONE
, ONE
),
833 swizzle(tmp
, X
, ONE
, ONE
, ONE
), 0);
837 tmp
, A0_DEST_CHANNEL_ALL
, 0,
838 swizzle(tmp
, X
, Y
, X
, Y
),
839 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
843 tmp
, A0_DEST_CHANNEL_ALL
, 0,
844 swizzle(tmp
, X
, Y
, Y
, W
),
845 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
849 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
850 get_result_flags(inst
), 0,
851 swizzle(tmp
, W
, Z
, Y
, X
),
852 i915_emit_const4fv(p
, sin_constants
), 0);
855 case TGSI_OPCODE_SLT
:
856 emit_simple_arith(p
, inst
, A0_SLT
, 2);
859 case TGSI_OPCODE_SGT
:
860 /* like SLT, but swap reg0, reg1 */
861 emit_simple_arith_swap2(p
, inst
, A0_SLT
, 2);
864 case TGSI_OPCODE_SUB
:
865 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
866 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
870 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
871 get_result_flags(inst
), 0,
872 src0
, negate(src1
, 1, 1, 1, 1), 0);
875 case TGSI_OPCODE_TEX
:
876 emit_tex(p
, inst
, T0_TEXLD
);
879 case TGSI_OPCODE_TXB
:
880 emit_tex(p
, inst
, T0_TEXLDB
);
883 case TGSI_OPCODE_TXP
:
884 emit_tex(p
, inst
, T0_TEXLDP
);
887 case TGSI_OPCODE_XPD
:
889 * result.x = src0.y * src1.z - src0.z * src1.y;
890 * result.y = src0.z * src1.x - src0.x * src1.z;
891 * result.z = src0.x * src1.y - src0.y * src1.x;
894 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
895 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
896 tmp
= i915_get_utemp(p
);
900 tmp
, A0_DEST_CHANNEL_ALL
, 0,
901 swizzle(src0
, Z
, X
, Y
, ONE
),
902 swizzle(src1
, Y
, Z
, X
, ONE
), 0);
906 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
907 get_result_flags(inst
), 0,
908 swizzle(src0
, Y
, Z
, X
, ONE
),
909 swizzle(src1
, Z
, X
, Y
, ONE
),
910 negate(tmp
, 1, 1, 1, 0));
914 i915_program_error(p
, "bad opcode %d", inst
->Instruction
.Opcode
);
919 i915_release_utemps(p
);
924 * Translate TGSI fragment shader into i915 hardware instructions.
925 * \param p the translation state
926 * \param tokens the TGSI token array
929 i915_translate_instructions(struct i915_fp_compile
*p
,
930 const struct tgsi_token
*tokens
)
932 struct i915_fragment_shader
*ifs
= p
->shader
;
933 struct tgsi_parse_context parse
;
935 tgsi_parse_init( &parse
, tokens
);
937 while( !tgsi_parse_end_of_tokens( &parse
) ) {
939 tgsi_parse_token( &parse
);
941 switch( parse
.FullToken
.Token
.Type
) {
942 case TGSI_TOKEN_TYPE_DECLARATION
:
943 if (parse
.FullToken
.FullDeclaration
.Declaration
.File
944 == TGSI_FILE_CONSTANT
) {
946 for (i
= parse
.FullToken
.FullDeclaration
.DeclarationRange
.First
;
947 i
<= parse
.FullToken
.FullDeclaration
.DeclarationRange
.Last
;
949 assert(ifs
->constant_flags
[i
] == 0x0);
950 ifs
->constant_flags
[i
] = I915_CONSTFLAG_USER
;
951 ifs
->num_constants
= MAX2(ifs
->num_constants
, i
+ 1);
954 else if (parse
.FullToken
.FullDeclaration
.Declaration
.File
955 == TGSI_FILE_TEMPORARY
) {
957 for (i
= parse
.FullToken
.FullDeclaration
.DeclarationRange
.First
;
958 i
<= parse
.FullToken
.FullDeclaration
.DeclarationRange
.Last
;
960 assert(i
< I915_MAX_TEMPORARY
);
961 /* XXX just use shader->info->file_mask[TGSI_FILE_TEMPORARY] */
962 p
->temp_flag
|= (1 << i
); /* mark temp as used */
967 case TGSI_TOKEN_TYPE_IMMEDIATE
:
969 const struct tgsi_full_immediate
*imm
970 = &parse
.FullToken
.FullImmediate
;
971 const uint pos
= p
->num_immediates
++;
973 for (j
= 0; j
< imm
->Immediate
.Size
; j
++) {
974 p
->immediates
[pos
][j
] = imm
->u
.ImmediateFloat32
[j
].Float
;
979 case TGSI_TOKEN_TYPE_INSTRUCTION
:
980 if (p
->first_instruction
) {
981 /* resolve location of immediates */
983 for (i
= 0; i
< p
->num_immediates
; i
++) {
984 /* find constant slot for this immediate */
985 for (j
= 0; j
< I915_MAX_CONSTANT
; j
++) {
986 if (ifs
->constant_flags
[j
] == 0x0) {
987 memcpy(ifs
->constants
[j
],
990 /*printf("immediate %d maps to const %d\n", i, j);*/
991 ifs
->constant_flags
[j
] = 0xf; /* all four comps used */
992 p
->immediates_map
[i
] = j
;
993 ifs
->num_constants
= MAX2(ifs
->num_constants
, j
+ 1);
999 p
->first_instruction
= FALSE
;
1002 i915_translate_instruction(p
, &parse
.FullToken
.FullInstruction
);
1011 tgsi_parse_free (&parse
);
1015 static struct i915_fp_compile
*
1016 i915_init_compile(struct i915_context
*i915
,
1017 struct i915_fragment_shader
*ifs
)
1019 struct i915_fp_compile
*p
= CALLOC_STRUCT(i915_fp_compile
);
1023 /* Put new constants at end of const buffer, growing downward.
1024 * The problem is we don't know how many user-defined constants might
1025 * be specified with pipe->set_constant_buffer().
1026 * Should pre-scan the user's program to determine the highest-numbered
1027 * constant referenced.
1029 ifs
->num_constants
= 0;
1030 memset(ifs
->constant_flags
, 0, sizeof(ifs
->constant_flags
));
1032 p
->first_instruction
= TRUE
;
1034 p
->nr_tex_indirect
= 1; /* correct? */
1037 p
->nr_decl_insn
= 0;
1039 p
->csr
= p
->program
;
1040 p
->decl
= p
->declarations
;
1043 p
->temp_flag
= ~0x0 << I915_MAX_TEMPORARY
;
1044 p
->utemp_flag
= ~0x7;
1048 /* initialize the first program word */
1049 *(p
->decl
++) = _3DSTATE_PIXEL_SHADER_PROGRAM
;
1055 /* Copy compile results to the fragment program struct and destroy the
1056 * compilation context.
1059 i915_fini_compile(struct i915_context
*i915
, struct i915_fp_compile
*p
)
1061 struct i915_fragment_shader
*ifs
= p
->shader
;
1062 unsigned long program_size
= (unsigned long) (p
->csr
- p
->program
);
1063 unsigned long decl_size
= (unsigned long) (p
->decl
- p
->declarations
);
1065 if (p
->nr_tex_indirect
> I915_MAX_TEX_INDIRECT
)
1066 i915_program_error(p
, "Exceeded max nr indirect texture lookups");
1068 if (p
->nr_tex_insn
> I915_MAX_TEX_INSN
)
1069 i915_program_error(p
, "Exceeded max TEX instructions");
1071 if (p
->nr_alu_insn
> I915_MAX_ALU_INSN
)
1072 i915_program_error(p
, "Exceeded max ALU instructions");
1074 if (p
->nr_decl_insn
> I915_MAX_DECL_INSN
)
1075 i915_program_error(p
, "Exceeded max DECL instructions");
1078 p
->NumNativeInstructions
= 0;
1079 p
->NumNativeAluInstructions
= 0;
1080 p
->NumNativeTexInstructions
= 0;
1081 p
->NumNativeTexIndirections
= 0;
1083 i915_use_passthrough_shader(ifs
);
1086 p
->NumNativeInstructions
1087 = p
->nr_alu_insn
+ p
->nr_tex_insn
+ p
->nr_decl_insn
;
1088 p
->NumNativeAluInstructions
= p
->nr_alu_insn
;
1089 p
->NumNativeTexInstructions
= p
->nr_tex_insn
;
1090 p
->NumNativeTexIndirections
= p
->nr_tex_indirect
;
1092 /* patch in the program length */
1093 p
->declarations
[0] |= program_size
+ decl_size
- 2;
1095 /* Copy compilation results to fragment program struct:
1097 assert(!ifs
->program
);
1099 = (uint
*) MALLOC((program_size
+ decl_size
) * sizeof(uint
));
1101 ifs
->program_len
= program_size
+ decl_size
;
1103 memcpy(ifs
->program
,
1105 decl_size
* sizeof(uint
));
1107 memcpy(ifs
->program
+ decl_size
,
1109 program_size
* sizeof(uint
));
1113 /* Release the compilation struct:
1120 * Find an unused texture coordinate slot to use for fragment WPOS.
1121 * Update p->fp->wpos_tex with the result (-1 if no used texcoord slot is found).
1124 i915_find_wpos_space(struct i915_fp_compile
*p
)
1128 = p
->shader
->inputs_read
| (1 << TGSI_ATTRIB_POS
); /*XXX hack*/
1133 if (inputs
& (1 << TGSI_ATTRIB_POS
)) {
1134 for (i
= 0; i
< I915_TEX_UNITS
; i
++) {
1135 if ((inputs
& (1 << (TGSI_ATTRIB_TEX0
+ i
))) == 0) {
1141 i915_program_error(p
, "No free texcoord for wpos value");
1144 if (p
->shader
->info
.input_semantic_name
[0] == TGSI_SEMANTIC_POSITION
) {
1145 /* frag shader using the fragment position input */
1157 * Rather than trying to intercept and jiggle depth writes during
1158 * emit, just move the value into its correct position at the end of
1162 i915_fixup_depth_write(struct i915_fp_compile
*p
)
1164 /* XXX assuming pos/depth is always in output[0] */
1165 if (p
->shader
->info
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
) {
1166 const uint depth
= UREG(REG_TYPE_OD
, 0);
1169 A0_MOV
, /* opcode */
1170 depth
, /* dest reg */
1171 A0_DEST_CHANNEL_W
, /* write mask */
1173 swizzle(depth
, X
, Y
, Z
, Z
), /* src0 */
1174 0, 0 /* src1, src2 */);
1180 i915_translate_fragment_program( struct i915_context
*i915
,
1181 struct i915_fragment_shader
*fs
)
1183 struct i915_fp_compile
*p
= i915_init_compile(i915
, fs
);
1184 const struct tgsi_token
*tokens
= fs
->state
.tokens
;
1186 i915_find_wpos_space(p
);
1189 tgsi_dump(tokens
, 0);
1192 i915_translate_instructions(p
, tokens
);
1193 i915_fixup_depth_write(p
);
1195 i915_fini_compile(i915
, p
);