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 /* kill if src[0].x < 0 || src[0].y < 0 ... */
564 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
565 tmp
= i915_get_utemp(p
);
568 tmp
, /* dest reg: a dummy reg */
569 A0_DEST_CHANNEL_ALL
, /* dest writemask */
572 T0_TEXKILL
); /* opcode */
575 case TGSI_OPCODE_KILP
:
576 assert(0); /* not tested yet */
579 case TGSI_OPCODE_LG2
:
580 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
584 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
585 get_result_flags(inst
), 0,
586 swizzle(src0
, X
, X
, X
, X
), 0, 0);
589 case TGSI_OPCODE_LIT
:
590 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
591 tmp
= i915_get_utemp(p
);
593 /* tmp = max( a.xyzw, a.00zw )
594 * XXX: Clamp tmp.w to -128..128
596 * tmp.y = tmp.w * tmp.y
598 * result = cmp (a.11-x1, a.1x01, a.1xy1 )
600 i915_emit_arith(p
, A0_MAX
, tmp
, A0_DEST_CHANNEL_ALL
, 0,
601 src0
, swizzle(src0
, ZERO
, ZERO
, Z
, W
), 0);
603 i915_emit_arith(p
, A0_LOG
, tmp
, A0_DEST_CHANNEL_Y
, 0,
604 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
606 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_Y
, 0,
607 swizzle(tmp
, ZERO
, Y
, ZERO
, ZERO
),
608 swizzle(tmp
, ZERO
, W
, ZERO
, ZERO
), 0);
610 i915_emit_arith(p
, A0_EXP
, tmp
, A0_DEST_CHANNEL_Y
, 0,
611 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
613 i915_emit_arith(p
, A0_CMP
,
614 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
615 get_result_flags(inst
), 0,
616 negate(swizzle(tmp
, ONE
, ONE
, X
, ONE
), 0, 0, 1, 0),
617 swizzle(tmp
, ONE
, X
, ZERO
, ONE
),
618 swizzle(tmp
, ONE
, X
, Y
, ONE
));
622 case TGSI_OPCODE_LRP
:
623 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
624 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
625 src2
= src_vector(p
, &inst
->FullSrcRegisters
[2]);
626 flags
= get_result_flags(inst
);
627 tmp
= i915_get_utemp(p
);
634 * result = (-c)*a + tmp
636 i915_emit_arith(p
, A0_MAD
, tmp
,
637 flags
& A0_DEST_CHANNEL_ALL
, 0, src1
, src0
, src2
);
639 i915_emit_arith(p
, A0_MAD
,
640 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
641 flags
, 0, negate(src2
, 1, 1, 1, 1), src0
, tmp
);
644 case TGSI_OPCODE_MAD
:
645 emit_simple_arith(p
, inst
, A0_MAD
, 3);
648 case TGSI_OPCODE_MAX
:
649 emit_simple_arith(p
, inst
, A0_MAX
, 2);
652 case TGSI_OPCODE_MIN
:
653 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
654 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
655 tmp
= i915_get_utemp(p
);
656 flags
= get_result_flags(inst
);
660 tmp
, flags
& A0_DEST_CHANNEL_ALL
, 0,
661 negate(src0
, 1, 1, 1, 1),
662 negate(src1
, 1, 1, 1, 1), 0);
666 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
667 flags
, 0, negate(tmp
, 1, 1, 1, 1), 0, 0);
670 case TGSI_OPCODE_MOV
:
671 case TGSI_OPCODE_SWZ
:
672 emit_simple_arith(p
, inst
, A0_MOV
, 1);
675 case TGSI_OPCODE_MUL
:
676 emit_simple_arith(p
, inst
, A0_MUL
, 2);
679 case TGSI_OPCODE_POW
:
680 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
681 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
682 tmp
= i915_get_utemp(p
);
683 flags
= get_result_flags(inst
);
685 /* XXX: masking on intermediate values, here and elsewhere.
689 tmp
, A0_DEST_CHANNEL_X
, 0,
690 swizzle(src0
, X
, X
, X
, X
), 0, 0);
692 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, src1
, 0);
696 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
697 flags
, 0, swizzle(tmp
, X
, X
, X
, X
), 0, 0);
700 case TGSI_OPCODE_RET
:
704 case TGSI_OPCODE_RCP
:
705 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
709 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
710 get_result_flags(inst
), 0,
711 swizzle(src0
, X
, X
, X
, X
), 0, 0);
714 case TGSI_OPCODE_RSQ
:
715 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
719 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
720 get_result_flags(inst
), 0,
721 swizzle(src0
, X
, X
, X
, X
), 0, 0);
724 case TGSI_OPCODE_SCS
:
725 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
726 tmp
= i915_get_utemp(p
);
729 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
730 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
731 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
732 * scs.x = DP4 t1, sin_constants
733 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
734 * scs.y = DP4 t1, cos_constants
738 tmp
, A0_DEST_CHANNEL_XY
, 0,
739 swizzle(src0
, X
, X
, ONE
, ONE
),
740 swizzle(src0
, X
, ONE
, ONE
, ONE
), 0);
744 tmp
, A0_DEST_CHANNEL_ALL
, 0,
745 swizzle(tmp
, X
, Y
, X
, Y
),
746 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
748 writemask
= inst
->FullDstRegisters
[0].DstRegister
.WriteMask
;
750 if (writemask
& TGSI_WRITEMASK_Y
) {
753 if (writemask
& TGSI_WRITEMASK_X
)
754 tmp1
= i915_get_utemp(p
);
760 tmp1
, A0_DEST_CHANNEL_ALL
, 0,
761 swizzle(tmp
, X
, Y
, Y
, W
),
762 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
766 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
767 A0_DEST_CHANNEL_Y
, 0,
768 swizzle(tmp1
, W
, Z
, Y
, X
),
769 i915_emit_const4fv(p
, sin_constants
), 0);
772 if (writemask
& TGSI_WRITEMASK_X
) {
775 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
776 swizzle(tmp
, X
, X
, Z
, ONE
),
777 swizzle(tmp
, Z
, ONE
, ONE
, ONE
), 0);
781 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
782 A0_DEST_CHANNEL_X
, 0,
783 swizzle(tmp
, ONE
, Z
, Y
, X
),
784 i915_emit_const4fv(p
, cos_constants
), 0);
788 case TGSI_OPCODE_SGE
:
789 emit_simple_arith(p
, inst
, A0_SGE
, 2);
792 case TGSI_OPCODE_SLE
:
793 /* like SGE, but swap reg0, reg1 */
794 emit_simple_arith_swap2(p
, inst
, A0_SGE
, 2);
797 case TGSI_OPCODE_SIN
:
798 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
799 tmp
= i915_get_utemp(p
);
803 tmp
, A0_DEST_CHANNEL_X
, 0,
804 src0
, i915_emit_const1f(p
, 1.0f
/ (float) (M_PI
* 2.0)), 0);
806 i915_emit_arith(p
, A0_MOD
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
808 /* By choosing different taylor constants, could get rid of this mul:
812 tmp
, A0_DEST_CHANNEL_X
, 0,
813 tmp
, i915_emit_const1f(p
, (float) (M_PI
* 2.0)), 0);
816 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
817 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
818 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
819 * result = DP4 t1.wzyx, sin_constants
823 tmp
, A0_DEST_CHANNEL_XY
, 0,
824 swizzle(tmp
, X
, X
, ONE
, ONE
),
825 swizzle(tmp
, X
, ONE
, ONE
, ONE
), 0);
829 tmp
, A0_DEST_CHANNEL_ALL
, 0,
830 swizzle(tmp
, X
, Y
, X
, Y
),
831 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
835 tmp
, A0_DEST_CHANNEL_ALL
, 0,
836 swizzle(tmp
, X
, Y
, Y
, W
),
837 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
841 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
842 get_result_flags(inst
), 0,
843 swizzle(tmp
, W
, Z
, Y
, X
),
844 i915_emit_const4fv(p
, sin_constants
), 0);
847 case TGSI_OPCODE_SLT
:
848 emit_simple_arith(p
, inst
, A0_SLT
, 2);
851 case TGSI_OPCODE_SGT
:
852 /* like SLT, but swap reg0, reg1 */
853 emit_simple_arith_swap2(p
, inst
, A0_SLT
, 2);
856 case TGSI_OPCODE_SUB
:
857 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
858 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
862 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
863 get_result_flags(inst
), 0,
864 src0
, negate(src1
, 1, 1, 1, 1), 0);
867 case TGSI_OPCODE_TEX
:
868 emit_tex(p
, inst
, T0_TEXLD
);
871 case TGSI_OPCODE_TXB
:
872 emit_tex(p
, inst
, T0_TEXLDB
);
875 case TGSI_OPCODE_TXP
:
876 emit_tex(p
, inst
, T0_TEXLDP
);
879 case TGSI_OPCODE_XPD
:
881 * result.x = src0.y * src1.z - src0.z * src1.y;
882 * result.y = src0.z * src1.x - src0.x * src1.z;
883 * result.z = src0.x * src1.y - src0.y * src1.x;
886 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
887 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
888 tmp
= i915_get_utemp(p
);
892 tmp
, A0_DEST_CHANNEL_ALL
, 0,
893 swizzle(src0
, Z
, X
, Y
, ONE
),
894 swizzle(src1
, Y
, Z
, X
, ONE
), 0);
898 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
899 get_result_flags(inst
), 0,
900 swizzle(src0
, Y
, Z
, X
, ONE
),
901 swizzle(src1
, Z
, X
, Y
, ONE
),
902 negate(tmp
, 1, 1, 1, 0));
906 i915_program_error(p
, "bad opcode %d", inst
->Instruction
.Opcode
);
911 i915_release_utemps(p
);
916 * Translate TGSI fragment shader into i915 hardware instructions.
917 * \param p the translation state
918 * \param tokens the TGSI token array
921 i915_translate_instructions(struct i915_fp_compile
*p
,
922 const struct tgsi_token
*tokens
)
924 struct i915_fragment_shader
*ifs
= p
->shader
;
925 struct tgsi_parse_context parse
;
927 tgsi_parse_init( &parse
, tokens
);
929 while( !tgsi_parse_end_of_tokens( &parse
) ) {
931 tgsi_parse_token( &parse
);
933 switch( parse
.FullToken
.Token
.Type
) {
934 case TGSI_TOKEN_TYPE_DECLARATION
:
935 if (parse
.FullToken
.FullDeclaration
.Declaration
.File
936 == TGSI_FILE_CONSTANT
) {
938 for (i
= parse
.FullToken
.FullDeclaration
.DeclarationRange
.First
;
939 i
<= parse
.FullToken
.FullDeclaration
.DeclarationRange
.Last
;
941 assert(ifs
->constant_flags
[i
] == 0x0);
942 ifs
->constant_flags
[i
] = I915_CONSTFLAG_USER
;
943 ifs
->num_constants
= MAX2(ifs
->num_constants
, i
+ 1);
946 else if (parse
.FullToken
.FullDeclaration
.Declaration
.File
947 == TGSI_FILE_TEMPORARY
) {
949 for (i
= parse
.FullToken
.FullDeclaration
.DeclarationRange
.First
;
950 i
<= parse
.FullToken
.FullDeclaration
.DeclarationRange
.Last
;
952 assert(i
< I915_MAX_TEMPORARY
);
953 /* XXX just use shader->info->file_mask[TGSI_FILE_TEMPORARY] */
954 p
->temp_flag
|= (1 << i
); /* mark temp as used */
959 case TGSI_TOKEN_TYPE_IMMEDIATE
:
961 const struct tgsi_full_immediate
*imm
962 = &parse
.FullToken
.FullImmediate
;
963 const uint pos
= p
->num_immediates
++;
965 for (j
= 0; j
< imm
->Immediate
.Size
; j
++) {
966 p
->immediates
[pos
][j
] = imm
->u
.ImmediateFloat32
[j
].Float
;
971 case TGSI_TOKEN_TYPE_INSTRUCTION
:
972 if (p
->first_instruction
) {
973 /* resolve location of immediates */
975 for (i
= 0; i
< p
->num_immediates
; i
++) {
976 /* find constant slot for this immediate */
977 for (j
= 0; j
< I915_MAX_CONSTANT
; j
++) {
978 if (ifs
->constant_flags
[j
] == 0x0) {
979 memcpy(ifs
->constants
[j
],
982 /*printf("immediate %d maps to const %d\n", i, j);*/
983 ifs
->constant_flags
[j
] = 0xf; /* all four comps used */
984 p
->immediates_map
[i
] = j
;
985 ifs
->num_constants
= MAX2(ifs
->num_constants
, j
+ 1);
991 p
->first_instruction
= FALSE
;
994 i915_translate_instruction(p
, &parse
.FullToken
.FullInstruction
);
1003 tgsi_parse_free (&parse
);
1007 static struct i915_fp_compile
*
1008 i915_init_compile(struct i915_context
*i915
,
1009 struct i915_fragment_shader
*ifs
)
1011 struct i915_fp_compile
*p
= CALLOC_STRUCT(i915_fp_compile
);
1015 /* Put new constants at end of const buffer, growing downward.
1016 * The problem is we don't know how many user-defined constants might
1017 * be specified with pipe->set_constant_buffer().
1018 * Should pre-scan the user's program to determine the highest-numbered
1019 * constant referenced.
1021 ifs
->num_constants
= 0;
1022 memset(ifs
->constant_flags
, 0, sizeof(ifs
->constant_flags
));
1024 p
->first_instruction
= TRUE
;
1026 p
->nr_tex_indirect
= 1; /* correct? */
1029 p
->nr_decl_insn
= 0;
1031 p
->csr
= p
->program
;
1032 p
->decl
= p
->declarations
;
1035 p
->temp_flag
= ~0x0 << I915_MAX_TEMPORARY
;
1036 p
->utemp_flag
= ~0x7;
1040 /* initialize the first program word */
1041 *(p
->decl
++) = _3DSTATE_PIXEL_SHADER_PROGRAM
;
1047 /* Copy compile results to the fragment program struct and destroy the
1048 * compilation context.
1051 i915_fini_compile(struct i915_context
*i915
, struct i915_fp_compile
*p
)
1053 struct i915_fragment_shader
*ifs
= p
->shader
;
1054 unsigned long program_size
= (unsigned long) (p
->csr
- p
->program
);
1055 unsigned long decl_size
= (unsigned long) (p
->decl
- p
->declarations
);
1057 if (p
->nr_tex_indirect
> I915_MAX_TEX_INDIRECT
)
1058 i915_program_error(p
, "Exceeded max nr indirect texture lookups");
1060 if (p
->nr_tex_insn
> I915_MAX_TEX_INSN
)
1061 i915_program_error(p
, "Exceeded max TEX instructions");
1063 if (p
->nr_alu_insn
> I915_MAX_ALU_INSN
)
1064 i915_program_error(p
, "Exceeded max ALU instructions");
1066 if (p
->nr_decl_insn
> I915_MAX_DECL_INSN
)
1067 i915_program_error(p
, "Exceeded max DECL instructions");
1070 p
->NumNativeInstructions
= 0;
1071 p
->NumNativeAluInstructions
= 0;
1072 p
->NumNativeTexInstructions
= 0;
1073 p
->NumNativeTexIndirections
= 0;
1075 i915_use_passthrough_shader(ifs
);
1078 p
->NumNativeInstructions
1079 = p
->nr_alu_insn
+ p
->nr_tex_insn
+ p
->nr_decl_insn
;
1080 p
->NumNativeAluInstructions
= p
->nr_alu_insn
;
1081 p
->NumNativeTexInstructions
= p
->nr_tex_insn
;
1082 p
->NumNativeTexIndirections
= p
->nr_tex_indirect
;
1084 /* patch in the program length */
1085 p
->declarations
[0] |= program_size
+ decl_size
- 2;
1087 /* Copy compilation results to fragment program struct:
1089 assert(!ifs
->program
);
1091 = (uint
*) MALLOC((program_size
+ decl_size
) * sizeof(uint
));
1093 ifs
->program_len
= program_size
+ decl_size
;
1095 memcpy(ifs
->program
,
1097 decl_size
* sizeof(uint
));
1099 memcpy(ifs
->program
+ decl_size
,
1101 program_size
* sizeof(uint
));
1105 /* Release the compilation struct:
1112 * Find an unused texture coordinate slot to use for fragment WPOS.
1113 * Update p->fp->wpos_tex with the result (-1 if no used texcoord slot is found).
1116 i915_find_wpos_space(struct i915_fp_compile
*p
)
1120 = p
->shader
->inputs_read
| (1 << TGSI_ATTRIB_POS
); /*XXX hack*/
1125 if (inputs
& (1 << TGSI_ATTRIB_POS
)) {
1126 for (i
= 0; i
< I915_TEX_UNITS
; i
++) {
1127 if ((inputs
& (1 << (TGSI_ATTRIB_TEX0
+ i
))) == 0) {
1133 i915_program_error(p
, "No free texcoord for wpos value");
1136 if (p
->shader
->info
.input_semantic_name
[0] == TGSI_SEMANTIC_POSITION
) {
1137 /* frag shader using the fragment position input */
1149 * Rather than trying to intercept and jiggle depth writes during
1150 * emit, just move the value into its correct position at the end of
1154 i915_fixup_depth_write(struct i915_fp_compile
*p
)
1156 /* XXX assuming pos/depth is always in output[0] */
1157 if (p
->shader
->info
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
) {
1158 const uint depth
= UREG(REG_TYPE_OD
, 0);
1161 A0_MOV
, /* opcode */
1162 depth
, /* dest reg */
1163 A0_DEST_CHANNEL_W
, /* write mask */
1165 swizzle(depth
, X
, Y
, Z
, Z
), /* src0 */
1166 0, 0 /* src1, src2 */);
1172 i915_translate_fragment_program( struct i915_context
*i915
,
1173 struct i915_fragment_shader
*fs
)
1175 struct i915_fp_compile
*p
= i915_init_compile(i915
, fs
);
1176 const struct tgsi_token
*tokens
= fs
->state
.tokens
;
1178 i915_find_wpos_space(p
);
1181 tgsi_dump(tokens
, 0);
1184 i915_translate_instructions(p
, tokens
);
1185 i915_fixup_depth_write(p
);
1187 i915_fini_compile(i915
, p
);