1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
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 VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "main/glheader.h"
29 #include "main/macros.h"
30 #include "main/enums.h"
32 #include "program/prog_instruction.h"
33 #include "program/prog_parameter.h"
34 #include "program/program.h"
35 #include "program/programopt.h"
36 #include "program/prog_print.h"
39 #include "tnl/t_context.h"
41 #include "intel_batchbuffer.h"
44 #include "i915_context.h"
45 #include "i915_program.h"
47 static const GLfloat sin_quad_constants
[2][4] = {
62 static const GLfloat sin_constants
[4] = { 1.0,
64 1.0 / (5 * 4 * 3 * 2 * 1),
65 -1.0 / (7 * 6 * 5 * 4 * 3 * 2 * 1)
68 /* 1, -1/2!, 1/4!, -1/6! */
69 static const GLfloat cos_constants
[4] = { 1.0,
71 1.0 / (4 * 3 * 2 * 1),
72 -1.0 / (6 * 5 * 4 * 3 * 2 * 1)
76 * Retrieve a ureg for the given source register. Will emit
77 * constants, apply swizzling and negation as needed.
80 src_vector(struct i915_fragment_program
*p
,
81 const struct prog_src_register
*source
,
82 const struct gl_fragment_program
*program
)
86 switch (source
->File
) {
90 case PROGRAM_TEMPORARY
:
91 if (source
->Index
>= I915_MAX_TEMPORARY
) {
92 i915_program_error(p
, "Exceeded max temporary reg: %d/%d",
93 source
->Index
, I915_MAX_TEMPORARY
);
96 src
= UREG(REG_TYPE_R
, source
->Index
);
99 switch (source
->Index
) {
100 case VARYING_SLOT_POS
:
101 src
= i915_emit_decl(p
, REG_TYPE_T
, p
->wpos_tex
, D0_CHANNEL_ALL
);
103 case VARYING_SLOT_COL0
:
104 src
= i915_emit_decl(p
, REG_TYPE_T
, T_DIFFUSE
, D0_CHANNEL_ALL
);
106 case VARYING_SLOT_COL1
:
107 src
= i915_emit_decl(p
, REG_TYPE_T
, T_SPECULAR
, D0_CHANNEL_XYZ
);
108 src
= swizzle(src
, X
, Y
, Z
, ONE
);
110 case VARYING_SLOT_FOGC
:
111 src
= i915_emit_decl(p
, REG_TYPE_T
, T_FOG_W
, D0_CHANNEL_W
);
112 src
= swizzle(src
, W
, ZERO
, ZERO
, ONE
);
114 case VARYING_SLOT_TEX0
:
115 case VARYING_SLOT_TEX1
:
116 case VARYING_SLOT_TEX2
:
117 case VARYING_SLOT_TEX3
:
118 case VARYING_SLOT_TEX4
:
119 case VARYING_SLOT_TEX5
:
120 case VARYING_SLOT_TEX6
:
121 case VARYING_SLOT_TEX7
:
122 src
= i915_emit_decl(p
, REG_TYPE_T
,
123 T_TEX0
+ (source
->Index
- VARYING_SLOT_TEX0
),
127 case VARYING_SLOT_VAR0
:
128 case VARYING_SLOT_VAR0
+ 1:
129 case VARYING_SLOT_VAR0
+ 2:
130 case VARYING_SLOT_VAR0
+ 3:
131 case VARYING_SLOT_VAR0
+ 4:
132 case VARYING_SLOT_VAR0
+ 5:
133 case VARYING_SLOT_VAR0
+ 6:
134 case VARYING_SLOT_VAR0
+ 7:
135 src
= i915_emit_decl(p
, REG_TYPE_T
,
136 T_TEX0
+ (source
->Index
- VARYING_SLOT_VAR0
),
141 i915_program_error(p
, "Bad source->Index: %d", source
->Index
);
147 switch (source
->Index
) {
148 case FRAG_RESULT_COLOR
:
149 case FRAG_RESULT_DATA0
:
150 src
= UREG(REG_TYPE_OC
, 0);
152 case FRAG_RESULT_DEPTH
:
153 src
= UREG(REG_TYPE_OD
, 0);
156 i915_program_error(p
, "Bad source->Index: %d", source
->Index
);
161 /* Various paramters and env values. All emitted to
162 * hardware as program constants.
164 case PROGRAM_CONSTANT
:
165 case PROGRAM_STATE_VAR
:
166 case PROGRAM_UNIFORM
:
167 src
= i915_emit_param4fv(p
,
168 &program
->Base
.Parameters
->ParameterValues
[source
->Index
][0].f
);
172 i915_program_error(p
, "Bad source->File: %d", source
->File
);
177 GET_SWZ(source
->Swizzle
, 0),
178 GET_SWZ(source
->Swizzle
, 1),
179 GET_SWZ(source
->Swizzle
, 2), GET_SWZ(source
->Swizzle
, 3));
183 GET_BIT(source
->Negate
, 0),
184 GET_BIT(source
->Negate
, 1),
185 GET_BIT(source
->Negate
, 2),
186 GET_BIT(source
->Negate
, 3));
193 get_result_vector(struct i915_fragment_program
*p
,
194 const struct prog_instruction
*inst
)
196 switch (inst
->DstReg
.File
) {
198 switch (inst
->DstReg
.Index
) {
199 case FRAG_RESULT_COLOR
:
200 case FRAG_RESULT_DATA0
:
201 return UREG(REG_TYPE_OC
, 0);
202 case FRAG_RESULT_DEPTH
:
203 p
->depth_written
= 1;
204 return UREG(REG_TYPE_OD
, 0);
206 i915_program_error(p
, "Bad inst->DstReg.Index: %d",
210 case PROGRAM_TEMPORARY
:
211 return UREG(REG_TYPE_R
, inst
->DstReg
.Index
);
213 i915_program_error(p
, "Bad inst->DstReg.File: %d", inst
->DstReg
.File
);
219 get_result_flags(const struct prog_instruction
*inst
)
223 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
224 flags
|= A0_DEST_SATURATE
;
225 if (inst
->DstReg
.WriteMask
& WRITEMASK_X
)
226 flags
|= A0_DEST_CHANNEL_X
;
227 if (inst
->DstReg
.WriteMask
& WRITEMASK_Y
)
228 flags
|= A0_DEST_CHANNEL_Y
;
229 if (inst
->DstReg
.WriteMask
& WRITEMASK_Z
)
230 flags
|= A0_DEST_CHANNEL_Z
;
231 if (inst
->DstReg
.WriteMask
& WRITEMASK_W
)
232 flags
|= A0_DEST_CHANNEL_W
;
238 translate_tex_src_target(struct i915_fragment_program
*p
, GLubyte bit
)
241 case TEXTURE_1D_INDEX
:
242 return D0_SAMPLE_TYPE_2D
;
243 case TEXTURE_2D_INDEX
:
244 return D0_SAMPLE_TYPE_2D
;
245 case TEXTURE_RECT_INDEX
:
246 return D0_SAMPLE_TYPE_2D
;
247 case TEXTURE_3D_INDEX
:
248 return D0_SAMPLE_TYPE_VOLUME
;
249 case TEXTURE_CUBE_INDEX
:
250 return D0_SAMPLE_TYPE_CUBE
;
252 i915_program_error(p
, "TexSrcBit: %d", bit
);
257 #define EMIT_TEX( OP ) \
259 GLuint dim = translate_tex_src_target( p, inst->TexSrcTarget ); \
260 const struct gl_fragment_program *program = &p->FragProg; \
261 GLuint unit = program->Base.SamplerUnits[inst->TexSrcUnit]; \
262 GLuint sampler = i915_emit_decl(p, REG_TYPE_S, \
264 GLuint coord = src_vector( p, &inst->SrcReg[0], program); \
267 i915_emit_texld( p, get_live_regs(p, inst), \
268 get_result_vector( p, inst ), \
269 get_result_flags( inst ), \
275 #define EMIT_ARITH( OP, N ) \
277 i915_emit_arith( p, \
279 get_result_vector( p, inst ), \
280 get_result_flags( inst ), 0, \
281 (N<1)?0:src_vector( p, &inst->SrcReg[0], program), \
282 (N<2)?0:src_vector( p, &inst->SrcReg[1], program), \
283 (N<3)?0:src_vector( p, &inst->SrcReg[2], program)); \
286 #define EMIT_1ARG_ARITH( OP ) EMIT_ARITH( OP, 1 )
287 #define EMIT_2ARG_ARITH( OP ) EMIT_ARITH( OP, 2 )
288 #define EMIT_3ARG_ARITH( OP ) EMIT_ARITH( OP, 3 )
291 * TODO: consider moving this into core
293 static bool calc_live_regs( struct i915_fragment_program
*p
)
295 const struct gl_fragment_program
*program
= &p
->FragProg
;
296 GLuint regsUsed
= ~((1 << I915_MAX_TEMPORARY
) - 1);
297 uint8_t live_components
[I915_MAX_TEMPORARY
] = { 0, };
300 for (i
= program
->Base
.NumInstructions
- 1; i
>= 0; i
--) {
301 struct prog_instruction
*inst
= &program
->Base
.Instructions
[i
];
302 int opArgs
= _mesa_num_inst_src_regs(inst
->Opcode
);
305 /* Register is written to: unmark as live for this and preceeding ops */
306 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
) {
307 if (inst
->DstReg
.Index
>= I915_MAX_TEMPORARY
)
310 live_components
[inst
->DstReg
.Index
] &= ~inst
->DstReg
.WriteMask
;
311 if (live_components
[inst
->DstReg
.Index
] == 0)
312 regsUsed
&= ~(1 << inst
->DstReg
.Index
);
315 for (a
= 0; a
< opArgs
; a
++) {
316 /* Register is read from: mark as live for this and preceeding ops */
317 if (inst
->SrcReg
[a
].File
== PROGRAM_TEMPORARY
) {
320 if (inst
->SrcReg
[a
].Index
>= I915_MAX_TEMPORARY
)
323 regsUsed
|= 1 << inst
->SrcReg
[a
].Index
;
325 for (c
= 0; c
< 4; c
++) {
326 const unsigned field
= GET_SWZ(inst
->SrcReg
[a
].Swizzle
, c
);
328 if (field
<= SWIZZLE_W
)
329 live_components
[inst
->SrcReg
[a
].Index
] |= (1U << field
);
334 p
->usedRegs
[i
] = regsUsed
;
340 static GLuint
get_live_regs( struct i915_fragment_program
*p
,
341 const struct prog_instruction
*inst
)
343 const struct gl_fragment_program
*program
= &p
->FragProg
;
344 GLuint nr
= inst
- program
->Base
.Instructions
;
346 return p
->usedRegs
[nr
];
350 /* Possible concerns:
352 * SIN, COS -- could use another taylor step?
353 * LIT -- results seem a little different to sw mesa
354 * LOG -- different to mesa on negative numbers, but this is conformant.
356 * Parse failures -- Mesa doesn't currently give a good indication
357 * internally whether a particular program string parsed or not. This
358 * can lead to confusion -- hopefully we cope with it ok now.
362 upload_program(struct i915_fragment_program
*p
)
364 const struct gl_fragment_program
*program
= &p
->FragProg
;
365 const struct prog_instruction
*inst
= program
->Base
.Instructions
;
367 if (INTEL_DEBUG
& DEBUG_WM
)
368 _mesa_print_program(&program
->Base
);
370 /* Is this a parse-failed program? Ensure a valid program is
371 * loaded, as the flagging of an error isn't sufficient to stop
372 * this being uploaded to hardware.
374 if (inst
[0].Opcode
== OPCODE_END
) {
375 GLuint tmp
= i915_get_utemp(p
);
378 UREG(REG_TYPE_OC
, 0),
379 A0_DEST_CHANNEL_ALL
, 0,
380 swizzle(tmp
, ONE
, ZERO
, ONE
, ONE
), 0, 0);
384 if (program
->Base
.NumInstructions
> I915_MAX_INSN
) {
385 i915_program_error(p
, "Exceeded max instructions (%d out of %d)",
386 program
->Base
.NumInstructions
, I915_MAX_INSN
);
390 /* Not always needed:
392 if (!calc_live_regs(p
)) {
393 i915_program_error(p
, "Could not allocate registers");
398 GLuint src0
, src1
, src2
, flags
;
399 GLuint tmp
= 0, dst
, consts0
= 0, consts1
= 0;
401 switch (inst
->Opcode
) {
403 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
406 get_result_vector(p
, inst
),
407 get_result_flags(inst
), 0,
408 src0
, negate(src0
, 1, 1, 1, 1), 0);
412 EMIT_2ARG_ARITH(A0_ADD
);
416 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
417 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
418 src2
= src_vector(p
, &inst
->SrcReg
[2], program
);
419 i915_emit_arith(p
, A0_CMP
, get_result_vector(p
, inst
), get_result_flags(inst
), 0, src0
, src2
, src1
); /* NOTE: order of src2, src1 */
423 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
424 tmp
= i915_get_utemp(p
);
425 consts0
= i915_emit_const4fv(p
, sin_quad_constants
[0]);
426 consts1
= i915_emit_const4fv(p
, sin_quad_constants
[1]);
428 /* Reduce range from repeating about [-pi,pi] to [-1,1] */
431 tmp
, A0_DEST_CHANNEL_X
, 0,
433 swizzle(consts1
, Z
, ZERO
, ZERO
, ZERO
), /* 1/(2pi) */
434 swizzle(consts0
, W
, ZERO
, ZERO
, ZERO
)); /* .75 */
436 i915_emit_arith(p
, A0_FRC
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
440 tmp
, A0_DEST_CHANNEL_X
, 0,
442 swizzle(consts0
, X
, ZERO
, ZERO
, ZERO
), /* 2 */
443 swizzle(consts0
, Y
, ZERO
, ZERO
, ZERO
)); /* -1 */
445 /* Compute COS with the same calculation used for SIN, but a
446 * different source range has been mapped to [-1,1] this time.
449 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */
452 tmp
, A0_DEST_CHANNEL_Y
, 0,
453 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
454 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0),
457 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */
460 tmp
, A0_DEST_CHANNEL_Y
, 0,
461 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
465 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */
468 tmp
, A0_DEST_CHANNEL_X
, 0,
470 swizzle(consts1
, X
, Y
, ZERO
, ZERO
),
473 /* tmp.x now contains a first approximation (y). Now, weight it
474 * against tmp.y**2 to get closer.
478 tmp
, A0_DEST_CHANNEL_Y
, 0,
479 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
480 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0),
483 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */
486 tmp
, A0_DEST_CHANNEL_Y
, 0,
487 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
488 swizzle(tmp
, ZERO
, Y
, ZERO
, ZERO
),
489 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0));
491 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */
494 get_result_vector(p
, inst
),
495 get_result_flags(inst
), 0,
496 swizzle(consts1
, W
, W
, W
, W
),
497 swizzle(tmp
, Y
, Y
, Y
, Y
),
498 swizzle(tmp
, X
, X
, X
, X
));
502 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
503 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
506 get_result_vector(p
, inst
),
507 get_result_flags(inst
), 0,
508 swizzle(src0
, X
, Y
, ZERO
, ZERO
),
509 swizzle(src1
, X
, Y
, ZERO
, ZERO
),
514 EMIT_2ARG_ARITH(A0_DP3
);
518 EMIT_2ARG_ARITH(A0_DP4
);
522 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
523 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
527 get_result_vector(p
, inst
),
528 get_result_flags(inst
), 0,
529 swizzle(src0
, X
, Y
, Z
, ONE
), src1
, 0);
533 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
534 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
536 /* result[0] = 1 * 1;
537 * result[1] = a[1] * b[1];
538 * result[2] = a[2] * 1;
539 * result[3] = 1 * b[3];
543 get_result_vector(p
, inst
),
544 get_result_flags(inst
), 0,
545 swizzle(src0
, ONE
, Y
, Z
, ONE
),
546 swizzle(src1
, ONE
, Y
, ONE
, W
), 0);
550 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
554 get_result_vector(p
, inst
),
555 get_result_flags(inst
), 0,
556 swizzle(src0
, X
, X
, X
, X
), 0, 0);
560 EMIT_1ARG_ARITH(A0_FLR
);
564 EMIT_1ARG_ARITH(A0_TRC
);
568 EMIT_1ARG_ARITH(A0_FRC
);
572 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
573 tmp
= i915_get_utemp(p
);
575 i915_emit_texld(p
, get_live_regs(p
, inst
),
576 tmp
, A0_DEST_CHANNEL_ALL
, /* use a dummy dest reg */
577 0, src0
, T0_TEXKILL
);
581 if (inst
->DstReg
.CondMask
== COND_TR
) {
582 tmp
= i915_get_utemp(p
);
584 /* The KIL instruction discards the fragment if any component of
585 * the source is < 0. Emit an immediate operand of {-1}.xywz.
587 i915_emit_texld(p
, get_live_regs(p
, inst
),
588 tmp
, A0_DEST_CHANNEL_ALL
,
589 0, /* use a dummy dest reg */
590 negate(swizzle(tmp
, ONE
, ONE
, ONE
, ONE
),
595 i915_program_error(p
, "Unsupported KIL_NV condition code: %d",
596 inst
->DstReg
.CondMask
);
601 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
605 get_result_vector(p
, inst
),
606 get_result_flags(inst
), 0,
607 swizzle(src0
, X
, X
, X
, X
), 0, 0);
611 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
612 tmp
= i915_get_utemp(p
);
614 /* tmp = max( a.xyzw, a.00zw )
615 * XXX: Clamp tmp.w to -128..128
617 * tmp.y = tmp.w * tmp.y
619 * result = cmp (a.11-x1, a.1x01, a.1xy1 )
621 i915_emit_arith(p
, A0_MAX
, tmp
, A0_DEST_CHANNEL_ALL
, 0,
622 src0
, swizzle(src0
, ZERO
, ZERO
, Z
, W
), 0);
624 i915_emit_arith(p
, A0_LOG
, tmp
, A0_DEST_CHANNEL_Y
, 0,
625 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
627 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_Y
, 0,
628 swizzle(tmp
, ZERO
, Y
, ZERO
, ZERO
),
629 swizzle(tmp
, ZERO
, W
, ZERO
, ZERO
), 0);
631 i915_emit_arith(p
, A0_EXP
, tmp
, A0_DEST_CHANNEL_Y
, 0,
632 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
634 i915_emit_arith(p
, A0_CMP
,
635 get_result_vector(p
, inst
),
636 get_result_flags(inst
), 0,
637 negate(swizzle(tmp
, ONE
, ONE
, X
, ONE
), 0, 0, 1, 0),
638 swizzle(tmp
, ONE
, X
, ZERO
, ONE
),
639 swizzle(tmp
, ONE
, X
, Y
, ONE
));
644 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
645 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
646 src2
= src_vector(p
, &inst
->SrcReg
[2], program
);
647 flags
= get_result_flags(inst
);
648 tmp
= i915_get_utemp(p
);
655 * result = (-c)*a + tmp
657 i915_emit_arith(p
, A0_MAD
, tmp
,
658 flags
& A0_DEST_CHANNEL_ALL
, 0, src1
, src0
, src2
);
660 i915_emit_arith(p
, A0_MAD
,
661 get_result_vector(p
, inst
),
662 flags
, 0, negate(src2
, 1, 1, 1, 1), src0
, tmp
);
666 EMIT_3ARG_ARITH(A0_MAD
);
670 EMIT_2ARG_ARITH(A0_MAX
);
674 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
675 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
676 tmp
= i915_get_utemp(p
);
677 flags
= get_result_flags(inst
);
681 tmp
, flags
& A0_DEST_CHANNEL_ALL
, 0,
682 negate(src0
, 1, 1, 1, 1),
683 negate(src1
, 1, 1, 1, 1), 0);
687 get_result_vector(p
, inst
),
688 flags
, 0, negate(tmp
, 1, 1, 1, 1), 0, 0);
692 EMIT_1ARG_ARITH(A0_MOV
);
696 EMIT_2ARG_ARITH(A0_MUL
);
700 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
701 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
702 tmp
= i915_get_utemp(p
);
703 flags
= get_result_flags(inst
);
705 /* XXX: masking on intermediate values, here and elsewhere.
709 tmp
, A0_DEST_CHANNEL_X
, 0,
710 swizzle(src0
, X
, X
, X
, X
), 0, 0);
712 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, src1
, 0);
717 get_result_vector(p
, inst
),
718 flags
, 0, swizzle(tmp
, X
, X
, X
, X
), 0, 0);
723 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
727 get_result_vector(p
, inst
),
728 get_result_flags(inst
), 0,
729 swizzle(src0
, X
, X
, X
, X
), 0, 0);
734 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
738 get_result_vector(p
, inst
),
739 get_result_flags(inst
), 0,
740 swizzle(src0
, X
, X
, X
, X
), 0, 0);
744 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
745 tmp
= i915_get_utemp(p
);
748 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
749 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
750 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
751 * scs.x = DP4 t1, sin_constants
752 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
753 * scs.y = DP4 t1, cos_constants
757 tmp
, A0_DEST_CHANNEL_XY
, 0,
758 swizzle(src0
, X
, X
, ONE
, ONE
),
759 swizzle(src0
, X
, ONE
, ONE
, ONE
), 0);
763 tmp
, A0_DEST_CHANNEL_ALL
, 0,
764 swizzle(tmp
, X
, Y
, X
, Y
),
765 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
767 if (inst
->DstReg
.WriteMask
& WRITEMASK_Y
) {
770 if (inst
->DstReg
.WriteMask
& WRITEMASK_X
)
771 tmp1
= i915_get_utemp(p
);
777 tmp1
, A0_DEST_CHANNEL_ALL
, 0,
778 swizzle(tmp
, X
, Y
, Y
, W
),
779 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
783 get_result_vector(p
, inst
),
784 A0_DEST_CHANNEL_Y
, 0,
785 swizzle(tmp1
, W
, Z
, Y
, X
),
786 i915_emit_const4fv(p
, sin_constants
), 0);
789 if (inst
->DstReg
.WriteMask
& WRITEMASK_X
) {
792 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
793 swizzle(tmp
, X
, X
, Z
, ONE
),
794 swizzle(tmp
, Z
, ONE
, ONE
, ONE
), 0);
798 get_result_vector(p
, inst
),
799 A0_DEST_CHANNEL_X
, 0,
800 swizzle(tmp
, ONE
, Z
, Y
, X
),
801 i915_emit_const4fv(p
, cos_constants
), 0);
806 tmp
= i915_get_utemp(p
);
807 flags
= get_result_flags(inst
);
808 dst
= get_result_vector(p
, inst
);
810 /* If both operands are uniforms or constants, we get 5 instructions
813 * U[1] = MOV CONST[1]
814 * U[0].xyz = SGE CONST[0].xxxx, U[1]
815 * U[1] = MOV CONST[1].-x-y-z-w
816 * R[0].xyz = SGE CONST[0].-x-x-x-x, U[1]
817 * R[0].xyz = MUL R[0], U[0]
819 * This code is stupid. Instead of having the individual calls to
820 * i915_emit_arith generate the moves to utemps, do it in the caller.
821 * This results in code like:
823 * U[1] = MOV CONST[1]
824 * U[0].xyz = SGE CONST[0].xxxx, U[1]
825 * R[0].xyz = SGE CONST[0].-x-x-x-x, U[1].-x-y-z-w
826 * R[0].xyz = MUL R[0], U[0]
828 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
829 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
831 if (GET_UREG_TYPE(src0
) == REG_TYPE_CONST
832 && GET_UREG_TYPE(src1
) == REG_TYPE_CONST
) {
833 unsigned tmp
= i915_get_utemp(p
);
835 i915_emit_arith(p
, A0_MOV
, tmp
, A0_DEST_CHANNEL_ALL
, 0,
841 /* tmp = src1 >= src2 */
849 /* dst = src1 <= src2 */
854 negate(src0
, 1, 1, 1, 1),
855 negate(src1
, 1, 1, 1, 1),
857 /* dst = tmp && dst */
868 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
869 tmp
= i915_get_utemp(p
);
870 consts0
= i915_emit_const4fv(p
, sin_quad_constants
[0]);
871 consts1
= i915_emit_const4fv(p
, sin_quad_constants
[1]);
873 /* Reduce range from repeating about [-pi,pi] to [-1,1] */
876 tmp
, A0_DEST_CHANNEL_X
, 0,
878 swizzle(consts1
, Z
, ZERO
, ZERO
, ZERO
), /* 1/(2pi) */
879 swizzle(consts0
, Z
, ZERO
, ZERO
, ZERO
)); /* .5 */
881 i915_emit_arith(p
, A0_FRC
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
885 tmp
, A0_DEST_CHANNEL_X
, 0,
887 swizzle(consts0
, X
, ZERO
, ZERO
, ZERO
), /* 2 */
888 swizzle(consts0
, Y
, ZERO
, ZERO
, ZERO
)); /* -1 */
890 /* Compute sin using a quadratic and quartic. It gives continuity
891 * that repeating the Taylor series lacks every 2*pi, and has
894 * The idea was described at:
895 * http://www.devmaster.net/forums/showthread.php?t=5784
898 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */
901 tmp
, A0_DEST_CHANNEL_Y
, 0,
902 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
903 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0),
906 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */
909 tmp
, A0_DEST_CHANNEL_Y
, 0,
910 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
914 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */
917 tmp
, A0_DEST_CHANNEL_X
, 0,
919 swizzle(consts1
, X
, Y
, ZERO
, ZERO
),
922 /* tmp.x now contains a first approximation (y). Now, weight it
923 * against tmp.y**2 to get closer.
927 tmp
, A0_DEST_CHANNEL_Y
, 0,
928 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
929 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0),
932 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */
935 tmp
, A0_DEST_CHANNEL_Y
, 0,
936 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
937 swizzle(tmp
, ZERO
, Y
, ZERO
, ZERO
),
938 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0));
940 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */
943 get_result_vector(p
, inst
),
944 get_result_flags(inst
), 0,
945 swizzle(consts1
, W
, W
, W
, W
),
946 swizzle(tmp
, Y
, Y
, Y
, Y
),
947 swizzle(tmp
, X
, X
, X
, X
));
952 EMIT_2ARG_ARITH(A0_SGE
);
958 get_result_vector( p
, inst
),
959 get_result_flags( inst
), 0,
960 negate(src_vector( p
, &inst
->SrcReg
[0], program
),
962 negate(src_vector( p
, &inst
->SrcReg
[1], program
),
970 get_result_vector( p
, inst
),
971 get_result_flags( inst
), 0,
972 negate(src_vector( p
, &inst
->SrcReg
[0], program
),
974 negate(src_vector( p
, &inst
->SrcReg
[1], program
),
980 EMIT_2ARG_ARITH(A0_SLT
);
984 tmp
= i915_get_utemp(p
);
985 flags
= get_result_flags(inst
);
986 dst
= get_result_vector(p
, inst
);
988 /* If both operands are uniforms or constants, we get 5 instructions
991 * U[1] = MOV CONST[1]
992 * U[0].xyz = SLT CONST[0].xxxx, U[1]
993 * U[1] = MOV CONST[1].-x-y-z-w
994 * R[0].xyz = SLT CONST[0].-x-x-x-x, U[1]
995 * R[0].xyz = MUL R[0], U[0]
997 * This code is stupid. Instead of having the individual calls to
998 * i915_emit_arith generate the moves to utemps, do it in the caller.
999 * This results in code like:
1001 * U[1] = MOV CONST[1]
1002 * U[0].xyz = SLT CONST[0].xxxx, U[1]
1003 * R[0].xyz = SLT CONST[0].-x-x-x-x, U[1].-x-y-z-w
1004 * R[0].xyz = MUL R[0], U[0]
1006 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
1007 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
1009 if (GET_UREG_TYPE(src0
) == REG_TYPE_CONST
1010 && GET_UREG_TYPE(src1
) == REG_TYPE_CONST
) {
1011 unsigned tmp
= i915_get_utemp(p
);
1013 i915_emit_arith(p
, A0_MOV
, tmp
, A0_DEST_CHANNEL_ALL
, 0,
1019 /* tmp = src1 < src2 */
1027 /* dst = src1 > src2 */
1032 negate(src0
, 1, 1, 1, 1),
1033 negate(src1
, 1, 1, 1, 1),
1035 /* dst = tmp || dst */
1039 flags
| A0_DEST_SATURATE
, 0,
1046 dst
= get_result_vector(p
, inst
);
1047 flags
= get_result_flags(inst
);
1048 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
1049 tmp
= i915_get_utemp(p
);
1051 /* tmp = (src < 0.0) */
1057 swizzle(src0
, ZERO
, ZERO
, ZERO
, ZERO
),
1060 /* dst = (0.0 < src) */
1065 swizzle(src0
, ZERO
, ZERO
, ZERO
, ZERO
),
1069 /* dst = (src > 0.0) - (src < 0.0) */
1075 negate(tmp
, 1, 1, 1, 1),
1081 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
1082 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
1086 get_result_vector(p
, inst
),
1087 get_result_flags(inst
), 0,
1088 src0
, negate(src1
, 1, 1, 1, 1), 0);
1092 EMIT_1ARG_ARITH(A0_MOV
); /* extended swizzle handled natively */
1100 EMIT_TEX(T0_TEXLDB
);
1104 EMIT_TEX(T0_TEXLDP
);
1109 * result.x = src0.y * src1.z - src0.z * src1.y;
1110 * result.y = src0.z * src1.x - src0.x * src1.z;
1111 * result.z = src0.x * src1.y - src0.y * src1.x;
1114 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
1115 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
1116 tmp
= i915_get_utemp(p
);
1120 tmp
, A0_DEST_CHANNEL_ALL
, 0,
1121 swizzle(src0
, Z
, X
, Y
, ONE
),
1122 swizzle(src1
, Y
, Z
, X
, ONE
), 0);
1126 get_result_vector(p
, inst
),
1127 get_result_flags(inst
), 0,
1128 swizzle(src0
, Y
, Z
, X
, ONE
),
1129 swizzle(src1
, Z
, X
, Y
, ONE
),
1130 negate(tmp
, 1, 1, 1, 0));
1136 case OPCODE_BGNLOOP
:
1145 case OPCODE_ENDLOOP
:
1150 i915_program_error(p
, "Unsupported opcode: %s",
1151 _mesa_opcode_string(inst
->Opcode
));
1156 /* These opcodes are claimed as GLSL, NV_vp, and ARB_vp in
1157 * prog_instruction.h, but apparently GLSL doesn't ever emit them.
1158 * Instead, it translates to EX2 or LG2.
1162 /* These opcodes are claimed by GLSL in prog_instruction.h, but
1163 * only NV_vp/fp appears to emit them.
1166 i915_program_error(p
, "bad opcode: %s",
1167 _mesa_opcode_string(inst
->Opcode
));
1172 i915_release_utemps(p
);
1176 /* Rather than trying to intercept and jiggle depth writes during
1177 * emit, just move the value into its correct position at the end of
1181 fixup_depth_write(struct i915_fragment_program
*p
)
1183 if (p
->depth_written
) {
1184 GLuint depth
= UREG(REG_TYPE_OD
, 0);
1188 depth
, A0_DEST_CHANNEL_W
, 0,
1189 swizzle(depth
, X
, Y
, Z
, Z
), 0, 0);
1195 check_wpos(struct i915_fragment_program
*p
)
1197 GLbitfield64 inputs
= p
->FragProg
.Base
.InputsRead
;
1202 for (i
= 0; i
< p
->ctx
->Const
.MaxTextureCoordUnits
; i
++) {
1203 if (inputs
& (VARYING_BIT_TEX(i
) | VARYING_BIT_VAR(i
)))
1205 else if (inputs
& VARYING_BIT_POS
) {
1207 inputs
&= ~VARYING_BIT_POS
;
1211 if (inputs
& VARYING_BIT_POS
) {
1212 i915_program_error(p
, "No free texcoord for wpos value");
1218 translate_program(struct i915_fragment_program
*p
)
1220 struct i915_context
*i915
= I915_CONTEXT(p
->ctx
);
1222 if (INTEL_DEBUG
& DEBUG_WM
) {
1224 _mesa_print_program(&p
->FragProg
.Base
);
1228 i915_init_program(i915
, p
);
1231 fixup_depth_write(p
);
1232 i915_fini_program(p
);
1239 track_params(struct i915_fragment_program
*p
)
1244 _mesa_load_state_parameters(p
->ctx
, p
->FragProg
.Base
.Parameters
);
1246 for (i
= 0; i
< p
->nr_params
; i
++) {
1247 GLint reg
= p
->param
[i
].reg
;
1248 COPY_4V(p
->constant
[reg
], p
->param
[i
].values
);
1251 p
->params_uptodate
= 1;
1252 p
->on_hardware
= 0; /* overkill */
1257 i915BindProgram(struct gl_context
* ctx
, GLenum target
, struct gl_program
*prog
)
1259 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
1260 struct i915_context
*i915
= I915_CONTEXT(ctx
);
1261 struct i915_fragment_program
*p
= (struct i915_fragment_program
*) prog
;
1263 if (i915
->current_program
== p
)
1266 if (i915
->current_program
) {
1267 i915
->current_program
->on_hardware
= 0;
1268 i915
->current_program
->params_uptodate
= 0;
1271 i915
->current_program
= p
;
1273 assert(p
->on_hardware
== 0);
1274 assert(p
->params_uptodate
== 0);
1279 static struct gl_program
*
1280 i915NewProgram(struct gl_context
* ctx
, GLenum target
, GLuint id
)
1283 case GL_VERTEX_PROGRAM_ARB
:
1284 return _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
1287 case GL_FRAGMENT_PROGRAM_ARB
:{
1288 struct i915_fragment_program
*prog
=
1289 CALLOC_STRUCT(i915_fragment_program
);
1291 i915_init_program(I915_CONTEXT(ctx
), prog
);
1293 return _mesa_init_fragment_program(ctx
, &prog
->FragProg
,
1303 return _mesa_new_program(ctx
, target
, id
);
1308 i915DeleteProgram(struct gl_context
* ctx
, struct gl_program
*prog
)
1310 if (prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1311 struct i915_context
*i915
= I915_CONTEXT(ctx
);
1312 struct i915_fragment_program
*p
= (struct i915_fragment_program
*) prog
;
1314 if (i915
->current_program
== p
)
1315 i915
->current_program
= 0;
1318 _mesa_delete_program(ctx
, prog
);
1323 i915IsProgramNative(struct gl_context
* ctx
, GLenum target
, struct gl_program
*prog
)
1325 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
1326 struct i915_fragment_program
*p
= (struct i915_fragment_program
*) prog
;
1329 translate_program(p
);
1338 i915ProgramStringNotify(struct gl_context
* ctx
,
1339 GLenum target
, struct gl_program
*prog
)
1341 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
1342 struct i915_fragment_program
*p
= (struct i915_fragment_program
*) prog
;
1346 (void) _tnl_program_string(ctx
, target
, prog
);
1348 /* XXX check if program is legal, within limits */
1353 i915SamplerUniformChange(struct gl_context
*ctx
,
1354 GLenum target
, struct gl_program
*prog
)
1356 i915ProgramStringNotify(ctx
, target
, prog
);
1360 i915_update_program(struct gl_context
*ctx
)
1362 struct intel_context
*intel
= intel_context(ctx
);
1363 struct i915_context
*i915
= i915_context(&intel
->ctx
);
1364 struct i915_fragment_program
*fp
=
1365 (struct i915_fragment_program
*) ctx
->FragmentProgram
._Current
;
1367 if (i915
->current_program
!= fp
) {
1368 if (i915
->current_program
) {
1369 i915
->current_program
->on_hardware
= 0;
1370 i915
->current_program
->params_uptodate
= 0;
1373 i915
->current_program
= fp
;
1376 if (!fp
->translated
)
1377 translate_program(fp
);
1379 FALLBACK(&i915
->intel
, I915_FALLBACK_PROGRAM
, fp
->error
);
1383 i915ValidateFragmentProgram(struct i915_context
*i915
)
1385 struct gl_context
*ctx
= &i915
->intel
.ctx
;
1386 struct intel_context
*intel
= intel_context(ctx
);
1387 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1388 struct vertex_buffer
*VB
= &tnl
->vb
;
1390 struct i915_fragment_program
*p
=
1391 (struct i915_fragment_program
*) ctx
->FragmentProgram
._Current
;
1393 const GLbitfield64 inputsRead
= p
->FragProg
.Base
.InputsRead
;
1394 GLuint s4
= i915
->state
.Ctx
[I915_CTXREG_LIS4
] & ~S4_VFMT_MASK
;
1395 GLuint s2
= S2_TEXCOORD_NONE
;
1400 VB
->AttribPtr
[VERT_ATTRIB_POS
] = VB
->NdcPtr
;
1403 translate_program(p
);
1405 intel
->vertex_attr_count
= 0;
1406 intel
->wpos_offset
= 0;
1407 intel
->coloroffset
= 0;
1408 intel
->specoffset
= 0;
1410 if (inputsRead
& VARYING_BITS_TEX_ANY
|| p
->wpos_tex
!= -1) {
1411 EMIT_ATTR(_TNL_ATTRIB_POS
, EMIT_4F_VIEWPORT
, S4_VFMT_XYZW
, 16);
1414 EMIT_ATTR(_TNL_ATTRIB_POS
, EMIT_3F_VIEWPORT
, S4_VFMT_XYZ
, 12);
1417 /* Handle gl_PointSize builtin var here */
1418 if (ctx
->Point
._Attenuated
|| ctx
->VertexProgram
.PointSizeEnabled
)
1419 EMIT_ATTR(_TNL_ATTRIB_POINTSIZE
, EMIT_1F
, S4_VFMT_POINT_WIDTH
, 4);
1421 if (inputsRead
& VARYING_BIT_COL0
) {
1422 intel
->coloroffset
= offset
/ 4;
1423 EMIT_ATTR(_TNL_ATTRIB_COLOR0
, EMIT_4UB_4F_BGRA
, S4_VFMT_COLOR
, 4);
1426 if (inputsRead
& VARYING_BIT_COL1
) {
1427 intel
->specoffset
= offset
/ 4;
1428 EMIT_ATTR(_TNL_ATTRIB_COLOR1
, EMIT_4UB_4F_BGRA
, S4_VFMT_SPEC_FOG
, 4);
1431 if ((inputsRead
& VARYING_BIT_FOGC
)) {
1432 EMIT_ATTR(_TNL_ATTRIB_FOG
, EMIT_1F
, S4_VFMT_FOG_PARAM
, 4);
1435 for (i
= 0; i
< p
->ctx
->Const
.MaxTextureCoordUnits
; i
++) {
1436 if (inputsRead
& VARYING_BIT_TEX(i
)) {
1437 int sz
= VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
]->size
;
1439 s2
&= ~S2_TEXCOORD_FMT(i
, S2_TEXCOORD_FMT0_MASK
);
1440 s2
|= S2_TEXCOORD_FMT(i
, SZ_TO_HW(sz
));
1442 EMIT_ATTR(_TNL_ATTRIB_TEX0
+ i
, EMIT_SZ(sz
), 0, sz
* 4);
1444 else if (inputsRead
& VARYING_BIT_VAR(i
)) {
1445 int sz
= VB
->AttribPtr
[_TNL_ATTRIB_GENERIC0
+ i
]->size
;
1447 s2
&= ~S2_TEXCOORD_FMT(i
, S2_TEXCOORD_FMT0_MASK
);
1448 s2
|= S2_TEXCOORD_FMT(i
, SZ_TO_HW(sz
));
1450 EMIT_ATTR(_TNL_ATTRIB_GENERIC0
+ i
, EMIT_SZ(sz
), 0, sz
* 4);
1452 else if (i
== p
->wpos_tex
) {
1453 int wpos_size
= 4 * sizeof(float);
1454 /* If WPOS is required, duplicate the XYZ position data in an
1455 * unused texture coordinate:
1457 s2
&= ~S2_TEXCOORD_FMT(i
, S2_TEXCOORD_FMT0_MASK
);
1458 s2
|= S2_TEXCOORD_FMT(i
, SZ_TO_HW(wpos_size
));
1460 intel
->wpos_offset
= offset
;
1461 EMIT_PAD(wpos_size
);
1465 if (s2
!= i915
->state
.Ctx
[I915_CTXREG_LIS2
] ||
1466 s4
!= i915
->state
.Ctx
[I915_CTXREG_LIS4
]) {
1469 I915_STATECHANGE(i915
, I915_UPLOAD_CTX
);
1471 /* Must do this *after* statechange, so as not to affect
1472 * buffered vertices reliant on the old state:
1474 intel
->vertex_size
= _tnl_install_attrs(&intel
->ctx
,
1475 intel
->vertex_attrs
,
1476 intel
->vertex_attr_count
,
1477 intel
->ViewportMatrix
.m
, 0);
1479 assert(intel
->prim
.current_offset
== intel
->prim
.start_offset
);
1480 intel
->prim
.start_offset
= (intel
->prim
.current_offset
+ intel
->vertex_size
-1) / intel
->vertex_size
* intel
->vertex_size
;
1481 intel
->prim
.current_offset
= intel
->prim
.start_offset
;
1483 intel
->vertex_size
>>= 2;
1485 i915
->state
.Ctx
[I915_CTXREG_LIS2
] = s2
;
1486 i915
->state
.Ctx
[I915_CTXREG_LIS4
] = s4
;
1488 k
= intel
->vtbl
.check_vertex_size(intel
, intel
->vertex_size
);
1492 if (!p
->params_uptodate
)
1495 if (!p
->on_hardware
)
1496 i915_upload_program(i915
, p
);
1498 if (INTEL_DEBUG
& DEBUG_WM
) {
1500 i915_disassemble_program(i915
->state
.Program
, i915
->state
.ProgramSize
);
1505 i915InitFragProgFuncs(struct dd_function_table
*functions
)
1507 functions
->BindProgram
= i915BindProgram
;
1508 functions
->NewProgram
= i915NewProgram
;
1509 functions
->DeleteProgram
= i915DeleteProgram
;
1510 functions
->IsProgramNative
= i915IsProgramNative
;
1511 functions
->ProgramStringNotify
= i915ProgramStringNotify
;
1512 functions
->SamplerUniformChange
= i915SamplerUniformChange
;