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)
75 /* texcoord_mapping[unit] = index | TEXCOORD_{TEX,VAR} */
76 #define TEXCOORD_TEX (0<<7)
77 #define TEXCOORD_VAR (1<<7)
80 get_texcoord_mapping(struct i915_fragment_program
*p
, uint8_t texcoord
)
82 for (unsigned i
= 0; i
< p
->ctx
->Const
.MaxTextureCoordUnits
; i
++) {
83 if (p
->texcoord_mapping
[i
] == texcoord
)
88 return p
->ctx
->Const
.MaxTextureCoordUnits
- 1;
92 * Retrieve a ureg for the given source register. Will emit
93 * constants, apply swizzling and negation as needed.
96 src_vector(struct i915_fragment_program
*p
,
97 const struct prog_src_register
*source
,
98 const struct gl_program
*program
)
103 switch (source
->File
) {
107 case PROGRAM_TEMPORARY
:
108 if (source
->Index
>= I915_MAX_TEMPORARY
) {
109 i915_program_error(p
, "Exceeded max temporary reg: %d/%d",
110 source
->Index
, I915_MAX_TEMPORARY
);
113 src
= UREG(REG_TYPE_R
, source
->Index
);
116 switch (source
->Index
) {
117 case VARYING_SLOT_POS
:
118 src
= i915_emit_decl(p
, REG_TYPE_T
, p
->wpos_tex
, D0_CHANNEL_ALL
);
120 case VARYING_SLOT_COL0
:
121 src
= i915_emit_decl(p
, REG_TYPE_T
, T_DIFFUSE
, D0_CHANNEL_ALL
);
123 case VARYING_SLOT_COL1
:
124 src
= i915_emit_decl(p
, REG_TYPE_T
, T_SPECULAR
, D0_CHANNEL_XYZ
);
125 src
= swizzle(src
, X
, Y
, Z
, ONE
);
127 case VARYING_SLOT_FOGC
:
128 src
= i915_emit_decl(p
, REG_TYPE_T
, T_FOG_W
, D0_CHANNEL_W
);
129 src
= swizzle(src
, W
, ZERO
, ZERO
, ONE
);
131 case VARYING_SLOT_TEX0
:
132 case VARYING_SLOT_TEX1
:
133 case VARYING_SLOT_TEX2
:
134 case VARYING_SLOT_TEX3
:
135 case VARYING_SLOT_TEX4
:
136 case VARYING_SLOT_TEX5
:
137 case VARYING_SLOT_TEX6
:
138 case VARYING_SLOT_TEX7
:
139 unit
= get_texcoord_mapping(p
, (source
->Index
-
140 VARYING_SLOT_TEX0
) | TEXCOORD_TEX
);
141 src
= i915_emit_decl(p
, REG_TYPE_T
,
146 case VARYING_SLOT_VAR0
:
147 case VARYING_SLOT_VAR0
+ 1:
148 case VARYING_SLOT_VAR0
+ 2:
149 case VARYING_SLOT_VAR0
+ 3:
150 case VARYING_SLOT_VAR0
+ 4:
151 case VARYING_SLOT_VAR0
+ 5:
152 case VARYING_SLOT_VAR0
+ 6:
153 case VARYING_SLOT_VAR0
+ 7:
154 unit
= get_texcoord_mapping(p
, (source
->Index
-
155 VARYING_SLOT_VAR0
) | TEXCOORD_VAR
);
156 src
= i915_emit_decl(p
, REG_TYPE_T
,
162 i915_program_error(p
, "Bad source->Index: %d", source
->Index
);
168 switch (source
->Index
) {
169 case FRAG_RESULT_COLOR
:
170 case FRAG_RESULT_DATA0
:
171 src
= UREG(REG_TYPE_OC
, 0);
173 case FRAG_RESULT_DEPTH
:
174 src
= UREG(REG_TYPE_OD
, 0);
177 i915_program_error(p
, "Bad source->Index: %d", source
->Index
);
182 /* Various paramters and env values. All emitted to
183 * hardware as program constants.
185 case PROGRAM_CONSTANT
:
186 case PROGRAM_STATE_VAR
:
187 case PROGRAM_UNIFORM
: {
188 struct gl_program_parameter_list
*params
= program
->Parameters
;
189 unsigned offset
= params
->ParameterValueOffset
[source
->Index
];
190 src
= i915_emit_param4fv(p
, ¶ms
->ParameterValues
[offset
].f
);
194 i915_program_error(p
, "Bad source->File: %d", source
->File
);
199 GET_SWZ(source
->Swizzle
, 0),
200 GET_SWZ(source
->Swizzle
, 1),
201 GET_SWZ(source
->Swizzle
, 2), GET_SWZ(source
->Swizzle
, 3));
205 GET_BIT(source
->Negate
, 0),
206 GET_BIT(source
->Negate
, 1),
207 GET_BIT(source
->Negate
, 2),
208 GET_BIT(source
->Negate
, 3));
215 get_result_vector(struct i915_fragment_program
*p
,
216 const struct prog_instruction
*inst
)
218 switch (inst
->DstReg
.File
) {
220 switch (inst
->DstReg
.Index
) {
221 case FRAG_RESULT_COLOR
:
222 case FRAG_RESULT_DATA0
:
223 return UREG(REG_TYPE_OC
, 0);
224 case FRAG_RESULT_DEPTH
:
225 p
->depth_written
= 1;
226 return UREG(REG_TYPE_OD
, 0);
228 i915_program_error(p
, "Bad inst->DstReg.Index: %d",
232 case PROGRAM_TEMPORARY
:
233 return UREG(REG_TYPE_R
, inst
->DstReg
.Index
);
235 i915_program_error(p
, "Bad inst->DstReg.File: %d", inst
->DstReg
.File
);
241 get_result_flags(const struct prog_instruction
*inst
)
246 flags
|= A0_DEST_SATURATE
;
247 if (inst
->DstReg
.WriteMask
& WRITEMASK_X
)
248 flags
|= A0_DEST_CHANNEL_X
;
249 if (inst
->DstReg
.WriteMask
& WRITEMASK_Y
)
250 flags
|= A0_DEST_CHANNEL_Y
;
251 if (inst
->DstReg
.WriteMask
& WRITEMASK_Z
)
252 flags
|= A0_DEST_CHANNEL_Z
;
253 if (inst
->DstReg
.WriteMask
& WRITEMASK_W
)
254 flags
|= A0_DEST_CHANNEL_W
;
260 translate_tex_src_target(struct i915_fragment_program
*p
, GLubyte bit
)
263 case TEXTURE_1D_INDEX
:
264 return D0_SAMPLE_TYPE_2D
;
265 case TEXTURE_2D_INDEX
:
266 return D0_SAMPLE_TYPE_2D
;
267 case TEXTURE_RECT_INDEX
:
268 return D0_SAMPLE_TYPE_2D
;
269 case TEXTURE_3D_INDEX
:
270 return D0_SAMPLE_TYPE_VOLUME
;
271 case TEXTURE_CUBE_INDEX
:
272 return D0_SAMPLE_TYPE_CUBE
;
274 i915_program_error(p
, "TexSrcBit: %d", bit
);
279 #define EMIT_TEX( OP ) \
281 GLuint dim = translate_tex_src_target( p, inst->TexSrcTarget ); \
282 const struct gl_program *program = &p->FragProg; \
283 GLuint unit = program->SamplerUnits[inst->TexSrcUnit]; \
284 GLuint sampler = i915_emit_decl(p, REG_TYPE_S, \
286 GLuint coord = src_vector( p, &inst->SrcReg[0], program); \
289 i915_emit_texld( p, get_live_regs(p, inst), \
290 get_result_vector( p, inst ), \
291 get_result_flags( inst ), \
297 #define EMIT_ARITH( OP, N ) \
299 i915_emit_arith( p, \
301 get_result_vector( p, inst ), \
302 get_result_flags( inst ), 0, \
303 (N<1)?0:src_vector( p, &inst->SrcReg[0], program), \
304 (N<2)?0:src_vector( p, &inst->SrcReg[1], program), \
305 (N<3)?0:src_vector( p, &inst->SrcReg[2], program)); \
308 #define EMIT_1ARG_ARITH( OP ) EMIT_ARITH( OP, 1 )
309 #define EMIT_2ARG_ARITH( OP ) EMIT_ARITH( OP, 2 )
310 #define EMIT_3ARG_ARITH( OP ) EMIT_ARITH( OP, 3 )
313 * TODO: consider moving this into core
315 static bool calc_live_regs( struct i915_fragment_program
*p
)
317 const struct gl_program
*program
= &p
->FragProg
;
318 GLuint regsUsed
= ~((1 << I915_MAX_TEMPORARY
) - 1);
319 uint8_t live_components
[I915_MAX_TEMPORARY
] = { 0, };
322 for (i
= program
->arb
.NumInstructions
- 1; i
>= 0; i
--) {
323 struct prog_instruction
*inst
= &program
->arb
.Instructions
[i
];
324 int opArgs
= _mesa_num_inst_src_regs(inst
->Opcode
);
327 /* Register is written to: unmark as live for this and preceeding ops */
328 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
) {
329 if (inst
->DstReg
.Index
>= I915_MAX_TEMPORARY
)
332 live_components
[inst
->DstReg
.Index
] &= ~inst
->DstReg
.WriteMask
;
333 if (live_components
[inst
->DstReg
.Index
] == 0)
334 regsUsed
&= ~(1 << inst
->DstReg
.Index
);
337 for (a
= 0; a
< opArgs
; a
++) {
338 /* Register is read from: mark as live for this and preceeding ops */
339 if (inst
->SrcReg
[a
].File
== PROGRAM_TEMPORARY
) {
342 if (inst
->SrcReg
[a
].Index
>= I915_MAX_TEMPORARY
)
345 regsUsed
|= 1 << inst
->SrcReg
[a
].Index
;
347 for (c
= 0; c
< 4; c
++) {
348 const unsigned field
= GET_SWZ(inst
->SrcReg
[a
].Swizzle
, c
);
350 if (field
<= SWIZZLE_W
)
351 live_components
[inst
->SrcReg
[a
].Index
] |= (1U << field
);
356 p
->usedRegs
[i
] = regsUsed
;
362 static GLuint
get_live_regs( struct i915_fragment_program
*p
,
363 const struct prog_instruction
*inst
)
365 const struct gl_program
*program
= &p
->FragProg
;
366 GLuint nr
= inst
- program
->arb
.Instructions
;
368 return p
->usedRegs
[nr
];
372 /* Possible concerns:
374 * SIN, COS -- could use another taylor step?
375 * LIT -- results seem a little different to sw mesa
376 * LOG -- different to mesa on negative numbers, but this is conformant.
378 * Parse failures -- Mesa doesn't currently give a good indication
379 * internally whether a particular program string parsed or not. This
380 * can lead to confusion -- hopefully we cope with it ok now.
384 upload_program(struct i915_fragment_program
*p
)
386 const struct gl_program
*program
= &p
->FragProg
;
387 const struct prog_instruction
*inst
= program
->arb
.Instructions
;
389 if (INTEL_DEBUG
& DEBUG_WM
)
390 _mesa_print_program(program
);
392 /* Is this a parse-failed program? Ensure a valid program is
393 * loaded, as the flagging of an error isn't sufficient to stop
394 * this being uploaded to hardware.
396 if (inst
[0].Opcode
== OPCODE_END
) {
397 GLuint tmp
= i915_get_utemp(p
);
400 UREG(REG_TYPE_OC
, 0),
401 A0_DEST_CHANNEL_ALL
, 0,
402 swizzle(tmp
, ONE
, ZERO
, ONE
, ONE
), 0, 0);
406 if (program
->arb
.NumInstructions
> I915_MAX_INSN
) {
407 i915_program_error(p
, "Exceeded max instructions (%d out of %d)",
408 program
->arb
.NumInstructions
, I915_MAX_INSN
);
412 /* Not always needed:
414 if (!calc_live_regs(p
)) {
415 i915_program_error(p
, "Could not allocate registers");
420 GLuint src0
, src1
, src2
, flags
;
421 GLuint tmp
= 0, dst
, consts0
= 0, consts1
= 0;
423 switch (inst
->Opcode
) {
425 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
428 get_result_vector(p
, inst
),
429 get_result_flags(inst
), 0,
430 src0
, negate(src0
, 1, 1, 1, 1), 0);
434 EMIT_2ARG_ARITH(A0_ADD
);
438 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
439 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
440 src2
= src_vector(p
, &inst
->SrcReg
[2], program
);
441 i915_emit_arith(p
, A0_CMP
, get_result_vector(p
, inst
), get_result_flags(inst
), 0, src0
, src2
, src1
); /* NOTE: order of src2, src1 */
445 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
446 tmp
= i915_get_utemp(p
);
447 consts0
= i915_emit_const4fv(p
, sin_quad_constants
[0]);
448 consts1
= i915_emit_const4fv(p
, sin_quad_constants
[1]);
450 /* Reduce range from repeating about [-pi,pi] to [-1,1] */
453 tmp
, A0_DEST_CHANNEL_X
, 0,
455 swizzle(consts1
, Z
, ZERO
, ZERO
, ZERO
), /* 1/(2pi) */
456 swizzle(consts0
, W
, ZERO
, ZERO
, ZERO
)); /* .75 */
458 i915_emit_arith(p
, A0_FRC
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
462 tmp
, A0_DEST_CHANNEL_X
, 0,
464 swizzle(consts0
, X
, ZERO
, ZERO
, ZERO
), /* 2 */
465 swizzle(consts0
, Y
, ZERO
, ZERO
, ZERO
)); /* -1 */
467 /* Compute COS with the same calculation used for SIN, but a
468 * different source range has been mapped to [-1,1] this time.
471 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */
474 tmp
, A0_DEST_CHANNEL_Y
, 0,
475 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
476 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0),
479 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */
482 tmp
, A0_DEST_CHANNEL_Y
, 0,
483 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
487 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */
490 tmp
, A0_DEST_CHANNEL_X
, 0,
492 swizzle(consts1
, X
, Y
, ZERO
, ZERO
),
495 /* tmp.x now contains a first approximation (y). Now, weight it
496 * against tmp.y**2 to get closer.
500 tmp
, A0_DEST_CHANNEL_Y
, 0,
501 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
502 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0),
505 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */
508 tmp
, A0_DEST_CHANNEL_Y
, 0,
509 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
510 swizzle(tmp
, ZERO
, Y
, ZERO
, ZERO
),
511 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0));
513 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */
516 get_result_vector(p
, inst
),
517 get_result_flags(inst
), 0,
518 swizzle(consts1
, W
, W
, W
, W
),
519 swizzle(tmp
, Y
, Y
, Y
, Y
),
520 swizzle(tmp
, X
, X
, X
, X
));
524 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
525 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
528 get_result_vector(p
, inst
),
529 get_result_flags(inst
), 0,
530 swizzle(src0
, X
, Y
, ZERO
, ZERO
),
531 swizzle(src1
, X
, Y
, ZERO
, ZERO
),
536 EMIT_2ARG_ARITH(A0_DP3
);
540 EMIT_2ARG_ARITH(A0_DP4
);
544 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
545 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
549 get_result_vector(p
, inst
),
550 get_result_flags(inst
), 0,
551 swizzle(src0
, X
, Y
, Z
, ONE
), src1
, 0);
555 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
556 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
558 /* result[0] = 1 * 1;
559 * result[1] = a[1] * b[1];
560 * result[2] = a[2] * 1;
561 * result[3] = 1 * b[3];
565 get_result_vector(p
, inst
),
566 get_result_flags(inst
), 0,
567 swizzle(src0
, ONE
, Y
, Z
, ONE
),
568 swizzle(src1
, ONE
, Y
, ONE
, W
), 0);
572 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
576 get_result_vector(p
, inst
),
577 get_result_flags(inst
), 0,
578 swizzle(src0
, X
, X
, X
, X
), 0, 0);
582 EMIT_1ARG_ARITH(A0_FLR
);
586 EMIT_1ARG_ARITH(A0_TRC
);
590 EMIT_1ARG_ARITH(A0_FRC
);
594 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
595 tmp
= i915_get_utemp(p
);
597 i915_emit_texld(p
, get_live_regs(p
, inst
),
598 tmp
, A0_DEST_CHANNEL_ALL
, /* use a dummy dest reg */
599 0, src0
, T0_TEXKILL
);
603 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
607 get_result_vector(p
, inst
),
608 get_result_flags(inst
), 0,
609 swizzle(src0
, X
, X
, X
, X
), 0, 0);
613 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
614 tmp
= i915_get_utemp(p
);
616 /* tmp = max( a.xyzw, a.00zw )
617 * XXX: Clamp tmp.w to -128..128
619 * tmp.y = tmp.w * tmp.y
621 * result = cmp (a.11-x1, a.1x01, a.1xy1 )
623 i915_emit_arith(p
, A0_MAX
, tmp
, A0_DEST_CHANNEL_ALL
, 0,
624 src0
, swizzle(src0
, ZERO
, ZERO
, Z
, W
), 0);
626 i915_emit_arith(p
, A0_LOG
, tmp
, A0_DEST_CHANNEL_Y
, 0,
627 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
629 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_Y
, 0,
630 swizzle(tmp
, ZERO
, Y
, ZERO
, ZERO
),
631 swizzle(tmp
, ZERO
, W
, ZERO
, ZERO
), 0);
633 i915_emit_arith(p
, A0_EXP
, tmp
, A0_DEST_CHANNEL_Y
, 0,
634 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
636 i915_emit_arith(p
, A0_CMP
,
637 get_result_vector(p
, inst
),
638 get_result_flags(inst
), 0,
639 negate(swizzle(tmp
, ONE
, ONE
, X
, ONE
), 0, 0, 1, 0),
640 swizzle(tmp
, ONE
, X
, ZERO
, ONE
),
641 swizzle(tmp
, ONE
, X
, Y
, ONE
));
646 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
647 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
648 src2
= src_vector(p
, &inst
->SrcReg
[2], program
);
649 flags
= get_result_flags(inst
);
650 tmp
= i915_get_utemp(p
);
657 * result = (-c)*a + tmp
659 i915_emit_arith(p
, A0_MAD
, tmp
,
660 flags
& A0_DEST_CHANNEL_ALL
, 0, src1
, src0
, src2
);
662 i915_emit_arith(p
, A0_MAD
,
663 get_result_vector(p
, inst
),
664 flags
, 0, negate(src2
, 1, 1, 1, 1), src0
, tmp
);
668 EMIT_3ARG_ARITH(A0_MAD
);
672 EMIT_2ARG_ARITH(A0_MAX
);
676 EMIT_2ARG_ARITH(A0_MIN
);
680 EMIT_1ARG_ARITH(A0_MOV
);
684 EMIT_2ARG_ARITH(A0_MUL
);
688 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
689 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
690 tmp
= i915_get_utemp(p
);
691 flags
= get_result_flags(inst
);
693 /* XXX: masking on intermediate values, here and elsewhere.
697 tmp
, A0_DEST_CHANNEL_X
, 0,
698 swizzle(src0
, X
, X
, X
, X
), 0, 0);
700 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, src1
, 0);
705 get_result_vector(p
, inst
),
706 flags
, 0, swizzle(tmp
, X
, X
, X
, X
), 0, 0);
711 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
715 get_result_vector(p
, inst
),
716 get_result_flags(inst
), 0,
717 swizzle(src0
, X
, X
, X
, X
), 0, 0);
722 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
726 get_result_vector(p
, inst
),
727 get_result_flags(inst
), 0,
728 swizzle(src0
, X
, X
, X
, X
), 0, 0);
732 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
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 if (inst
->DstReg
.WriteMask
& WRITEMASK_Y
) {
758 if (inst
->DstReg
.WriteMask
& WRITEMASK_X
)
759 tmp1
= i915_get_utemp(p
);
765 tmp1
, A0_DEST_CHANNEL_ALL
, 0,
766 swizzle(tmp
, X
, Y
, Y
, W
),
767 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
771 get_result_vector(p
, inst
),
772 A0_DEST_CHANNEL_Y
, 0,
773 swizzle(tmp1
, W
, Z
, Y
, X
),
774 i915_emit_const4fv(p
, sin_constants
), 0);
777 if (inst
->DstReg
.WriteMask
& WRITEMASK_X
) {
780 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
781 swizzle(tmp
, X
, X
, Z
, ONE
),
782 swizzle(tmp
, Z
, ONE
, ONE
, ONE
), 0);
786 get_result_vector(p
, inst
),
787 A0_DEST_CHANNEL_X
, 0,
788 swizzle(tmp
, ONE
, Z
, Y
, X
),
789 i915_emit_const4fv(p
, cos_constants
), 0);
794 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
795 tmp
= i915_get_utemp(p
);
796 consts0
= i915_emit_const4fv(p
, sin_quad_constants
[0]);
797 consts1
= i915_emit_const4fv(p
, sin_quad_constants
[1]);
799 /* Reduce range from repeating about [-pi,pi] to [-1,1] */
802 tmp
, A0_DEST_CHANNEL_X
, 0,
804 swizzle(consts1
, Z
, ZERO
, ZERO
, ZERO
), /* 1/(2pi) */
805 swizzle(consts0
, Z
, ZERO
, ZERO
, ZERO
)); /* .5 */
807 i915_emit_arith(p
, A0_FRC
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
811 tmp
, A0_DEST_CHANNEL_X
, 0,
813 swizzle(consts0
, X
, ZERO
, ZERO
, ZERO
), /* 2 */
814 swizzle(consts0
, Y
, ZERO
, ZERO
, ZERO
)); /* -1 */
816 /* Compute sin using a quadratic and quartic. It gives continuity
817 * that repeating the Taylor series lacks every 2*pi, and has
820 * The idea was described at:
821 * http://www.devmaster.net/forums/showthread.php?t=5784
824 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */
827 tmp
, A0_DEST_CHANNEL_Y
, 0,
828 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
829 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0),
832 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */
835 tmp
, A0_DEST_CHANNEL_Y
, 0,
836 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
840 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */
843 tmp
, A0_DEST_CHANNEL_X
, 0,
845 swizzle(consts1
, X
, Y
, ZERO
, ZERO
),
848 /* tmp.x now contains a first approximation (y). Now, weight it
849 * against tmp.y**2 to get closer.
853 tmp
, A0_DEST_CHANNEL_Y
, 0,
854 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
855 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0),
858 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */
861 tmp
, A0_DEST_CHANNEL_Y
, 0,
862 swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
),
863 swizzle(tmp
, ZERO
, Y
, ZERO
, ZERO
),
864 negate(swizzle(tmp
, ZERO
, X
, ZERO
, ZERO
), 0, 1, 0, 0));
866 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */
869 get_result_vector(p
, inst
),
870 get_result_flags(inst
), 0,
871 swizzle(consts1
, W
, W
, W
, W
),
872 swizzle(tmp
, Y
, Y
, Y
, Y
),
873 swizzle(tmp
, X
, X
, X
, X
));
878 EMIT_2ARG_ARITH(A0_SGE
);
882 EMIT_2ARG_ARITH(A0_SLT
);
886 dst
= get_result_vector(p
, inst
);
887 flags
= get_result_flags(inst
);
888 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
889 tmp
= i915_get_utemp(p
);
891 /* tmp = (src < 0.0) */
897 swizzle(src0
, ZERO
, ZERO
, ZERO
, ZERO
),
900 /* dst = (0.0 < src) */
905 swizzle(src0
, ZERO
, ZERO
, ZERO
, ZERO
),
909 /* dst = (src > 0.0) - (src < 0.0) */
915 negate(tmp
, 1, 1, 1, 1),
921 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
922 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
926 get_result_vector(p
, inst
),
927 get_result_flags(inst
), 0,
928 src0
, negate(src1
, 1, 1, 1, 1), 0);
932 EMIT_1ARG_ARITH(A0_MOV
); /* extended swizzle handled natively */
949 * result.x = src0.y * src1.z - src0.z * src1.y;
950 * result.y = src0.z * src1.x - src0.x * src1.z;
951 * result.z = src0.x * src1.y - src0.y * src1.x;
954 src0
= src_vector(p
, &inst
->SrcReg
[0], program
);
955 src1
= src_vector(p
, &inst
->SrcReg
[1], program
);
956 tmp
= i915_get_utemp(p
);
960 tmp
, A0_DEST_CHANNEL_ALL
, 0,
961 swizzle(src0
, Z
, X
, Y
, ONE
),
962 swizzle(src1
, Y
, Z
, X
, ONE
), 0);
966 get_result_vector(p
, inst
),
967 get_result_flags(inst
), 0,
968 swizzle(src0
, Y
, Z
, X
, ONE
),
969 swizzle(src1
, Z
, X
, Y
, ONE
),
970 negate(tmp
, 1, 1, 1, 0));
990 i915_program_error(p
, "Unsupported opcode: %s",
991 _mesa_opcode_string(inst
->Opcode
));
996 /* These opcodes are claimed as GLSL, NV_vp, and ARB_vp in
997 * prog_instruction.h, but apparently GLSL doesn't ever emit them.
998 * Instead, it translates to EX2 or LG2.
1002 /* These opcodes are claimed by GLSL in prog_instruction.h, but
1003 * only NV_vp/fp appears to emit them.
1006 i915_program_error(p
, "bad opcode: %s",
1007 _mesa_opcode_string(inst
->Opcode
));
1012 i915_release_utemps(p
);
1016 /* Rather than trying to intercept and jiggle depth writes during
1017 * emit, just move the value into its correct position at the end of
1021 fixup_depth_write(struct i915_fragment_program
*p
)
1023 if (p
->depth_written
) {
1024 GLuint depth
= UREG(REG_TYPE_OD
, 0);
1028 depth
, A0_DEST_CHANNEL_W
, 0,
1029 swizzle(depth
, X
, Y
, Z
, Z
), 0, 0);
1034 check_texcoord_mapping(struct i915_fragment_program
*p
)
1036 GLbitfield64 inputs
= p
->FragProg
.info
.inputs_read
;
1039 for (unsigned i
= 0; i
< p
->ctx
->Const
.MaxTextureCoordUnits
; i
++) {
1040 if (inputs
& VARYING_BIT_TEX(i
)) {
1041 if (unit
>= p
->ctx
->Const
.MaxTextureCoordUnits
) {
1045 p
->texcoord_mapping
[unit
++] = i
| TEXCOORD_TEX
;
1047 if (inputs
& VARYING_BIT_VAR(i
)) {
1048 if (unit
>= p
->ctx
->Const
.MaxTextureCoordUnits
) {
1052 p
->texcoord_mapping
[unit
++] = i
| TEXCOORD_VAR
;
1056 if (unit
> p
->ctx
->Const
.MaxTextureCoordUnits
)
1057 i915_program_error(p
, "Too many texcoord units");
1061 check_wpos(struct i915_fragment_program
*p
)
1063 GLbitfield64 inputs
= p
->FragProg
.info
.inputs_read
;
1067 p
->wpos_tex
= I915_WPOS_TEX_INVALID
;
1069 if ((inputs
& VARYING_BIT_POS
) == 0)
1072 for (i
= 0; i
< p
->ctx
->Const
.MaxTextureCoordUnits
; i
++) {
1073 unit
+= !!(inputs
& VARYING_BIT_TEX(i
));
1074 unit
+= !!(inputs
& VARYING_BIT_VAR(i
));
1077 if (unit
< p
->ctx
->Const
.MaxTextureCoordUnits
)
1080 i915_program_error(p
, "No free texcoord for wpos value");
1085 translate_program(struct i915_fragment_program
*p
)
1087 struct i915_context
*i915
= I915_CONTEXT(p
->ctx
);
1089 if (INTEL_DEBUG
& DEBUG_WM
) {
1091 _mesa_print_program(&p
->FragProg
);
1095 i915_init_program(i915
, p
);
1096 check_texcoord_mapping(p
);
1099 fixup_depth_write(p
);
1100 i915_fini_program(p
);
1107 track_params(struct i915_fragment_program
*p
)
1112 _mesa_load_state_parameters(p
->ctx
, p
->FragProg
.Parameters
);
1114 for (i
= 0; i
< p
->nr_params
; i
++) {
1115 GLint reg
= p
->param
[i
].reg
;
1116 COPY_4V(p
->constant
[reg
], p
->param
[i
].values
);
1119 p
->params_uptodate
= 1;
1120 p
->on_hardware
= 0; /* overkill */
1123 static struct gl_program
*
1124 i915NewProgram(struct gl_context
* ctx
, gl_shader_stage stage
, GLuint id
,
1128 case MESA_SHADER_VERTEX
: {
1129 struct gl_program
*prog
= rzalloc(NULL
, struct gl_program
);
1130 return _mesa_init_gl_program(prog
, stage
, id
, is_arb_asm
);
1133 case MESA_SHADER_FRAGMENT
:{
1134 struct i915_fragment_program
*prog
=
1135 rzalloc(NULL
, struct i915_fragment_program
);
1137 i915_init_program(I915_CONTEXT(ctx
), prog
);
1139 return _mesa_init_gl_program(&prog
->FragProg
, stage
, id
,
1149 return _mesa_new_program(ctx
, stage
, id
, is_arb_asm
);
1154 i915DeleteProgram(struct gl_context
* ctx
, struct gl_program
*prog
)
1156 if (prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1157 struct i915_context
*i915
= I915_CONTEXT(ctx
);
1158 struct i915_fragment_program
*p
= (struct i915_fragment_program
*) prog
;
1160 if (i915
->current_program
== p
)
1161 i915
->current_program
= 0;
1164 _mesa_delete_program(ctx
, prog
);
1169 i915IsProgramNative(struct gl_context
* ctx
, GLenum target
, struct gl_program
*prog
)
1171 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
1172 struct i915_fragment_program
*p
= (struct i915_fragment_program
*) prog
;
1175 translate_program(p
);
1184 i915ProgramStringNotify(struct gl_context
* ctx
,
1185 GLenum target
, struct gl_program
*prog
)
1187 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
1188 struct i915_fragment_program
*p
= (struct i915_fragment_program
*) prog
;
1192 (void) _tnl_program_string(ctx
, target
, prog
);
1194 /* XXX check if program is legal, within limits */
1199 i915SamplerUniformChange(struct gl_context
*ctx
,
1200 GLenum target
, struct gl_program
*prog
)
1202 i915ProgramStringNotify(ctx
, target
, prog
);
1206 i915_update_program(struct gl_context
*ctx
)
1208 struct intel_context
*intel
= intel_context(ctx
);
1209 struct i915_context
*i915
= i915_context(&intel
->ctx
);
1210 struct i915_fragment_program
*fp
=
1211 (struct i915_fragment_program
*) ctx
->FragmentProgram
._Current
;
1213 if (i915
->current_program
!= fp
) {
1214 if (i915
->current_program
) {
1215 i915
->current_program
->on_hardware
= 0;
1216 i915
->current_program
->params_uptodate
= 0;
1219 i915
->current_program
= fp
;
1222 if (!fp
->translated
)
1223 translate_program(fp
);
1225 FALLBACK(&i915
->intel
, I915_FALLBACK_PROGRAM
, fp
->error
);
1229 i915ValidateFragmentProgram(struct i915_context
*i915
)
1231 struct gl_context
*ctx
= &i915
->intel
.ctx
;
1232 struct intel_context
*intel
= intel_context(ctx
);
1233 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1234 struct vertex_buffer
*VB
= &tnl
->vb
;
1236 struct i915_fragment_program
*p
=
1237 (struct i915_fragment_program
*) ctx
->FragmentProgram
._Current
;
1239 const GLbitfield64 inputsRead
= p
->FragProg
.info
.inputs_read
;
1240 GLuint s4
= i915
->state
.Ctx
[I915_CTXREG_LIS4
] & ~S4_VFMT_MASK
;
1241 GLuint s2
= S2_TEXCOORD_NONE
;
1247 VB
->AttribPtr
[VERT_ATTRIB_POS
] = VB
->NdcPtr
;
1250 translate_program(p
);
1252 intel
->vertex_attr_count
= 0;
1253 intel
->wpos_offset
= 0;
1254 intel
->coloroffset
= 0;
1255 intel
->specoffset
= 0;
1257 /* Always emit W to get consistent perspective
1258 * correct interpolation of primary/secondary colors.
1260 EMIT_ATTR(_TNL_ATTRIB_POS
, EMIT_4F_VIEWPORT
, S4_VFMT_XYZW
, 16);
1262 /* Handle gl_PointSize builtin var here */
1263 if (ctx
->Point
._Attenuated
|| ctx
->VertexProgram
.PointSizeEnabled
)
1264 EMIT_ATTR(_TNL_ATTRIB_POINTSIZE
, EMIT_1F
, S4_VFMT_POINT_WIDTH
, 4);
1266 if (inputsRead
& VARYING_BIT_COL0
) {
1267 intel
->coloroffset
= offset
/ 4;
1268 EMIT_ATTR(_TNL_ATTRIB_COLOR0
, EMIT_4UB_4F_BGRA
, S4_VFMT_COLOR
, 4);
1271 if (inputsRead
& VARYING_BIT_COL1
) {
1272 intel
->specoffset
= offset
/ 4;
1273 EMIT_ATTR(_TNL_ATTRIB_COLOR1
, EMIT_4UB_4F_BGRA
, S4_VFMT_SPEC_FOG
, 4);
1276 if ((inputsRead
& VARYING_BIT_FOGC
)) {
1277 EMIT_ATTR(_TNL_ATTRIB_FOG
, EMIT_1F
, S4_VFMT_FOG_PARAM
, 4);
1280 for (i
= 0; i
< p
->ctx
->Const
.MaxTextureCoordUnits
; i
++) {
1281 if (inputsRead
& VARYING_BIT_TEX(i
)) {
1282 int unit
= get_texcoord_mapping(p
, i
| TEXCOORD_TEX
);
1283 int sz
= VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
]->size
;
1285 s2
&= ~S2_TEXCOORD_FMT(unit
, S2_TEXCOORD_FMT0_MASK
);
1286 s2
|= S2_TEXCOORD_FMT(unit
, SZ_TO_HW(sz
));
1288 EMIT_ATTR(_TNL_ATTRIB_TEX0
+ i
, EMIT_SZ(sz
), 0, sz
* 4);
1290 if (inputsRead
& VARYING_BIT_VAR(i
)) {
1291 int unit
= get_texcoord_mapping(p
, i
| TEXCOORD_VAR
);
1292 int sz
= VB
->AttribPtr
[_TNL_ATTRIB_GENERIC0
+ i
]->size
;
1294 s2
&= ~S2_TEXCOORD_FMT(unit
, S2_TEXCOORD_FMT0_MASK
);
1295 s2
|= S2_TEXCOORD_FMT(unit
, SZ_TO_HW(sz
));
1297 EMIT_ATTR(_TNL_ATTRIB_GENERIC0
+ i
, EMIT_SZ(sz
), 0, sz
* 4);
1299 if (i
== p
->wpos_tex
) {
1300 int wpos_size
= 4 * sizeof(float);
1301 /* If WPOS is required, duplicate the XYZ position data in an
1302 * unused texture coordinate:
1304 s2
&= ~S2_TEXCOORD_FMT(i
, S2_TEXCOORD_FMT0_MASK
);
1305 s2
|= S2_TEXCOORD_FMT(i
, SZ_TO_HW(wpos_size
));
1306 s3
|= S3_TEXCOORD_PERSPECTIVE_DISABLE(i
);
1308 intel
->wpos_offset
= offset
;
1309 EMIT_PAD(wpos_size
);
1313 if (s2
!= i915
->state
.Ctx
[I915_CTXREG_LIS2
] ||
1314 s3
!= i915
->state
.Ctx
[I915_CTXREG_LIS3
] ||
1315 s4
!= i915
->state
.Ctx
[I915_CTXREG_LIS4
]) {
1316 I915_STATECHANGE(i915
, I915_UPLOAD_CTX
);
1318 /* Must do this *after* statechange, so as not to affect
1319 * buffered vertices reliant on the old state:
1321 intel
->vertex_size
= _tnl_install_attrs(&intel
->ctx
,
1322 intel
->vertex_attrs
,
1323 intel
->vertex_attr_count
,
1324 intel
->ViewportMatrix
.m
, 0);
1326 assert(intel
->prim
.current_offset
== intel
->prim
.start_offset
);
1327 intel
->prim
.start_offset
= (intel
->prim
.current_offset
+ intel
->vertex_size
-1) / intel
->vertex_size
* intel
->vertex_size
;
1328 intel
->prim
.current_offset
= intel
->prim
.start_offset
;
1330 intel
->vertex_size
>>= 2;
1332 i915
->state
.Ctx
[I915_CTXREG_LIS2
] = s2
;
1333 i915
->state
.Ctx
[I915_CTXREG_LIS3
] = s3
;
1334 i915
->state
.Ctx
[I915_CTXREG_LIS4
] = s4
;
1336 assert(intel
->vtbl
.check_vertex_size(intel
, intel
->vertex_size
));
1339 if (!p
->params_uptodate
)
1342 if (!p
->on_hardware
)
1343 i915_upload_program(i915
, p
);
1345 if (INTEL_DEBUG
& DEBUG_WM
) {
1347 i915_disassemble_program(i915
->state
.Program
, i915
->state
.ProgramSize
);
1352 i915InitFragProgFuncs(struct dd_function_table
*functions
)
1354 functions
->NewProgram
= i915NewProgram
;
1355 functions
->DeleteProgram
= i915DeleteProgram
;
1356 functions
->IsProgramNative
= i915IsProgramNative
;
1357 functions
->ProgramStringNotify
= i915ProgramStringNotify
;
1358 functions
->SamplerUniformChange
= i915SamplerUniformChange
;