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_math.h"
37 #include "util/u_memory.h"
38 #include "util/u_string.h"
39 #include "tgsi/tgsi_parse.h"
40 #include "tgsi/tgsi_dump.h"
42 #include "draw/draw_vertex.h"
46 * Simple pass-through fragment shader to use when we don't have
47 * a real shader (or it fails to compile for some reason).
49 static unsigned passthrough
[] =
51 _3DSTATE_PIXEL_SHADER_PROGRAM
| ((2*3)-1),
53 /* declare input color:
56 (REG_TYPE_T
<< D0_TYPE_SHIFT
) |
57 (T_DIFFUSE
<< D0_NR_SHIFT
) |
62 /* move to output color:
65 (REG_TYPE_OC
<< A0_DEST_TYPE_SHIFT
) |
67 (REG_TYPE_T
<< A0_SRC0_TYPE_SHIFT
) |
68 (T_DIFFUSE
<< A0_SRC0_NR_SHIFT
)),
69 0x01230000, /* .xyzw */
74 /* 1, -1/3!, 1/5!, -1/7! */
75 static const float sin_constants
[4] = { 1.0,
77 1.0f
/ (5 * 4 * 3 * 2 * 1),
78 -1.0f
/ (7 * 6 * 5 * 4 * 3 * 2 * 1)
81 /* 1, -1/2!, 1/4!, -1/6! */
82 static const float cos_constants
[4] = { 1.0,
84 1.0f
/ (4 * 3 * 2 * 1),
85 -1.0f
/ (6 * 5 * 4 * 3 * 2 * 1)
91 * component-wise negation of ureg
94 negate(int reg
, int x
, int y
, int z
, int w
)
96 /* Another neat thing about the UREG representation */
97 return reg
^ (((x
& 1) << UREG_CHANNEL_X_NEGATE_SHIFT
) |
98 ((y
& 1) << UREG_CHANNEL_Y_NEGATE_SHIFT
) |
99 ((z
& 1) << UREG_CHANNEL_Z_NEGATE_SHIFT
) |
100 ((w
& 1) << UREG_CHANNEL_W_NEGATE_SHIFT
));
105 * In the event of a translation failure, we'll generate a simple color
106 * pass-through program.
109 i915_use_passthrough_shader(struct i915_fragment_shader
*fs
)
111 fs
->program
= (uint
*) MALLOC(sizeof(passthrough
));
113 memcpy(fs
->program
, passthrough
, sizeof(passthrough
));
114 fs
->program_len
= Elements(passthrough
);
116 fs
->num_constants
= 0;
121 i915_program_error(struct i915_fp_compile
*p
, const char *msg
, ...)
126 debug_printf("i915_program_error: ");
127 va_start( args
, msg
);
128 util_vsnprintf( buffer
, sizeof(buffer
), msg
, args
);
130 debug_printf("%s", buffer
);
139 * Construct a ureg for the given source register. Will emit
140 * constants, apply swizzling and negation as needed.
143 src_vector(struct i915_fp_compile
*p
,
144 const struct tgsi_full_src_register
*source
)
146 uint index
= source
->Register
.Index
;
147 uint src
= 0, sem_name
, sem_ind
;
149 switch (source
->Register
.File
) {
150 case TGSI_FILE_TEMPORARY
:
151 if (source
->Register
.Index
>= I915_MAX_TEMPORARY
) {
152 i915_program_error(p
, "Exceeded max temporary reg");
155 src
= UREG(REG_TYPE_R
, index
);
157 case TGSI_FILE_INPUT
:
158 /* XXX: Packing COL1, FOGC into a single attribute works for
159 * texenv programs, but will fail for real fragment programs
160 * that use these attributes and expect them to be a full 4
161 * components wide. Could use a texcoord to pass these
162 * attributes if necessary, but that won't work in the general
165 * We also use a texture coordinate to pass wpos when possible.
168 sem_name
= p
->shader
->info
.input_semantic_name
[index
];
169 sem_ind
= p
->shader
->info
.input_semantic_index
[index
];
172 case TGSI_SEMANTIC_POSITION
:
173 debug_printf("SKIP SEM POS\n");
175 assert(p->wpos_tex != -1);
176 src = i915_emit_decl(p, REG_TYPE_T, p->wpos_tex, D0_CHANNEL_ALL);
179 case TGSI_SEMANTIC_COLOR
:
181 src
= i915_emit_decl(p
, REG_TYPE_T
, T_DIFFUSE
, D0_CHANNEL_ALL
);
184 /* secondary color */
185 assert(sem_ind
== 1);
186 src
= i915_emit_decl(p
, REG_TYPE_T
, T_SPECULAR
, D0_CHANNEL_XYZ
);
187 src
= swizzle(src
, X
, Y
, Z
, ONE
);
190 case TGSI_SEMANTIC_FOG
:
191 src
= i915_emit_decl(p
, REG_TYPE_T
, T_FOG_W
, D0_CHANNEL_W
);
192 src
= swizzle(src
, W
, W
, W
, W
);
194 case TGSI_SEMANTIC_GENERIC
:
197 src
= i915_emit_decl(p
, REG_TYPE_T
, T_TEX0
+ sem_ind
, D0_CHANNEL_ALL
);
198 else if ( (sem_ind
>= 10) && (sem_ind
< 18) )
200 src
= i915_emit_decl(p
, REG_TYPE_T
, T_TEX0
+ sem_ind
- 10, D0_CHANNEL_ALL
);
202 debug_printf("%s: unhandled generic %d\n", __func__
, sem_ind
);
205 i915_program_error(p
, "Bad source->Index");
210 case TGSI_FILE_IMMEDIATE
:
211 assert(index
< p
->num_immediates
);
212 index
= p
->immediates_map
[index
];
214 case TGSI_FILE_CONSTANT
:
215 src
= UREG(REG_TYPE_CONST
, index
);
219 i915_program_error(p
, "Bad source->File");
224 source
->Register
.SwizzleX
,
225 source
->Register
.SwizzleY
,
226 source
->Register
.SwizzleZ
,
227 source
->Register
.SwizzleW
);
230 /* There's both negate-all-components and per-component negation.
231 * Try to handle both here.
234 int n
= source
->Register
.Negate
;
235 src
= negate(src
, n
, n
, n
, n
);
240 /* XXX assertions disabled to allow arbfplight.c to run */
241 /* XXX enable these assertions, or fix things */
242 assert(!source
->Register
.Absolute
);
249 * Construct a ureg for a destination register.
252 get_result_vector(struct i915_fp_compile
*p
,
253 const struct tgsi_full_dst_register
*dest
)
255 switch (dest
->Register
.File
) {
256 case TGSI_FILE_OUTPUT
:
258 uint sem_name
= p
->shader
->info
.output_semantic_name
[dest
->Register
.Index
];
260 case TGSI_SEMANTIC_POSITION
:
261 return UREG(REG_TYPE_OD
, 0);
262 case TGSI_SEMANTIC_COLOR
:
263 return UREG(REG_TYPE_OC
, 0);
265 i915_program_error(p
, "Bad inst->DstReg.Index/semantics");
269 case TGSI_FILE_TEMPORARY
:
270 return UREG(REG_TYPE_R
, dest
->Register
.Index
);
272 i915_program_error(p
, "Bad inst->DstReg.File");
279 * Compute flags for saturation and writemask.
282 get_result_flags(const struct tgsi_full_instruction
*inst
)
285 = inst
->Dst
[0].Register
.WriteMask
;
288 if (inst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
)
289 flags
|= A0_DEST_SATURATE
;
291 if (writeMask
& TGSI_WRITEMASK_X
)
292 flags
|= A0_DEST_CHANNEL_X
;
293 if (writeMask
& TGSI_WRITEMASK_Y
)
294 flags
|= A0_DEST_CHANNEL_Y
;
295 if (writeMask
& TGSI_WRITEMASK_Z
)
296 flags
|= A0_DEST_CHANNEL_Z
;
297 if (writeMask
& TGSI_WRITEMASK_W
)
298 flags
|= A0_DEST_CHANNEL_W
;
305 * Convert TGSI_TEXTURE_x token to DO_SAMPLE_TYPE_x token
308 translate_tex_src_target(struct i915_fp_compile
*p
, uint tex
)
311 case TGSI_TEXTURE_SHADOW1D
:
313 case TGSI_TEXTURE_1D
:
314 return D0_SAMPLE_TYPE_2D
;
316 case TGSI_TEXTURE_SHADOW2D
:
318 case TGSI_TEXTURE_2D
:
319 return D0_SAMPLE_TYPE_2D
;
321 case TGSI_TEXTURE_SHADOWRECT
:
323 case TGSI_TEXTURE_RECT
:
324 return D0_SAMPLE_TYPE_2D
;
326 case TGSI_TEXTURE_3D
:
327 return D0_SAMPLE_TYPE_VOLUME
;
329 case TGSI_TEXTURE_CUBE
:
330 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
->Texture
.Texture
;
348 uint unit
= inst
->Src
[1].Register
.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
->Src
[0]);
354 get_result_vector( p
, &inst
->Dst
[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
->Src
[0] );
377 arg2
= (numArgs
< 2) ? 0 : src_vector( p
, &inst
->Src
[1] );
378 arg3
= (numArgs
< 3) ? 0 : src_vector( p
, &inst
->Src
[2] );
382 get_result_vector( p
, &inst
->Dst
[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
.Src
[0] = inst
->Src
[1];
403 inst2
.Src
[1] = inst
->Src
[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
->Src
[0]);
435 get_result_vector(p
, &inst
->Dst
[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
->Src
[0]);
446 src1
= src_vector(p
, &inst
->Src
[1]);
447 src2
= src_vector(p
, &inst
->Src
[2]);
448 i915_emit_arith(p
, A0_CMP
,
449 get_result_vector(p
, &inst
->Dst
[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
->Src
[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
->Dst
[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
->Src
[0]);
514 src1
= src_vector(p
, &inst
->Src
[1]);
518 get_result_vector(p
, &inst
->Dst
[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
->Src
[0]);
525 src1
= src_vector(p
, &inst
->Src
[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
->Dst
[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
->Src
[0]);
549 get_result_vector(p
, &inst
->Dst
[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
->Src
[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
->Src
[0]);
584 get_result_vector(p
, &inst
->Dst
[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
->Src
[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
->Dst
[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
->Src
[0]);
624 src1
= src_vector(p
, &inst
->Src
[1]);
625 src2
= src_vector(p
, &inst
->Src
[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
->Dst
[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
->Src
[0]);
654 src1
= src_vector(p
, &inst
->Src
[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
->Dst
[0]),
667 flags
, 0, negate(tmp
, 1, 1, 1, 1), 0, 0);
670 case TGSI_OPCODE_MOV
:
671 emit_simple_arith(p
, inst
, A0_MOV
, 1);
674 case TGSI_OPCODE_MUL
:
675 emit_simple_arith(p
, inst
, A0_MUL
, 2);
678 case TGSI_OPCODE_POW
:
679 src0
= src_vector(p
, &inst
->Src
[0]);
680 src1
= src_vector(p
, &inst
->Src
[1]);
681 tmp
= i915_get_utemp(p
);
682 flags
= get_result_flags(inst
);
684 /* XXX: masking on intermediate values, here and elsewhere.
688 tmp
, A0_DEST_CHANNEL_X
, 0,
689 swizzle(src0
, X
, X
, X
, X
), 0, 0);
691 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, src1
, 0);
695 get_result_vector(p
, &inst
->Dst
[0]),
696 flags
, 0, swizzle(tmp
, X
, X
, X
, X
), 0, 0);
699 case TGSI_OPCODE_RET
:
703 case TGSI_OPCODE_RCP
:
704 src0
= src_vector(p
, &inst
->Src
[0]);
708 get_result_vector(p
, &inst
->Dst
[0]),
709 get_result_flags(inst
), 0,
710 swizzle(src0
, X
, X
, X
, X
), 0, 0);
713 case TGSI_OPCODE_RSQ
:
714 src0
= src_vector(p
, &inst
->Src
[0]);
718 get_result_vector(p
, &inst
->Dst
[0]),
719 get_result_flags(inst
), 0,
720 swizzle(src0
, X
, X
, X
, X
), 0, 0);
723 case TGSI_OPCODE_SCS
:
724 src0
= src_vector(p
, &inst
->Src
[0]);
725 tmp
= i915_get_utemp(p
);
728 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
729 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
730 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
731 * scs.x = DP4 t1, sin_constants
732 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
733 * scs.y = DP4 t1, cos_constants
737 tmp
, A0_DEST_CHANNEL_XY
, 0,
738 swizzle(src0
, X
, X
, ONE
, ONE
),
739 swizzle(src0
, X
, ONE
, ONE
, ONE
), 0);
743 tmp
, A0_DEST_CHANNEL_ALL
, 0,
744 swizzle(tmp
, X
, Y
, X
, Y
),
745 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
747 writemask
= inst
->Dst
[0].Register
.WriteMask
;
749 if (writemask
& TGSI_WRITEMASK_Y
) {
752 if (writemask
& TGSI_WRITEMASK_X
)
753 tmp1
= i915_get_utemp(p
);
759 tmp1
, A0_DEST_CHANNEL_ALL
, 0,
760 swizzle(tmp
, X
, Y
, Y
, W
),
761 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
765 get_result_vector(p
, &inst
->Dst
[0]),
766 A0_DEST_CHANNEL_Y
, 0,
767 swizzle(tmp1
, W
, Z
, Y
, X
),
768 i915_emit_const4fv(p
, sin_constants
), 0);
771 if (writemask
& TGSI_WRITEMASK_X
) {
774 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
775 swizzle(tmp
, X
, X
, Z
, ONE
),
776 swizzle(tmp
, Z
, ONE
, ONE
, ONE
), 0);
780 get_result_vector(p
, &inst
->Dst
[0]),
781 A0_DEST_CHANNEL_X
, 0,
782 swizzle(tmp
, ONE
, Z
, Y
, X
),
783 i915_emit_const4fv(p
, cos_constants
), 0);
787 case TGSI_OPCODE_SGE
:
788 emit_simple_arith(p
, inst
, A0_SGE
, 2);
791 case TGSI_OPCODE_SLE
:
792 /* like SGE, but swap reg0, reg1 */
793 emit_simple_arith_swap2(p
, inst
, A0_SGE
, 2);
796 case TGSI_OPCODE_SIN
:
797 src0
= src_vector(p
, &inst
->Src
[0]);
798 tmp
= i915_get_utemp(p
);
802 tmp
, A0_DEST_CHANNEL_X
, 0,
803 src0
, i915_emit_const1f(p
, 1.0f
/ (float) (M_PI
* 2.0)), 0);
805 i915_emit_arith(p
, A0_MOD
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
807 /* By choosing different taylor constants, could get rid of this mul:
811 tmp
, A0_DEST_CHANNEL_X
, 0,
812 tmp
, i915_emit_const1f(p
, (float) (M_PI
* 2.0)), 0);
815 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
816 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
817 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
818 * result = DP4 t1.wzyx, sin_constants
822 tmp
, A0_DEST_CHANNEL_XY
, 0,
823 swizzle(tmp
, X
, X
, ONE
, ONE
),
824 swizzle(tmp
, X
, ONE
, ONE
, ONE
), 0);
828 tmp
, A0_DEST_CHANNEL_ALL
, 0,
829 swizzle(tmp
, X
, Y
, X
, Y
),
830 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
834 tmp
, A0_DEST_CHANNEL_ALL
, 0,
835 swizzle(tmp
, X
, Y
, Y
, W
),
836 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
840 get_result_vector(p
, &inst
->Dst
[0]),
841 get_result_flags(inst
), 0,
842 swizzle(tmp
, W
, Z
, Y
, X
),
843 i915_emit_const4fv(p
, sin_constants
), 0);
846 case TGSI_OPCODE_SLT
:
847 emit_simple_arith(p
, inst
, A0_SLT
, 2);
850 case TGSI_OPCODE_SGT
:
851 /* like SLT, but swap reg0, reg1 */
852 emit_simple_arith_swap2(p
, inst
, A0_SLT
, 2);
855 case TGSI_OPCODE_SUB
:
856 src0
= src_vector(p
, &inst
->Src
[0]);
857 src1
= src_vector(p
, &inst
->Src
[1]);
861 get_result_vector(p
, &inst
->Dst
[0]),
862 get_result_flags(inst
), 0,
863 src0
, negate(src1
, 1, 1, 1, 1), 0);
866 case TGSI_OPCODE_TEX
:
867 emit_tex(p
, inst
, T0_TEXLD
);
870 case TGSI_OPCODE_TXB
:
871 emit_tex(p
, inst
, T0_TEXLDB
);
874 case TGSI_OPCODE_TXP
:
875 emit_tex(p
, inst
, T0_TEXLDP
);
878 case TGSI_OPCODE_XPD
:
880 * result.x = src0.y * src1.z - src0.z * src1.y;
881 * result.y = src0.z * src1.x - src0.x * src1.z;
882 * result.z = src0.x * src1.y - src0.y * src1.x;
885 src0
= src_vector(p
, &inst
->Src
[0]);
886 src1
= src_vector(p
, &inst
->Src
[1]);
887 tmp
= i915_get_utemp(p
);
891 tmp
, A0_DEST_CHANNEL_ALL
, 0,
892 swizzle(src0
, Z
, X
, Y
, ONE
),
893 swizzle(src1
, Y
, Z
, X
, ONE
), 0);
897 get_result_vector(p
, &inst
->Dst
[0]),
898 get_result_flags(inst
), 0,
899 swizzle(src0
, Y
, Z
, X
, ONE
),
900 swizzle(src1
, Z
, X
, Y
, ONE
),
901 negate(tmp
, 1, 1, 1, 0));
905 i915_program_error(p
, "bad opcode %d", inst
->Instruction
.Opcode
);
910 i915_release_utemps(p
);
915 * Translate TGSI fragment shader into i915 hardware instructions.
916 * \param p the translation state
917 * \param tokens the TGSI token array
920 i915_translate_instructions(struct i915_fp_compile
*p
,
921 const struct tgsi_token
*tokens
)
923 struct i915_fragment_shader
*ifs
= p
->shader
;
924 struct tgsi_parse_context parse
;
926 tgsi_parse_init( &parse
, tokens
);
928 while( !tgsi_parse_end_of_tokens( &parse
) ) {
930 tgsi_parse_token( &parse
);
932 switch( parse
.FullToken
.Token
.Type
) {
933 case TGSI_TOKEN_TYPE_PROPERTY
:
935 * We only support one cbuf, but we still need to ignore the property
936 * correctly so we don't hit the assert at the end of the switch case.
938 assert(parse
.FullToken
.FullProperty
.Property
.PropertyName
==
939 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
);
941 case TGSI_TOKEN_TYPE_DECLARATION
:
942 if (parse
.FullToken
.FullDeclaration
.Declaration
.File
943 == TGSI_FILE_CONSTANT
) {
945 for (i
= parse
.FullToken
.FullDeclaration
.Range
.First
;
946 i
<= parse
.FullToken
.FullDeclaration
.Range
.Last
;
948 assert(ifs
->constant_flags
[i
] == 0x0);
949 ifs
->constant_flags
[i
] = I915_CONSTFLAG_USER
;
950 ifs
->num_constants
= MAX2(ifs
->num_constants
, i
+ 1);
953 else if (parse
.FullToken
.FullDeclaration
.Declaration
.File
954 == TGSI_FILE_TEMPORARY
) {
956 for (i
= parse
.FullToken
.FullDeclaration
.Range
.First
;
957 i
<= parse
.FullToken
.FullDeclaration
.Range
.Last
;
959 assert(i
< I915_MAX_TEMPORARY
);
960 /* XXX just use shader->info->file_mask[TGSI_FILE_TEMPORARY] */
961 p
->temp_flag
|= (1 << i
); /* mark temp as used */
966 case TGSI_TOKEN_TYPE_IMMEDIATE
:
968 const struct tgsi_full_immediate
*imm
969 = &parse
.FullToken
.FullImmediate
;
970 const uint pos
= p
->num_immediates
++;
972 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
973 for (j
= 0; j
< imm
->Immediate
.NrTokens
- 1; j
++) {
974 p
->immediates
[pos
][j
] = imm
->u
[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
;
1184 const struct tgsi_token
*tokens
= fs
->state
.tokens
;
1187 tgsi_dump(tokens
, 0);
1190 /* hw doesn't seem to like empty frag programs, even when the depth write
1191 * fixup gets emitted below - may that one is fishy, too? */
1192 if (fs
->info
.num_instructions
== 1) {
1193 i915_use_passthrough_shader(fs
);
1198 p
= i915_init_compile(i915
, fs
);
1199 i915_find_wpos_space(p
);
1201 i915_translate_instructions(p
, tokens
);
1202 i915_fixup_depth_write(p
);
1204 i915_fini_compile(i915
, p
);