1 /**************************************************************************
3 * Copyright 2003 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 "tnl/t_context.h"
33 #include "intel_batchbuffer.h"
36 #include "i915_context.h"
37 #include "i915_program.h"
39 #include "nvfragprog.h"
41 #include "arbfragparse.h"
49 /* 1, -1/3!, 1/5!, -1/7! */
50 static const GLfloat sin_constants
[4] = { 1.0,
53 -1.0/(7*6*5*4*3*2*1) };
55 /* 1, -1/2!, 1/4!, -1/6! */
56 static const GLfloat cos_constants
[4] = { 1.0,
62 * Retrieve a ureg for the given source register. Will emit
63 * constants, apply swizzling and negation as needed.
65 static GLuint
src_vector( struct i915_fragment_program
*p
,
66 const struct fp_src_register
*source
,
67 const struct fragment_program
*program
)
71 switch (source
->File
) {
75 case PROGRAM_TEMPORARY
:
76 if (source
->Index
>= I915_MAX_TEMPORARY
) {
77 i915_program_error( p
, "Exceeded max temporary reg" );
80 src
= UREG( REG_TYPE_R
, source
->Index
);
83 switch (source
->Index
) {
84 case FRAG_ATTRIB_WPOS
:
85 src
= i915_emit_decl( p
, REG_TYPE_T
, p
->wpos_tex
, D0_CHANNEL_ALL
);
87 case FRAG_ATTRIB_COL0
:
88 src
= i915_emit_decl( p
, REG_TYPE_T
, T_DIFFUSE
, D0_CHANNEL_ALL
);
90 case FRAG_ATTRIB_COL1
:
91 src
= i915_emit_decl( p
, REG_TYPE_T
, T_SPECULAR
, D0_CHANNEL_XYZ
);
92 src
= swizzle( src
, X
, Y
, Z
, ONE
);
94 case FRAG_ATTRIB_FOGC
:
95 src
= i915_emit_decl( p
, REG_TYPE_T
, T_FOG_W
, D0_CHANNEL_W
);
96 src
= swizzle( src
, W
, W
, W
, W
);
98 case FRAG_ATTRIB_TEX0
:
99 case FRAG_ATTRIB_TEX1
:
100 case FRAG_ATTRIB_TEX2
:
101 case FRAG_ATTRIB_TEX3
:
102 case FRAG_ATTRIB_TEX4
:
103 case FRAG_ATTRIB_TEX5
:
104 case FRAG_ATTRIB_TEX6
:
105 case FRAG_ATTRIB_TEX7
:
106 src
= i915_emit_decl( p
, REG_TYPE_T
,
107 T_TEX0
+ (source
->Index
- FRAG_ATTRIB_TEX0
),
112 i915_program_error( p
, "Bad source->Index" );
117 /* Various paramters and env values. All emitted to
118 * hardware as program constants.
120 case PROGRAM_LOCAL_PARAM
:
121 src
= i915_emit_param4fv(
122 p
, program
->Base
.LocalParams
[source
->Index
]);
125 case PROGRAM_ENV_PARAM
:
126 src
= i915_emit_param4fv(
127 p
, p
->ctx
->FragmentProgram
.Parameters
[source
->Index
]);
130 case PROGRAM_STATE_VAR
:
131 case PROGRAM_NAMED_PARAM
:
132 src
= i915_emit_param4fv(
133 p
, program
->Parameters
->ParameterValues
[source
->Index
] );
137 i915_program_error( p
, "Bad source->File" );
142 GET_SWZ(source
->Swizzle
, 0),
143 GET_SWZ(source
->Swizzle
, 1),
144 GET_SWZ(source
->Swizzle
, 2),
145 GET_SWZ(source
->Swizzle
, 3));
147 if (source
->NegateBase
)
149 GET_BIT(source
->NegateBase
, 0),
150 GET_BIT(source
->NegateBase
, 1),
151 GET_BIT(source
->NegateBase
, 2),
152 GET_BIT(source
->NegateBase
, 3));
158 static GLuint
get_result_vector( struct i915_fragment_program
*p
,
159 const struct fp_instruction
*inst
)
161 switch (inst
->DstReg
.File
) {
163 switch (inst
->DstReg
.Index
) {
165 return UREG(REG_TYPE_OC
, 0);
167 p
->depth_written
= 1;
168 return UREG(REG_TYPE_OD
, 0);
170 i915_program_error( p
, "Bad inst->DstReg.Index" );
173 case PROGRAM_TEMPORARY
:
174 return UREG(REG_TYPE_R
, inst
->DstReg
.Index
);
176 i915_program_error( p
, "Bad inst->DstReg.File" );
181 static GLuint
get_result_flags( const struct fp_instruction
*inst
)
185 if (inst
->Saturate
) flags
|= A0_DEST_SATURATE
;
186 if (inst
->DstReg
.WriteMask
& WRITEMASK_X
) flags
|= A0_DEST_CHANNEL_X
;
187 if (inst
->DstReg
.WriteMask
& WRITEMASK_Y
) flags
|= A0_DEST_CHANNEL_Y
;
188 if (inst
->DstReg
.WriteMask
& WRITEMASK_Z
) flags
|= A0_DEST_CHANNEL_Z
;
189 if (inst
->DstReg
.WriteMask
& WRITEMASK_W
) flags
|= A0_DEST_CHANNEL_W
;
194 static GLuint
translate_tex_src_idx( struct i915_fragment_program
*p
,
198 case TEXTURE_1D_INDEX
: return D0_SAMPLE_TYPE_2D
;
199 case TEXTURE_2D_INDEX
: return D0_SAMPLE_TYPE_2D
;
200 case TEXTURE_RECT_INDEX
: return D0_SAMPLE_TYPE_2D
;
201 case TEXTURE_3D_INDEX
: return D0_SAMPLE_TYPE_VOLUME
;
202 case TEXTURE_CUBE_INDEX
: return D0_SAMPLE_TYPE_CUBE
;
203 default: i915_program_error(p
, "TexSrcBit"); return 0;
207 #define EMIT_TEX( OP ) \
209 GLuint dim = translate_tex_src_idx( p, inst->TexSrcIdx ); \
210 GLuint sampler = i915_emit_decl(p, REG_TYPE_S, \
211 inst->TexSrcUnit, dim); \
212 GLuint coord = src_vector( p, &inst->SrcReg[0], program); \
215 i915_emit_texld( p, \
216 get_result_vector( p, inst ), \
217 get_result_flags( inst ), \
223 #define EMIT_ARITH( OP, N ) \
225 i915_emit_arith( p, \
227 get_result_vector( p, inst ), \
228 get_result_flags( inst ), 0, \
229 (N<1)?0:src_vector( p, &inst->SrcReg[0], program), \
230 (N<2)?0:src_vector( p, &inst->SrcReg[1], program), \
231 (N<3)?0:src_vector( p, &inst->SrcReg[2], program)); \
234 #define EMIT_1ARG_ARITH( OP ) EMIT_ARITH( OP, 1 )
235 #define EMIT_2ARG_ARITH( OP ) EMIT_ARITH( OP, 2 )
236 #define EMIT_3ARG_ARITH( OP ) EMIT_ARITH( OP, 3 )
239 /* Possible concerns:
241 * SIN, COS -- could use another taylor step?
242 * LIT -- results seem a little different to sw mesa
243 * LOG -- different to mesa on negative numbers, but this is conformant.
245 * Parse failures -- Mesa doesn't currently give a good indication
246 * internally whether a particular program string parsed or not. This
247 * can lead to confusion -- hopefully we cope with it ok now.
250 static void upload_program( struct i915_fragment_program
*p
)
252 const struct fragment_program
*program
= p
->ctx
->FragmentProgram
._Current
;
253 const struct fp_instruction
*inst
= program
->Instructions
;
255 /* _mesa_debug_fp_inst(program->Base.NumInstructions, inst); */
257 /* Is this a parse-failed program? Ensure a valid program is
258 * loaded, as the flagging of an error isn't sufficient to stop
259 * this being uploaded to hardware.
261 if (inst
[0].Opcode
== FP_OPCODE_END
) {
262 GLuint tmp
= i915_get_utemp( p
);
265 UREG(REG_TYPE_OC
, 0),
266 A0_DEST_CHANNEL_ALL
, 0,
267 swizzle(tmp
,ONE
,ZERO
,ONE
,ONE
), 0, 0);
272 GLuint src0
, src1
, src2
, flags
;
275 switch (inst
->Opcode
) {
277 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
280 get_result_vector( p
, inst
),
281 get_result_flags( inst
), 0,
282 src0
, negate(src0
, 1,1,1,1), 0);
286 EMIT_2ARG_ARITH( A0_ADD
);
290 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
291 src1
= src_vector( p
, &inst
->SrcReg
[1], program
);
292 src2
= src_vector( p
, &inst
->SrcReg
[2], program
);
295 get_result_vector( p
, inst
),
296 get_result_flags( inst
), 0,
297 src0
, src2
, src1
); /* NOTE: order of src2, src1 */
301 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
302 tmp
= i915_get_utemp( p
);
306 tmp
, A0_DEST_CHANNEL_X
, 0,
308 i915_emit_const1f(p
, 1.0/(PI
* 2)),
313 tmp
, A0_DEST_CHANNEL_X
, 0,
317 /* By choosing different taylor constants, could get rid of this mul:
321 tmp
, A0_DEST_CHANNEL_X
, 0,
323 i915_emit_const1f(p
, (PI
* 2)),
327 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
328 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, 1
329 * t0 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
330 * result = DP4 t0, cos_constants
334 tmp
, A0_DEST_CHANNEL_XY
, 0,
335 swizzle(tmp
, X
,X
,ONE
,ONE
),
336 swizzle(tmp
, X
,ONE
,ONE
,ONE
), 0);
340 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
341 swizzle(tmp
, X
,Y
,X
,ONE
),
342 swizzle(tmp
, X
,X
,ONE
,ONE
), 0);
346 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
347 swizzle(tmp
, X
,X
,Z
,ONE
),
348 swizzle(tmp
, Z
,ONE
,ONE
,ONE
), 0);
352 get_result_vector( p
, inst
),
353 get_result_flags( inst
), 0,
354 swizzle(tmp
, ONE
,Z
,Y
,X
),
355 i915_emit_const4fv( p
, cos_constants
), 0);
360 EMIT_2ARG_ARITH( A0_DP3
);
364 EMIT_2ARG_ARITH( A0_DP4
);
368 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
369 src1
= src_vector( p
, &inst
->SrcReg
[1], program
);
373 get_result_vector( p
, inst
),
374 get_result_flags( inst
), 0,
375 swizzle(src0
, X
,Y
,Z
,ONE
), src1
, 0);
379 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
380 src1
= src_vector( p
, &inst
->SrcReg
[1], program
);
382 /* result[0] = 1 * 1;
383 * result[1] = a[1] * b[1];
384 * result[2] = a[2] * 1;
385 * result[3] = 1 * b[3];
389 get_result_vector( p
, inst
),
390 get_result_flags( inst
), 0,
391 swizzle(src0
, ONE
, Y
, Z
, ONE
),
392 swizzle(src1
, ONE
, Y
, ONE
, W
),
397 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
401 get_result_vector( p
, inst
),
402 get_result_flags( inst
), 0,
403 swizzle(src0
,X
,X
,X
,X
), 0, 0);
407 EMIT_1ARG_ARITH( A0_FLR
);
411 EMIT_1ARG_ARITH( A0_FRC
);
415 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
416 tmp
= i915_get_utemp( p
);
419 tmp
, A0_DEST_CHANNEL_ALL
, /* use a dummy dest reg */
426 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
430 get_result_vector( p
, inst
),
431 get_result_flags( inst
), 0,
432 swizzle(src0
,X
,X
,X
,X
), 0, 0);
436 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
437 tmp
= i915_get_utemp( p
);
439 /* tmp = max( a.xyzw, a.00zw )
440 * XXX: Clamp tmp.w to -128..128
442 * tmp.y = tmp.w * tmp.y
444 * result = cmp (a.11-x1, a.1x01, a.1xy1 )
446 i915_emit_arith( p
, A0_MAX
, tmp
, A0_DEST_CHANNEL_ALL
, 0,
447 src0
, swizzle(src0
, ZERO
, ZERO
, Z
, W
), 0 );
449 i915_emit_arith( p
, A0_LOG
, tmp
, A0_DEST_CHANNEL_Y
, 0,
450 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0 );
452 i915_emit_arith( p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_Y
, 0,
453 swizzle(tmp
, ZERO
, Y
, ZERO
, ZERO
),
454 swizzle(tmp
, ZERO
, W
, ZERO
, ZERO
), 0 );
456 i915_emit_arith( p
, A0_EXP
, tmp
, A0_DEST_CHANNEL_Y
, 0,
457 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0 );
459 i915_emit_arith( p
, A0_CMP
,
460 get_result_vector( p
, inst
),
461 get_result_flags( inst
), 0,
462 negate(swizzle(tmp
, ONE
, ONE
, X
, ONE
),0,0,1,0),
463 swizzle(tmp
, ONE
, X
, ZERO
, ONE
),
464 swizzle(tmp
, ONE
, X
, Y
, ONE
));
469 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
470 src1
= src_vector( p
, &inst
->SrcReg
[1], program
);
471 src2
= src_vector( p
, &inst
->SrcReg
[2], program
);
472 flags
= get_result_flags( inst
);
473 tmp
= i915_get_utemp( p
);
480 * result = (-c)*a + tmp
482 i915_emit_arith( p
, A0_MAD
, tmp
,
483 flags
& A0_DEST_CHANNEL_ALL
, 0,
486 i915_emit_arith( p
, A0_MAD
,
487 get_result_vector( p
, inst
),
489 negate(src2
, 1,1,1,1), src0
, tmp
);
493 EMIT_3ARG_ARITH( A0_MAD
);
497 EMIT_2ARG_ARITH( A0_MAX
);
501 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
502 src1
= src_vector( p
, &inst
->SrcReg
[1], program
);
503 tmp
= i915_get_utemp( p
);
504 flags
= get_result_flags( inst
);
508 tmp
, flags
& A0_DEST_CHANNEL_ALL
, 0,
509 negate(src0
,1,1,1,1),
510 negate(src1
,1,1,1,1), 0);
514 get_result_vector( p
, inst
),
516 negate(tmp
, 1,1,1,1), 0, 0);
520 EMIT_1ARG_ARITH( A0_MOV
);
524 EMIT_2ARG_ARITH( A0_MUL
);
528 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
529 src1
= src_vector( p
, &inst
->SrcReg
[1], program
);
530 tmp
= i915_get_utemp( p
);
531 flags
= get_result_flags( inst
);
533 /* XXX: masking on intermediate values, here and elsewhere.
537 tmp
, A0_DEST_CHANNEL_X
, 0,
538 swizzle(src0
,X
,X
,X
,X
), 0, 0);
542 tmp
, A0_DEST_CHANNEL_X
, 0,
548 get_result_vector( p
, inst
),
550 swizzle(tmp
,X
,X
,X
,X
), 0, 0);
555 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
559 get_result_vector( p
, inst
),
560 get_result_flags( inst
), 0,
561 swizzle(src0
,X
,X
,X
,X
), 0, 0);
566 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
570 get_result_vector( p
, inst
),
571 get_result_flags( inst
), 0,
572 swizzle(src0
,X
,X
,X
,X
), 0, 0);
576 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
577 tmp
= i915_get_utemp( p
);
580 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
581 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
582 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
583 * scs.x = DP4 t1, sin_constants
584 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
585 * scs.y = DP4 t1, cos_constants
589 tmp
, A0_DEST_CHANNEL_XY
, 0,
590 swizzle(src0
, X
,X
,ONE
,ONE
),
591 swizzle(src0
, X
,ONE
,ONE
,ONE
), 0);
595 tmp
, A0_DEST_CHANNEL_ALL
, 0,
596 swizzle(tmp
, X
,Y
,X
,Y
),
597 swizzle(tmp
, X
,X
,ONE
,ONE
), 0);
599 if (inst
->DstReg
.WriteMask
& WRITEMASK_Y
) {
602 if (inst
->DstReg
.WriteMask
& WRITEMASK_X
)
603 tmp1
= i915_get_utemp( p
);
609 tmp1
, A0_DEST_CHANNEL_ALL
, 0,
610 swizzle(tmp
, X
,Y
,Y
,W
),
611 swizzle(tmp
, X
,Z
,ONE
,ONE
), 0);
615 get_result_vector( p
, inst
),
616 A0_DEST_CHANNEL_Y
, 0,
617 swizzle(tmp1
, W
,Z
,Y
,X
),
618 i915_emit_const4fv( p
, sin_constants
), 0);
621 if (inst
->DstReg
.WriteMask
& WRITEMASK_X
) {
624 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
625 swizzle(tmp
, X
,X
,Z
,ONE
),
626 swizzle(tmp
, Z
,ONE
,ONE
,ONE
), 0);
630 get_result_vector( p
, inst
),
631 A0_DEST_CHANNEL_X
, 0,
632 swizzle(tmp
, ONE
,Z
,Y
,X
),
633 i915_emit_const4fv( p
, cos_constants
), 0);
638 EMIT_2ARG_ARITH( A0_SGE
);
642 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
643 tmp
= i915_get_utemp( p
);
647 tmp
, A0_DEST_CHANNEL_X
, 0,
649 i915_emit_const1f(p
, 1.0/(PI
* 2)),
654 tmp
, A0_DEST_CHANNEL_X
, 0,
658 /* By choosing different taylor constants, could get rid of this mul:
662 tmp
, A0_DEST_CHANNEL_X
, 0,
664 i915_emit_const1f(p
, (PI
* 2)),
668 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
669 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
670 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
671 * result = DP4 t1.wzyx, sin_constants
675 tmp
, A0_DEST_CHANNEL_XY
, 0,
676 swizzle(tmp
, X
,X
,ONE
,ONE
),
677 swizzle(tmp
, X
,ONE
,ONE
,ONE
), 0);
681 tmp
, A0_DEST_CHANNEL_ALL
, 0,
682 swizzle(tmp
, X
,Y
,X
,Y
),
683 swizzle(tmp
, X
,X
,ONE
,ONE
), 0);
687 tmp
, A0_DEST_CHANNEL_ALL
, 0,
688 swizzle(tmp
, X
,Y
,Y
,W
),
689 swizzle(tmp
, X
,Z
,ONE
,ONE
), 0);
693 get_result_vector( p
, inst
),
694 get_result_flags( inst
), 0,
695 swizzle(tmp
, W
, Z
, Y
, X
),
696 i915_emit_const4fv( p
, sin_constants
), 0);
700 EMIT_2ARG_ARITH( A0_SLT
);
704 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
705 src1
= src_vector( p
, &inst
->SrcReg
[1], program
);
709 get_result_vector( p
, inst
),
710 get_result_flags( inst
), 0,
711 src0
, negate(src1
, 1,1,1,1), 0);
715 EMIT_1ARG_ARITH( A0_MOV
); /* extended swizzle handled natively */
719 EMIT_TEX( T0_TEXLD
);
723 EMIT_TEX( T0_TEXLDB
);
727 EMIT_TEX( T0_TEXLDP
);
732 * result.x = src0.y * src1.z - src0.z * src1.y;
733 * result.y = src0.z * src1.x - src0.x * src1.z;
734 * result.z = src0.x * src1.y - src0.y * src1.x;
737 src0
= src_vector( p
, &inst
->SrcReg
[0], program
);
738 src1
= src_vector( p
, &inst
->SrcReg
[1], program
);
739 tmp
= i915_get_utemp( p
);
743 tmp
, A0_DEST_CHANNEL_ALL
, 0,
744 swizzle(src0
,Z
,X
,Y
,ONE
),
745 swizzle(src1
,Y
,Z
,X
,ONE
), 0);
749 get_result_vector( p
, inst
),
750 get_result_flags( inst
), 0,
751 swizzle(src0
,Y
,Z
,X
,ONE
),
752 swizzle(src1
,Z
,X
,Y
,ONE
),
753 negate(tmp
,1,1,1,0));
760 i915_program_error( p
, "bad opcode" );
765 i915_release_utemps( p
);
769 /* Rather than trying to intercept and jiggle depth writes during
770 * emit, just move the value into its correct position at the end of
773 static void fixup_depth_write( struct i915_fragment_program
*p
)
775 if (p
->depth_written
) {
776 GLuint depth
= UREG(REG_TYPE_OD
, 0);
780 depth
, A0_DEST_CHANNEL_W
, 0,
781 swizzle(depth
,X
,Y
,Z
,Z
),
787 #define FRAG_BIT_TEX(n) (FRAG_BIT_TEX0 << (n))
790 static void check_wpos( struct i915_fragment_program
*p
)
792 GLuint inputs
= p
->FragProg
.InputsRead
;
797 for (i
= 0; i
< p
->ctx
->Const
.MaxTextureCoordUnits
; i
++) {
798 if (inputs
& FRAG_BIT_TEX(i
))
800 else if (inputs
& FRAG_BIT_WPOS
) {
802 inputs
&= ~FRAG_BIT_WPOS
;
806 if (inputs
& FRAG_BIT_WPOS
) {
807 i915_program_error(p
, "No free texcoord for wpos value");
812 static void translate_program( struct i915_fragment_program
*p
)
814 i915ContextPtr i915
= I915_CONTEXT(p
->ctx
);
816 i915_init_program( i915
, p
);
819 fixup_depth_write( p
);
820 i915_fini_program( p
);
826 static void track_params( struct i915_fragment_program
*p
)
831 _mesa_load_state_parameters(p
->ctx
, p
->FragProg
.Parameters
);
833 for (i
= 0; i
< p
->nr_params
; i
++) {
834 GLint reg
= p
->param
[i
].reg
;
835 COPY_4V( p
->constant
[reg
], p
->param
[i
].values
);
838 p
->params_uptodate
= 1;
839 p
->on_hardware
= 0; /* overkill */
843 static void i915BindProgram( GLcontext
*ctx
,
845 struct program
*prog
)
847 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
848 i915ContextPtr i915
= I915_CONTEXT(ctx
);
849 struct i915_fragment_program
*p
= (struct i915_fragment_program
*)prog
;
851 if (i915
->current_program
== p
)
854 if (i915
->current_program
) {
855 i915
->current_program
->on_hardware
= 0;
856 i915
->current_program
->params_uptodate
= 0;
859 i915
->current_program
= p
;
861 assert(p
->on_hardware
== 0);
862 assert(p
->params_uptodate
== 0);
864 /* Hack: make sure fog is correctly enabled according to this
865 * fragment program's fog options.
867 ctx
->Driver
.Enable( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
868 ctx
->FragmentProgram
.Enabled
);
872 static struct program
*i915NewProgram( GLcontext
*ctx
,
877 case GL_VERTEX_PROGRAM_ARB
:
878 return _mesa_init_vertex_program( ctx
, CALLOC_STRUCT(vertex_program
),
881 case GL_FRAGMENT_PROGRAM_ARB
: {
882 struct i915_fragment_program
*prog
= CALLOC_STRUCT(i915_fragment_program
);
884 i915_init_program( I915_CONTEXT(ctx
), prog
);
886 return _mesa_init_fragment_program( ctx
, &prog
->FragProg
,
896 return _mesa_new_program( ctx
, target
, id
);
900 static void i915DeleteProgram( GLcontext
*ctx
,
901 struct program
*prog
)
903 if (prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
904 i915ContextPtr i915
= I915_CONTEXT(ctx
);
905 struct i915_fragment_program
*p
= (struct i915_fragment_program
*)prog
;
907 if (i915
->current_program
== p
)
908 i915
->current_program
= 0;
911 _mesa_delete_program( ctx
, prog
);
915 static GLboolean
i915IsProgramNative( GLcontext
*ctx
,
917 struct program
*prog
)
919 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
920 struct i915_fragment_program
*p
= (struct i915_fragment_program
*)prog
;
923 translate_program( p
);
931 static void i915ProgramStringNotify( GLcontext
*ctx
,
933 struct program
*prog
)
935 if (target
== GL_FRAGMENT_PROGRAM_ARB
) {
936 struct i915_fragment_program
*p
= (struct i915_fragment_program
*)prog
;
939 /* Hack: make sure fog is correctly enabled according to this
940 * fragment program's fog options.
942 ctx
->Driver
.Enable( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
943 ctx
->FragmentProgram
.Enabled
);
948 void i915ValidateFragmentProgram( i915ContextPtr i915
)
950 GLcontext
*ctx
= &i915
->intel
.ctx
;
951 intelContextPtr intel
= INTEL_CONTEXT(ctx
);
952 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
953 struct vertex_buffer
*VB
= &tnl
->vb
;
955 struct i915_fragment_program
*p
=
956 (struct i915_fragment_program
*)ctx
->FragmentProgram
._Current
;
958 GLuint inputsRead
= p
->FragProg
.InputsRead
;
959 GLuint s4
= i915
->state
.Ctx
[I915_CTXREG_LIS4
] & ~S4_VFMT_MASK
;
960 GLuint s2
= S2_TEXCOORD_NONE
;
965 VB
->AttribPtr
[VERT_ATTRIB_POS
] = VB
->NdcPtr
;
968 translate_program( p
);
970 intel
->vertex_attr_count
= 0;
971 intel
->wpos_offset
= 0;
972 intel
->wpos_size
= 0;
973 intel
->coloroffset
= 0;
974 intel
->specoffset
= 0;
976 if (inputsRead
& FRAG_BITS_TEX_ANY
) {
977 EMIT_ATTR( _TNL_ATTRIB_POS
, EMIT_4F_VIEWPORT
, S4_VFMT_XYZW
, 16 );
980 EMIT_ATTR( _TNL_ATTRIB_POS
, EMIT_3F_VIEWPORT
, S4_VFMT_XYZ
, 12 );
983 if (inputsRead
& FRAG_BIT_COL0
) {
984 intel
->coloroffset
= offset
/ 4;
985 EMIT_ATTR( _TNL_ATTRIB_COLOR0
, EMIT_4UB_4F_BGRA
, S4_VFMT_COLOR
, 4 );
988 if ((inputsRead
& (FRAG_BIT_COL1
|FRAG_BIT_FOGC
)) ||
989 i915
->vertex_fog
!= I915_FOG_NONE
) {
991 if (inputsRead
& FRAG_BIT_COL1
) {
992 intel
->specoffset
= offset
/ 4;
993 EMIT_ATTR( _TNL_ATTRIB_COLOR1
, EMIT_3UB_3F_BGR
, S4_VFMT_SPEC_FOG
, 3 );
998 if ((inputsRead
& FRAG_BIT_FOGC
) || i915
->vertex_fog
!= I915_FOG_NONE
)
999 EMIT_ATTR( _TNL_ATTRIB_FOG
, EMIT_1UB_1F
, S4_VFMT_SPEC_FOG
, 1 );
1005 if ((inputsRead
& FRAG_BIT_FOGC
) || i915
->vertex_fog
!= I915_FOG_NONE
) {
1006 EMIT_ATTR( _TNL_ATTRIB_FOG
, EMIT_1F
, S4_VFMT_FOG_PARAM
, 4 );
1010 for (i
= 0; i
< p
->ctx
->Const
.MaxTextureCoordUnits
; i
++) {
1011 if (inputsRead
& FRAG_BIT_TEX(i
)) {
1012 int sz
= VB
->TexCoordPtr
[i
]->size
;
1014 s2
&= ~S2_TEXCOORD_FMT(i
, S2_TEXCOORD_FMT0_MASK
);
1015 s2
|= S2_TEXCOORD_FMT(i
, SZ_TO_HW(sz
));
1017 EMIT_ATTR( _TNL_ATTRIB_TEX0
+i
, EMIT_SZ(sz
), 0, sz
* 4 );
1019 else if (i
== p
->wpos_tex
) {
1021 /* If WPOS is required, duplicate the XYZ position data in an
1022 * unused texture coordinate:
1024 s2
&= ~S2_TEXCOORD_FMT(i
, S2_TEXCOORD_FMT0_MASK
);
1025 s2
|= S2_TEXCOORD_FMT(i
, SZ_TO_HW(3));
1027 intel
->wpos_offset
= offset
;
1028 intel
->wpos_size
= 3 * sizeof(GLuint
);
1030 EMIT_PAD( intel
->wpos_size
);
1034 if (s2
!= i915
->state
.Ctx
[I915_CTXREG_LIS2
] ||
1035 s4
!= i915
->state
.Ctx
[I915_CTXREG_LIS4
]) {
1037 I915_STATECHANGE( i915
, I915_UPLOAD_CTX
);
1039 /* Must do this *after* statechange, so as not to affect
1040 * buffered vertices reliant on the old state:
1042 intel
->vertex_size
= _tnl_install_attrs( &intel
->ctx
,
1043 intel
->vertex_attrs
,
1044 intel
->vertex_attr_count
,
1045 intel
->ViewportMatrix
.m
, 0 );
1047 intel
->vertex_size
>>= 2;
1049 i915
->state
.Ctx
[I915_CTXREG_LIS2
] = s2
;
1050 i915
->state
.Ctx
[I915_CTXREG_LIS4
] = s4
;
1052 assert(intel
->vtbl
.check_vertex_size( intel
, intel
->vertex_size
));
1055 if (!p
->params_uptodate
)
1058 if (!p
->on_hardware
)
1059 i915_upload_program( i915
, p
);
1062 void i915InitFragProgFuncs( struct dd_function_table
*functions
)
1064 functions
->BindProgram
= i915BindProgram
;
1065 functions
->NewProgram
= i915NewProgram
;
1066 functions
->DeleteProgram
= i915DeleteProgram
;
1067 functions
->IsProgramNative
= i915IsProgramNative
;
1068 functions
->ProgramStringNotify
= i915ProgramStringNotify
;