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 "tgsi/util/tgsi_parse.h"
37 #include "tgsi/util/tgsi_dump.h"
39 #include "draw/draw_vertex.h"
43 * Simple pass-through fragment shader to use when we don't have
44 * a real shader (or it fails to compile for some reason).
46 static unsigned passthrough
[] =
48 _3DSTATE_PIXEL_SHADER_PROGRAM
| ((2*3)-1),
50 /* declare input color:
53 (REG_TYPE_T
<< D0_TYPE_SHIFT
) |
54 (T_DIFFUSE
<< D0_NR_SHIFT
) |
59 /* move to output color:
62 (REG_TYPE_OC
<< A0_DEST_TYPE_SHIFT
) |
64 (REG_TYPE_T
<< A0_SRC0_TYPE_SHIFT
) |
65 (T_DIFFUSE
<< A0_SRC0_NR_SHIFT
)),
66 0x01230000, /* .xyzw */
71 /* 1, -1/3!, 1/5!, -1/7! */
72 static const float sin_constants
[4] = { 1.0,
74 1.0f
/ (5 * 4 * 3 * 2 * 1),
75 -1.0f
/ (7 * 6 * 5 * 4 * 3 * 2 * 1)
78 /* 1, -1/2!, 1/4!, -1/6! */
79 static const float cos_constants
[4] = { 1.0,
81 1.0f
/ (4 * 3 * 2 * 1),
82 -1.0f
/ (6 * 5 * 4 * 3 * 2 * 1)
88 * component-wise negation of ureg
91 negate(int reg
, int x
, int y
, int z
, int w
)
93 /* Another neat thing about the UREG representation */
94 return reg
^ (((x
& 1) << UREG_CHANNEL_X_NEGATE_SHIFT
) |
95 ((y
& 1) << UREG_CHANNEL_Y_NEGATE_SHIFT
) |
96 ((z
& 1) << UREG_CHANNEL_Z_NEGATE_SHIFT
) |
97 ((w
& 1) << UREG_CHANNEL_W_NEGATE_SHIFT
));
102 * In the event of a translation failure, we'll generate a simple color
103 * pass-through program.
106 i915_use_passthrough_shader(struct i915_fragment_shader
*fs
)
108 fs
->program
= (uint
*) MALLOC(sizeof(passthrough
));
110 memcpy(fs
->program
, passthrough
, sizeof(passthrough
));
111 fs
->program_len
= Elements(passthrough
);
113 fs
->num_constants
= 0;
118 i915_program_error(struct i915_fp_compile
*p
, const char *msg
, ...)
123 debug_printf("i915_program_error: ");
124 va_start( args
, msg
);
125 vsprintf( buffer
, msg
, args
);
127 debug_printf(buffer
);
136 * Construct a ureg for the given source register. Will emit
137 * constants, apply swizzling and negation as needed.
140 src_vector(struct i915_fp_compile
*p
,
141 const struct tgsi_full_src_register
*source
)
143 uint index
= source
->SrcRegister
.Index
;
144 uint src
, sem_name
, sem_ind
;
146 switch (source
->SrcRegister
.File
) {
147 case TGSI_FILE_TEMPORARY
:
148 if (source
->SrcRegister
.Index
>= I915_MAX_TEMPORARY
) {
149 i915_program_error(p
, "Exceeded max temporary reg");
152 src
= UREG(REG_TYPE_R
, index
);
154 case TGSI_FILE_INPUT
:
155 /* XXX: Packing COL1, FOGC into a single attribute works for
156 * texenv programs, but will fail for real fragment programs
157 * that use these attributes and expect them to be a full 4
158 * components wide. Could use a texcoord to pass these
159 * attributes if necessary, but that won't work in the general
162 * We also use a texture coordinate to pass wpos when possible.
165 sem_name
= p
->shader
->info
.input_semantic_name
[index
];
166 sem_ind
= p
->shader
->info
.input_semantic_index
[index
];
169 case TGSI_SEMANTIC_POSITION
:
170 debug_printf("SKIP SEM POS\n");
172 assert(p->wpos_tex != -1);
173 src = i915_emit_decl(p, REG_TYPE_T, p->wpos_tex, D0_CHANNEL_ALL);
176 case TGSI_SEMANTIC_COLOR
:
178 src
= i915_emit_decl(p
, REG_TYPE_T
, T_DIFFUSE
, D0_CHANNEL_ALL
);
181 /* secondary color */
182 assert(sem_ind
== 1);
183 src
= i915_emit_decl(p
, REG_TYPE_T
, T_SPECULAR
, D0_CHANNEL_XYZ
);
184 src
= swizzle(src
, X
, Y
, Z
, ONE
);
187 case TGSI_SEMANTIC_FOG
:
188 src
= i915_emit_decl(p
, REG_TYPE_T
, T_FOG_W
, D0_CHANNEL_W
);
189 src
= swizzle(src
, W
, W
, W
, W
);
191 case TGSI_SEMANTIC_GENERIC
:
192 /* usually a texcoord */
193 src
= i915_emit_decl(p
, REG_TYPE_T
, T_TEX0
+ sem_ind
, D0_CHANNEL_ALL
);
196 i915_program_error(p
, "Bad source->Index");
201 case TGSI_FILE_IMMEDIATE
:
202 assert(index
< p
->num_immediates
);
203 index
= p
->immediates_map
[index
];
205 case TGSI_FILE_CONSTANT
:
206 src
= UREG(REG_TYPE_CONST
, index
);
210 i915_program_error(p
, "Bad source->File");
214 if (source
->SrcRegister
.Extended
) {
216 source
->SrcRegisterExtSwz
.ExtSwizzleX
,
217 source
->SrcRegisterExtSwz
.ExtSwizzleY
,
218 source
->SrcRegisterExtSwz
.ExtSwizzleZ
,
219 source
->SrcRegisterExtSwz
.ExtSwizzleW
);
223 source
->SrcRegister
.SwizzleX
,
224 source
->SrcRegister
.SwizzleY
,
225 source
->SrcRegister
.SwizzleZ
,
226 source
->SrcRegister
.SwizzleW
);
230 /* There's both negate-all-components and per-component negation.
231 * Try to handle both here.
234 int nx
= source
->SrcRegisterExtSwz
.NegateX
;
235 int ny
= source
->SrcRegisterExtSwz
.NegateY
;
236 int nz
= source
->SrcRegisterExtSwz
.NegateZ
;
237 int nw
= source
->SrcRegisterExtSwz
.NegateW
;
238 if (source
->SrcRegister
.Negate
) {
244 src
= negate(src
, nx
, ny
, nz
, nw
);
247 /* no abs() or post-abs negation */
249 /* XXX assertions disabled to allow arbfplight.c to run */
250 /* XXX enable these assertions, or fix things */
251 assert(!source
->SrcRegisterExtMod
.Absolute
);
252 assert(!source
->SrcRegisterExtMod
.Negate
);
259 * Construct a ureg for a destination register.
262 get_result_vector(struct i915_fp_compile
*p
,
263 const struct tgsi_full_dst_register
*dest
)
265 switch (dest
->DstRegister
.File
) {
266 case TGSI_FILE_OUTPUT
:
268 uint sem_name
= p
->shader
->info
.output_semantic_name
[dest
->DstRegister
.Index
];
270 case TGSI_SEMANTIC_POSITION
:
271 return UREG(REG_TYPE_OD
, 0);
272 case TGSI_SEMANTIC_COLOR
:
273 return UREG(REG_TYPE_OC
, 0);
275 i915_program_error(p
, "Bad inst->DstReg.Index/semantics");
279 case TGSI_FILE_TEMPORARY
:
280 return UREG(REG_TYPE_R
, dest
->DstRegister
.Index
);
282 i915_program_error(p
, "Bad inst->DstReg.File");
289 * Compute flags for saturation and writemask.
292 get_result_flags(const struct tgsi_full_instruction
*inst
)
295 = inst
->FullDstRegisters
[0].DstRegister
.WriteMask
;
298 if (inst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
)
299 flags
|= A0_DEST_SATURATE
;
301 if (writeMask
& TGSI_WRITEMASK_X
)
302 flags
|= A0_DEST_CHANNEL_X
;
303 if (writeMask
& TGSI_WRITEMASK_Y
)
304 flags
|= A0_DEST_CHANNEL_Y
;
305 if (writeMask
& TGSI_WRITEMASK_Z
)
306 flags
|= A0_DEST_CHANNEL_Z
;
307 if (writeMask
& TGSI_WRITEMASK_W
)
308 flags
|= A0_DEST_CHANNEL_W
;
315 * Convert TGSI_TEXTURE_x token to DO_SAMPLE_TYPE_x token
318 translate_tex_src_target(struct i915_fp_compile
*p
, uint tex
)
321 case TGSI_TEXTURE_1D
:
322 return D0_SAMPLE_TYPE_2D
;
323 case TGSI_TEXTURE_2D
:
324 return D0_SAMPLE_TYPE_2D
;
325 case TGSI_TEXTURE_RECT
:
326 return D0_SAMPLE_TYPE_2D
;
327 case TGSI_TEXTURE_3D
:
328 return D0_SAMPLE_TYPE_VOLUME
;
329 case TGSI_TEXTURE_CUBE
:
330 return D0_SAMPLE_TYPE_CUBE
;
332 i915_program_error(p
, "TexSrc type");
339 * Generate texel lookup instruction.
342 emit_tex(struct i915_fp_compile
*p
,
343 const struct tgsi_full_instruction
*inst
,
346 uint texture
= inst
->InstructionExtTexture
.Texture
;
347 uint unit
= inst
->FullSrcRegisters
[1].SrcRegister
.Index
;
348 uint tex
= translate_tex_src_target( p
, texture
);
349 uint sampler
= i915_emit_decl(p
, REG_TYPE_S
, unit
, tex
);
350 uint coord
= src_vector( p
, &inst
->FullSrcRegisters
[0]);
353 get_result_vector( p
, &inst
->FullDstRegisters
[0] ),
354 get_result_flags( inst
),
362 * Generate a simple arithmetic instruction
363 * \param opcode the i915 opcode
364 * \param numArgs the number of input/src arguments
367 emit_simple_arith(struct i915_fp_compile
*p
,
368 const struct tgsi_full_instruction
*inst
,
369 uint opcode
, uint numArgs
)
371 uint arg1
, arg2
, arg3
;
373 assert(numArgs
<= 3);
375 arg1
= (numArgs
< 1) ? 0 : src_vector( p
, &inst
->FullSrcRegisters
[0] );
376 arg2
= (numArgs
< 2) ? 0 : src_vector( p
, &inst
->FullSrcRegisters
[1] );
377 arg3
= (numArgs
< 3) ? 0 : src_vector( p
, &inst
->FullSrcRegisters
[2] );
381 get_result_vector( p
, &inst
->FullDstRegisters
[0]),
382 get_result_flags( inst
), 0,
389 /** As above, but swap the first two src regs */
391 emit_simple_arith_swap2(struct i915_fp_compile
*p
,
392 const struct tgsi_full_instruction
*inst
,
393 uint opcode
, uint numArgs
)
395 struct tgsi_full_instruction inst2
;
397 assert(numArgs
== 2);
399 /* transpose first two registers */
401 inst2
.FullSrcRegisters
[0] = inst
->FullSrcRegisters
[1];
402 inst2
.FullSrcRegisters
[1] = inst
->FullSrcRegisters
[0];
404 emit_simple_arith(p
, &inst2
, opcode
, numArgs
);
409 #define M_PI 3.14159265358979323846
413 * Translate TGSI instruction to i915 instruction.
417 * SIN, COS -- could use another taylor step?
418 * LIT -- results seem a little different to sw mesa
419 * LOG -- different to mesa on negative numbers, but this is conformant.
422 i915_translate_instruction(struct i915_fp_compile
*p
,
423 const struct tgsi_full_instruction
*inst
)
426 uint src0
, src1
, src2
, flags
;
429 switch (inst
->Instruction
.Opcode
) {
430 case TGSI_OPCODE_ABS
:
431 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
434 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
435 get_result_flags(inst
), 0,
436 src0
, negate(src0
, 1, 1, 1, 1), 0);
439 case TGSI_OPCODE_ADD
:
440 emit_simple_arith(p
, inst
, A0_ADD
, 2);
443 case TGSI_OPCODE_CMP
:
444 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
445 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
446 src2
= src_vector(p
, &inst
->FullSrcRegisters
[2]);
447 i915_emit_arith(p
, A0_CMP
,
448 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
449 get_result_flags(inst
),
450 0, src0
, src2
, src1
); /* NOTE: order of src2, src1 */
453 case TGSI_OPCODE_COS
:
454 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
455 tmp
= i915_get_utemp(p
);
459 tmp
, A0_DEST_CHANNEL_X
, 0,
460 src0
, i915_emit_const1f(p
, 1.0f
/ (float) (M_PI
* 2.0)), 0);
462 i915_emit_arith(p
, A0_MOD
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
464 /* By choosing different taylor constants, could get rid of this mul:
468 tmp
, A0_DEST_CHANNEL_X
, 0,
469 tmp
, i915_emit_const1f(p
, (float) (M_PI
* 2.0)), 0);
472 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
473 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, 1
474 * t0 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
475 * result = DP4 t0, cos_constants
479 tmp
, A0_DEST_CHANNEL_XY
, 0,
480 swizzle(tmp
, X
, X
, ONE
, ONE
),
481 swizzle(tmp
, X
, ONE
, ONE
, ONE
), 0);
485 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
486 swizzle(tmp
, X
, Y
, X
, ONE
),
487 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
491 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
492 swizzle(tmp
, X
, X
, Z
, ONE
),
493 swizzle(tmp
, Z
, ONE
, ONE
, ONE
), 0);
497 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
498 get_result_flags(inst
), 0,
499 swizzle(tmp
, ONE
, Z
, Y
, X
),
500 i915_emit_const4fv(p
, cos_constants
), 0);
503 case TGSI_OPCODE_DP3
:
504 emit_simple_arith(p
, inst
, A0_DP3
, 2);
507 case TGSI_OPCODE_DP4
:
508 emit_simple_arith(p
, inst
, A0_DP4
, 2);
511 case TGSI_OPCODE_DPH
:
512 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
513 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
517 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
518 get_result_flags(inst
), 0,
519 swizzle(src0
, X
, Y
, Z
, ONE
), src1
, 0);
522 case TGSI_OPCODE_DST
:
523 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
524 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
526 /* result[0] = 1 * 1;
527 * result[1] = a[1] * b[1];
528 * result[2] = a[2] * 1;
529 * result[3] = 1 * b[3];
533 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
534 get_result_flags(inst
), 0,
535 swizzle(src0
, ONE
, Y
, Z
, ONE
),
536 swizzle(src1
, ONE
, Y
, ONE
, W
), 0);
539 case TGSI_OPCODE_END
:
543 case TGSI_OPCODE_EX2
:
544 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
548 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
549 get_result_flags(inst
), 0,
550 swizzle(src0
, X
, X
, X
, X
), 0, 0);
553 case TGSI_OPCODE_FLR
:
554 emit_simple_arith(p
, inst
, A0_FLR
, 1);
557 case TGSI_OPCODE_FRC
:
558 emit_simple_arith(p
, inst
, A0_FRC
, 1);
561 case TGSI_OPCODE_KIL
:
562 /* unconditional kill */
563 assert(0); /* not tested yet */
565 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
566 tmp
= i915_get_utemp(p
);
568 i915_emit_texld(p
, tmp
, A0_DEST_CHANNEL_ALL
, /* use a dummy dest reg */
569 0, src0
, T0_TEXKILL
);
573 case TGSI_OPCODE_KILP
:
574 /* kill if src[0].x < 0 || src[0].y < 0 ... */
575 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
576 tmp
= i915_get_utemp(p
);
579 tmp
, /* dest reg: a dummy reg */
580 A0_DEST_CHANNEL_ALL
, /* dest writemask */
583 T0_TEXKILL
); /* opcode */
586 case TGSI_OPCODE_LG2
:
587 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
591 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
592 get_result_flags(inst
), 0,
593 swizzle(src0
, X
, X
, X
, X
), 0, 0);
596 case TGSI_OPCODE_LIT
:
597 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
598 tmp
= i915_get_utemp(p
);
600 /* tmp = max( a.xyzw, a.00zw )
601 * XXX: Clamp tmp.w to -128..128
603 * tmp.y = tmp.w * tmp.y
605 * result = cmp (a.11-x1, a.1x01, a.1xy1 )
607 i915_emit_arith(p
, A0_MAX
, tmp
, A0_DEST_CHANNEL_ALL
, 0,
608 src0
, swizzle(src0
, ZERO
, ZERO
, Z
, W
), 0);
610 i915_emit_arith(p
, A0_LOG
, tmp
, A0_DEST_CHANNEL_Y
, 0,
611 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
613 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_Y
, 0,
614 swizzle(tmp
, ZERO
, Y
, ZERO
, ZERO
),
615 swizzle(tmp
, ZERO
, W
, ZERO
, ZERO
), 0);
617 i915_emit_arith(p
, A0_EXP
, tmp
, A0_DEST_CHANNEL_Y
, 0,
618 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
620 i915_emit_arith(p
, A0_CMP
,
621 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
622 get_result_flags(inst
), 0,
623 negate(swizzle(tmp
, ONE
, ONE
, X
, ONE
), 0, 0, 1, 0),
624 swizzle(tmp
, ONE
, X
, ZERO
, ONE
),
625 swizzle(tmp
, ONE
, X
, Y
, ONE
));
629 case TGSI_OPCODE_LRP
:
630 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
631 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
632 src2
= src_vector(p
, &inst
->FullSrcRegisters
[2]);
633 flags
= get_result_flags(inst
);
634 tmp
= i915_get_utemp(p
);
641 * result = (-c)*a + tmp
643 i915_emit_arith(p
, A0_MAD
, tmp
,
644 flags
& A0_DEST_CHANNEL_ALL
, 0, src1
, src0
, src2
);
646 i915_emit_arith(p
, A0_MAD
,
647 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
648 flags
, 0, negate(src2
, 1, 1, 1, 1), src0
, tmp
);
651 case TGSI_OPCODE_MAD
:
652 emit_simple_arith(p
, inst
, A0_MAD
, 3);
655 case TGSI_OPCODE_MAX
:
656 emit_simple_arith(p
, inst
, A0_MAX
, 2);
659 case TGSI_OPCODE_MIN
:
660 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
661 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
662 tmp
= i915_get_utemp(p
);
663 flags
= get_result_flags(inst
);
667 tmp
, flags
& A0_DEST_CHANNEL_ALL
, 0,
668 negate(src0
, 1, 1, 1, 1),
669 negate(src1
, 1, 1, 1, 1), 0);
673 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
674 flags
, 0, negate(tmp
, 1, 1, 1, 1), 0, 0);
677 case TGSI_OPCODE_MOV
:
678 /* aka TGSI_OPCODE_SWZ */
679 emit_simple_arith(p
, inst
, A0_MOV
, 1);
682 case TGSI_OPCODE_MUL
:
683 emit_simple_arith(p
, inst
, A0_MUL
, 2);
686 case TGSI_OPCODE_POW
:
687 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
688 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
689 tmp
= i915_get_utemp(p
);
690 flags
= get_result_flags(inst
);
692 /* XXX: masking on intermediate values, here and elsewhere.
696 tmp
, A0_DEST_CHANNEL_X
, 0,
697 swizzle(src0
, X
, X
, X
, X
), 0, 0);
699 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, src1
, 0);
703 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
704 flags
, 0, swizzle(tmp
, X
, X
, X
, X
), 0, 0);
707 case TGSI_OPCODE_RET
:
711 case TGSI_OPCODE_RCP
:
712 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
716 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
717 get_result_flags(inst
), 0,
718 swizzle(src0
, X
, X
, X
, X
), 0, 0);
721 case TGSI_OPCODE_RSQ
:
722 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
726 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
727 get_result_flags(inst
), 0,
728 swizzle(src0
, X
, X
, X
, X
), 0, 0);
731 case TGSI_OPCODE_SCS
:
732 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
733 tmp
= i915_get_utemp(p
);
736 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
737 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
738 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
739 * scs.x = DP4 t1, sin_constants
740 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
741 * scs.y = DP4 t1, cos_constants
745 tmp
, A0_DEST_CHANNEL_XY
, 0,
746 swizzle(src0
, X
, X
, ONE
, ONE
),
747 swizzle(src0
, X
, ONE
, ONE
, ONE
), 0);
751 tmp
, A0_DEST_CHANNEL_ALL
, 0,
752 swizzle(tmp
, X
, Y
, X
, Y
),
753 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
755 writemask
= inst
->FullDstRegisters
[0].DstRegister
.WriteMask
;
757 if (writemask
& TGSI_WRITEMASK_Y
) {
760 if (writemask
& TGSI_WRITEMASK_X
)
761 tmp1
= i915_get_utemp(p
);
767 tmp1
, A0_DEST_CHANNEL_ALL
, 0,
768 swizzle(tmp
, X
, Y
, Y
, W
),
769 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
773 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
774 A0_DEST_CHANNEL_Y
, 0,
775 swizzle(tmp1
, W
, Z
, Y
, X
),
776 i915_emit_const4fv(p
, sin_constants
), 0);
779 if (writemask
& TGSI_WRITEMASK_X
) {
782 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
783 swizzle(tmp
, X
, X
, Z
, ONE
),
784 swizzle(tmp
, Z
, ONE
, ONE
, ONE
), 0);
788 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
789 A0_DEST_CHANNEL_X
, 0,
790 swizzle(tmp
, ONE
, Z
, Y
, X
),
791 i915_emit_const4fv(p
, cos_constants
), 0);
795 case TGSI_OPCODE_SGE
:
796 emit_simple_arith(p
, inst
, A0_SGE
, 2);
799 case TGSI_OPCODE_SLE
:
800 /* like SGE, but swap reg0, reg1 */
801 emit_simple_arith_swap2(p
, inst
, A0_SGE
, 2);
804 case TGSI_OPCODE_SIN
:
805 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
806 tmp
= i915_get_utemp(p
);
810 tmp
, A0_DEST_CHANNEL_X
, 0,
811 src0
, i915_emit_const1f(p
, 1.0f
/ (float) (M_PI
* 2.0)), 0);
813 i915_emit_arith(p
, A0_MOD
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
815 /* By choosing different taylor constants, could get rid of this mul:
819 tmp
, A0_DEST_CHANNEL_X
, 0,
820 tmp
, i915_emit_const1f(p
, (float) (M_PI
* 2.0)), 0);
823 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
824 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
825 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
826 * result = DP4 t1.wzyx, sin_constants
830 tmp
, A0_DEST_CHANNEL_XY
, 0,
831 swizzle(tmp
, X
, X
, ONE
, ONE
),
832 swizzle(tmp
, X
, ONE
, ONE
, ONE
), 0);
836 tmp
, A0_DEST_CHANNEL_ALL
, 0,
837 swizzle(tmp
, X
, Y
, X
, Y
),
838 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
842 tmp
, A0_DEST_CHANNEL_ALL
, 0,
843 swizzle(tmp
, X
, Y
, Y
, W
),
844 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
848 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
849 get_result_flags(inst
), 0,
850 swizzle(tmp
, W
, Z
, Y
, X
),
851 i915_emit_const4fv(p
, sin_constants
), 0);
854 case TGSI_OPCODE_SLT
:
855 emit_simple_arith(p
, inst
, A0_SLT
, 2);
858 case TGSI_OPCODE_SGT
:
859 /* like SLT, but swap reg0, reg1 */
860 emit_simple_arith_swap2(p
, inst
, A0_SLT
, 2);
863 case TGSI_OPCODE_SUB
:
864 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
865 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
869 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
870 get_result_flags(inst
), 0,
871 src0
, negate(src1
, 1, 1, 1, 1), 0);
874 case TGSI_OPCODE_TEX
:
875 if (inst
->FullSrcRegisters
[0].SrcRegisterExtSwz
.ExtDivide
876 == TGSI_EXTSWIZZLE_W
) {
877 emit_tex(p
, inst
, T0_TEXLDP
);
880 emit_tex(p
, inst
, T0_TEXLD
);
884 case TGSI_OPCODE_TXB
:
885 emit_tex(p
, inst
, T0_TEXLDB
);
888 case TGSI_OPCODE_XPD
:
890 * result.x = src0.y * src1.z - src0.z * src1.y;
891 * result.y = src0.z * src1.x - src0.x * src1.z;
892 * result.z = src0.x * src1.y - src0.y * src1.x;
895 src0
= src_vector(p
, &inst
->FullSrcRegisters
[0]);
896 src1
= src_vector(p
, &inst
->FullSrcRegisters
[1]);
897 tmp
= i915_get_utemp(p
);
901 tmp
, A0_DEST_CHANNEL_ALL
, 0,
902 swizzle(src0
, Z
, X
, Y
, ONE
),
903 swizzle(src1
, Y
, Z
, X
, ONE
), 0);
907 get_result_vector(p
, &inst
->FullDstRegisters
[0]),
908 get_result_flags(inst
), 0,
909 swizzle(src0
, Y
, Z
, X
, ONE
),
910 swizzle(src1
, Z
, X
, Y
, ONE
),
911 negate(tmp
, 1, 1, 1, 0));
915 i915_program_error(p
, "bad opcode %d", inst
->Instruction
.Opcode
);
920 i915_release_utemps(p
);
925 * Translate TGSI fragment shader into i915 hardware instructions.
926 * \param p the translation state
927 * \param tokens the TGSI token array
930 i915_translate_instructions(struct i915_fp_compile
*p
,
931 const struct tgsi_token
*tokens
)
933 struct i915_fragment_shader
*ifs
= p
->shader
;
934 struct tgsi_parse_context parse
;
936 tgsi_parse_init( &parse
, tokens
);
938 while( !tgsi_parse_end_of_tokens( &parse
) ) {
940 tgsi_parse_token( &parse
);
942 switch( parse
.FullToken
.Token
.Type
) {
943 case TGSI_TOKEN_TYPE_DECLARATION
:
944 if (parse
.FullToken
.FullDeclaration
.Declaration
.File
945 == TGSI_FILE_CONSTANT
) {
947 for (i
= parse
.FullToken
.FullDeclaration
.u
.DeclarationRange
.First
;
948 i
<= parse
.FullToken
.FullDeclaration
.u
.DeclarationRange
.Last
;
950 assert(ifs
->constant_flags
[i
] == 0x0);
951 ifs
->constant_flags
[i
] = I915_CONSTFLAG_USER
;
952 ifs
->num_constants
= MAX2(ifs
->num_constants
, i
+ 1);
955 else if (parse
.FullToken
.FullDeclaration
.Declaration
.File
956 == TGSI_FILE_TEMPORARY
) {
958 for (i
= parse
.FullToken
.FullDeclaration
.u
.DeclarationRange
.First
;
959 i
<= parse
.FullToken
.FullDeclaration
.u
.DeclarationRange
.Last
;
961 assert(i
< I915_MAX_TEMPORARY
);
962 /* XXX just use shader->info->file_mask[TGSI_FILE_TEMPORARY] */
963 p
->temp_flag
|= (1 << i
); /* mark temp as used */
968 case TGSI_TOKEN_TYPE_IMMEDIATE
:
970 const struct tgsi_full_immediate
*imm
971 = &parse
.FullToken
.FullImmediate
;
972 const uint pos
= p
->num_immediates
++;
974 for (j
= 0; j
< imm
->Immediate
.Size
; j
++) {
975 p
->immediates
[pos
][j
] = imm
->u
.ImmediateFloat32
[j
].Float
;
980 case TGSI_TOKEN_TYPE_INSTRUCTION
:
981 if (p
->first_instruction
) {
982 /* resolve location of immediates */
984 for (i
= 0; i
< p
->num_immediates
; i
++) {
985 /* find constant slot for this immediate */
986 for (j
= 0; j
< I915_MAX_CONSTANT
; j
++) {
987 if (ifs
->constant_flags
[j
] == 0x0) {
988 memcpy(ifs
->constants
[j
],
991 /*printf("immediate %d maps to const %d\n", i, j);*/
992 ifs
->constant_flags
[j
] = 0xf; /* all four comps used */
993 p
->immediates_map
[i
] = j
;
994 ifs
->num_constants
= MAX2(ifs
->num_constants
, j
+ 1);
1000 p
->first_instruction
= FALSE
;
1003 i915_translate_instruction(p
, &parse
.FullToken
.FullInstruction
);
1012 tgsi_parse_free (&parse
);
1016 static struct i915_fp_compile
*
1017 i915_init_compile(struct i915_context
*i915
,
1018 struct i915_fragment_shader
*ifs
)
1020 struct i915_fp_compile
*p
= CALLOC_STRUCT(i915_fp_compile
);
1024 /* Put new constants at end of const buffer, growing downward.
1025 * The problem is we don't know how many user-defined constants might
1026 * be specified with pipe->set_constant_buffer().
1027 * Should pre-scan the user's program to determine the highest-numbered
1028 * constant referenced.
1030 ifs
->num_constants
= 0;
1031 memset(ifs
->constant_flags
, 0, sizeof(ifs
->constant_flags
));
1033 p
->first_instruction
= TRUE
;
1035 p
->nr_tex_indirect
= 1; /* correct? */
1038 p
->nr_decl_insn
= 0;
1040 p
->csr
= p
->program
;
1041 p
->decl
= p
->declarations
;
1044 p
->temp_flag
= ~0x0 << I915_MAX_TEMPORARY
;
1045 p
->utemp_flag
= ~0x7;
1049 /* initialize the first program word */
1050 *(p
->decl
++) = _3DSTATE_PIXEL_SHADER_PROGRAM
;
1056 /* Copy compile results to the fragment program struct and destroy the
1057 * compilation context.
1060 i915_fini_compile(struct i915_context
*i915
, struct i915_fp_compile
*p
)
1062 struct i915_fragment_shader
*ifs
= p
->shader
;
1063 unsigned long program_size
= (unsigned long) (p
->csr
- p
->program
);
1064 unsigned long decl_size
= (unsigned long) (p
->decl
- p
->declarations
);
1066 if (p
->nr_tex_indirect
> I915_MAX_TEX_INDIRECT
)
1067 i915_program_error(p
, "Exceeded max nr indirect texture lookups");
1069 if (p
->nr_tex_insn
> I915_MAX_TEX_INSN
)
1070 i915_program_error(p
, "Exceeded max TEX instructions");
1072 if (p
->nr_alu_insn
> I915_MAX_ALU_INSN
)
1073 i915_program_error(p
, "Exceeded max ALU instructions");
1075 if (p
->nr_decl_insn
> I915_MAX_DECL_INSN
)
1076 i915_program_error(p
, "Exceeded max DECL instructions");
1079 p
->NumNativeInstructions
= 0;
1080 p
->NumNativeAluInstructions
= 0;
1081 p
->NumNativeTexInstructions
= 0;
1082 p
->NumNativeTexIndirections
= 0;
1084 i915_use_passthrough_shader(ifs
);
1087 p
->NumNativeInstructions
1088 = p
->nr_alu_insn
+ p
->nr_tex_insn
+ p
->nr_decl_insn
;
1089 p
->NumNativeAluInstructions
= p
->nr_alu_insn
;
1090 p
->NumNativeTexInstructions
= p
->nr_tex_insn
;
1091 p
->NumNativeTexIndirections
= p
->nr_tex_indirect
;
1093 /* patch in the program length */
1094 p
->declarations
[0] |= program_size
+ decl_size
- 2;
1096 /* Copy compilation results to fragment program struct:
1098 assert(!ifs
->program
);
1100 = (uint
*) MALLOC((program_size
+ decl_size
) * sizeof(uint
));
1102 ifs
->program_len
= program_size
+ decl_size
;
1104 memcpy(ifs
->program
,
1106 decl_size
* sizeof(uint
));
1108 memcpy(ifs
->program
+ decl_size
,
1110 program_size
* sizeof(uint
));
1114 /* Release the compilation struct:
1121 * Find an unused texture coordinate slot to use for fragment WPOS.
1122 * Update p->fp->wpos_tex with the result (-1 if no used texcoord slot is found).
1125 i915_find_wpos_space(struct i915_fp_compile
*p
)
1129 = p
->shader
->inputs_read
| (1 << TGSI_ATTRIB_POS
); /*XXX hack*/
1134 if (inputs
& (1 << TGSI_ATTRIB_POS
)) {
1135 for (i
= 0; i
< I915_TEX_UNITS
; i
++) {
1136 if ((inputs
& (1 << (TGSI_ATTRIB_TEX0
+ i
))) == 0) {
1142 i915_program_error(p
, "No free texcoord for wpos value");
1145 if (p
->shader
->info
.input_semantic_name
[0] == TGSI_SEMANTIC_POSITION
) {
1146 /* frag shader using the fragment position input */
1158 * Rather than trying to intercept and jiggle depth writes during
1159 * emit, just move the value into its correct position at the end of
1163 i915_fixup_depth_write(struct i915_fp_compile
*p
)
1165 /* XXX assuming pos/depth is always in output[0] */
1166 if (p
->shader
->info
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
) {
1167 const uint depth
= UREG(REG_TYPE_OD
, 0);
1170 A0_MOV
, /* opcode */
1171 depth
, /* dest reg */
1172 A0_DEST_CHANNEL_W
, /* write mask */
1174 swizzle(depth
, X
, Y
, Z
, Z
), /* src0 */
1175 0, 0 /* src1, src2 */);
1181 i915_translate_fragment_program( struct i915_context
*i915
,
1182 struct i915_fragment_shader
*fs
)
1184 struct i915_fp_compile
*p
= i915_init_compile(i915
, fs
);
1185 const struct tgsi_token
*tokens
= fs
->state
.tokens
;
1187 i915_find_wpos_space(p
);
1190 tgsi_dump(tokens
, 0);
1193 i915_translate_instructions(p
, tokens
);
1194 i915_fixup_depth_write(p
);
1196 i915_fini_compile(i915
, p
);