2 /* FF is big and ugly so feel free to write lines as long as you like.
5 * Let me make that clearer:
6 * Aieeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ! !! !!!
10 #include "basetexture9.h"
11 #include "vertexdeclaration9.h"
12 #include "vertexshader9.h"
13 #include "pixelshader9.h"
15 #include "nine_defines.h"
16 #include "nine_helpers.h"
17 #include "nine_pipe.h"
18 #include "nine_dump.h"
20 #include "pipe/p_context.h"
21 #include "tgsi/tgsi_ureg.h"
22 #include "tgsi/tgsi_dump.h"
23 #include "util/u_box.h"
24 #include "util/u_hash_table.h"
25 #include "util/u_upload_mgr.h"
27 #define DBG_CHANNEL DBG_FF
29 #define NINE_FF_NUM_VS_CONST 256
30 #define NINE_FF_NUM_PS_CONST 24
41 uint32_t position_t
: 1;
42 uint32_t lighting
: 1;
43 uint32_t darkness
: 1; /* lighting enabled but no active lights */
44 uint32_t localviewer
: 1;
45 uint32_t vertexpointsize
: 1;
46 uint32_t pointscale
: 1;
47 uint32_t vertexblend
: 3;
48 uint32_t vertexblend_indexed
: 1;
49 uint32_t vertextween
: 1;
50 uint32_t mtl_diffuse
: 2; /* 0 = material, 1 = color1, 2 = color2 */
51 uint32_t mtl_ambient
: 2;
52 uint32_t mtl_specular
: 2;
53 uint32_t mtl_emissive
: 2;
54 uint32_t fog_mode
: 2;
55 uint32_t fog_range
: 1;
56 uint32_t color0in_one
: 1;
57 uint32_t color1in_one
: 1;
59 uint32_t specular_enable
: 1;
61 uint32_t tc_dim_input
: 16; /* 8 * 2 bits */
63 uint32_t tc_dim_output
: 24; /* 8 * 3 bits */
65 uint32_t tc_gen
: 24; /* 8 * 3 bits */
71 uint64_t value64
[3]; /* don't forget to resize VertexShader9.ff_key */
76 /* Texture stage state:
78 * COLOROP D3DTOP 5 bit
79 * ALPHAOP D3DTOP 5 bit
80 * COLORARG0 D3DTA 3 bit
81 * COLORARG1 D3DTA 3 bit
82 * COLORARG2 D3DTA 3 bit
83 * ALPHAARG0 D3DTA 3 bit
84 * ALPHAARG1 D3DTA 3 bit
85 * ALPHAARG2 D3DTA 3 bit
86 * RESULTARG D3DTA 1 bit (CURRENT:0 or TEMP:1)
87 * TEXCOORDINDEX 0 - 7 3 bit
88 * ===========================
98 uint32_t colorarg0
: 3;
99 uint32_t colorarg1
: 3;
100 uint32_t colorarg2
: 3;
101 uint32_t alphaarg0
: 3;
102 uint32_t alphaarg1
: 3;
103 uint32_t alphaarg2
: 3;
104 uint32_t resultarg
: 1; /* CURRENT:0 or TEMP:1 */
105 uint32_t textarget
: 2; /* 1D/2D/3D/CUBE */
107 /* that's 32 bit exactly */
109 uint32_t projected
: 16;
110 uint32_t fog
: 1; /* for vFog coming from VS */
111 uint32_t fog_mode
: 2;
112 uint32_t specular
: 1;
113 uint32_t pad1
: 12; /* 9 32-bit words with this */
114 uint8_t colorarg_b4
[3];
115 uint8_t colorarg_b5
[3];
116 uint8_t alphaarg_b4
[3]; /* 11 32-bit words plus a byte */
119 uint64_t value64
[6]; /* don't forget to resize PixelShader9.ff_key */
120 uint32_t value32
[12];
124 static unsigned nine_ff_vs_key_hash(void *key
)
126 struct nine_ff_vs_key
*vs
= key
;
128 uint32_t hash
= vs
->value32
[0];
129 for (i
= 1; i
< Elements(vs
->value32
); ++i
)
130 hash
^= vs
->value32
[i
];
133 static int nine_ff_vs_key_comp(void *key1
, void *key2
)
135 struct nine_ff_vs_key
*a
= (struct nine_ff_vs_key
*)key1
;
136 struct nine_ff_vs_key
*b
= (struct nine_ff_vs_key
*)key2
;
138 return memcmp(a
->value64
, b
->value64
, sizeof(a
->value64
));
140 static unsigned nine_ff_ps_key_hash(void *key
)
142 struct nine_ff_ps_key
*ps
= key
;
144 uint32_t hash
= ps
->value32
[0];
145 for (i
= 1; i
< Elements(ps
->value32
); ++i
)
146 hash
^= ps
->value32
[i
];
149 static int nine_ff_ps_key_comp(void *key1
, void *key2
)
151 struct nine_ff_ps_key
*a
= (struct nine_ff_ps_key
*)key1
;
152 struct nine_ff_ps_key
*b
= (struct nine_ff_ps_key
*)key2
;
154 return memcmp(a
->value64
, b
->value64
, sizeof(a
->value64
));
156 static unsigned nine_ff_fvf_key_hash(void *key
)
158 return *(DWORD
*)key
;
160 static int nine_ff_fvf_key_comp(void *key1
, void *key2
)
162 return *(DWORD
*)key1
!= *(DWORD
*)key2
;
165 static void nine_ff_prune_vs(struct NineDevice9
*);
166 static void nine_ff_prune_ps(struct NineDevice9
*);
168 static void nine_ureg_tgsi_dump(struct ureg_program
*ureg
, boolean override
)
170 if (debug_get_bool_option("NINE_FF_DUMP", FALSE
) || override
) {
172 const struct tgsi_token
*toks
= ureg_get_tokens(ureg
, &count
);
174 ureg_free_tokens(toks
);
178 #define _X(r) ureg_scalar(ureg_src(r), TGSI_SWIZZLE_X)
179 #define _Y(r) ureg_scalar(ureg_src(r), TGSI_SWIZZLE_Y)
180 #define _Z(r) ureg_scalar(ureg_src(r), TGSI_SWIZZLE_Z)
181 #define _W(r) ureg_scalar(ureg_src(r), TGSI_SWIZZLE_W)
183 #define _XXXX(r) ureg_scalar(r, TGSI_SWIZZLE_X)
184 #define _YYYY(r) ureg_scalar(r, TGSI_SWIZZLE_Y)
185 #define _ZZZZ(r) ureg_scalar(r, TGSI_SWIZZLE_Z)
186 #define _WWWW(r) ureg_scalar(r, TGSI_SWIZZLE_W)
190 /* AL should contain base address of lights table. */
191 #define LIGHT_CONST(i) \
192 ureg_src_indirect(ureg_DECL_constant(ureg, i), _X(AL))
194 #define MATERIAL_CONST(i) \
195 ureg_DECL_constant(ureg, 19 + (i))
197 #define _CONST(n) ureg_DECL_constant(ureg, n)
199 /* VS FF constants layout:
201 * CONST[ 0.. 3] D3DTS_WORLD * D3DTS_VIEW * D3DTS_PROJECTION
202 * CONST[ 4.. 7] D3DTS_WORLD * D3DTS_VIEW
203 * CONST[ 8..11] D3DTS_VIEW * D3DTS_PROJECTION
204 * CONST[12..15] D3DTS_VIEW
205 * CONST[16..18] Normal matrix
207 * CONST[19] MATERIAL.Emissive + Material.Ambient * RS.Ambient
208 * CONST[20] MATERIAL.Diffuse
209 * CONST[21] MATERIAL.Ambient
210 * CONST[22] MATERIAL.Specular
211 * CONST[23].x___ MATERIAL.Power
212 * CONST[24] MATERIAL.Emissive
213 * CONST[25] RS.Ambient
215 * CONST[26].x___ RS.PointSizeMin
216 * CONST[26]._y__ RS.PointSizeMax
217 * CONST[26].__z_ RS.PointSize
218 * CONST[26].___w RS.PointScaleA
219 * CONST[27].x___ RS.PointScaleB
220 * CONST[27]._y__ RS.PointScaleC
222 * CONST[28].x___ RS.FogEnd
223 * CONST[28]._y__ 1.0f / (RS.FogEnd - RS.FogStart)
224 * CONST[28].__z_ RS.FogDensity
226 * CONST[30].x___ TWEENFACTOR
228 * CONST[32].x___ LIGHT[0].Type
229 * CONST[32]._yzw LIGHT[0].Attenuation0,1,2
230 * CONST[33] LIGHT[0].Diffuse
231 * CONST[34] LIGHT[0].Specular
232 * CONST[35] LIGHT[0].Ambient
233 * CONST[36].xyz_ LIGHT[0].Position
234 * CONST[36].___w LIGHT[0].Range
235 * CONST[37].xyz_ LIGHT[0].Direction
236 * CONST[37].___w LIGHT[0].Falloff
237 * CONST[38].x___ cos(LIGHT[0].Theta / 2)
238 * CONST[38]._y__ cos(LIGHT[0].Phi / 2)
239 * CONST[38].__z_ 1.0f / (cos(LIGHT[0].Theta / 2) - cos(Light[0].Phi / 2))
240 * CONST[39].xyz_ LIGHT[0].HalfVector (for directional lights)
241 * CONST[39].___w 1 if this is the last active light, 0 if not
249 * NOTE: no lighting code is generated if there are no active lights
251 * CONST[100].x___ Viewport 2/width
252 * CONST[100]._y__ Viewport 2/height
253 * CONST[100].__z_ Viewport 1/(zmax - zmin)
254 * CONST[101].x___ Viewport x0
255 * CONST[101]._y__ Viewport y0
256 * CONST[101].__z_ Viewport z0
258 * CONST[128..131] D3DTS_TEXTURE0
259 * CONST[132..135] D3DTS_TEXTURE1
260 * CONST[136..139] D3DTS_TEXTURE2
261 * CONST[140..143] D3DTS_TEXTURE3
262 * CONST[144..147] D3DTS_TEXTURE4
263 * CONST[148..151] D3DTS_TEXTURE5
264 * CONST[152..155] D3DTS_TEXTURE6
265 * CONST[156..159] D3DTS_TEXTURE7
267 * CONST[224] D3DTS_WORLDMATRIX[0]
268 * CONST[228] D3DTS_WORLDMATRIX[1]
270 * CONST[252] D3DTS_WORLDMATRIX[7]
274 struct ureg_program
*ureg
;
275 const struct nine_ff_vs_key
*key
;
277 uint16_t input
[PIPE_MAX_ATTRIBS
];
280 struct ureg_src aVtx
;
281 struct ureg_src aNrm
;
282 struct ureg_src aCol
[2];
283 struct ureg_src aTex
[8];
284 struct ureg_src aPsz
;
285 struct ureg_src aInd
;
286 struct ureg_src aWgt
;
288 struct ureg_src aVtx1
; /* tweening */
289 struct ureg_src aNrm1
;
291 struct ureg_src mtlA
;
292 struct ureg_src mtlD
;
293 struct ureg_src mtlS
;
294 struct ureg_src mtlE
;
297 static inline unsigned
298 get_texcoord_sn(struct pipe_screen
*screen
)
300 if (screen
->get_param(screen
, PIPE_CAP_TGSI_TEXCOORD
))
301 return TGSI_SEMANTIC_TEXCOORD
;
302 return TGSI_SEMANTIC_GENERIC
;
305 static inline struct ureg_src
306 build_vs_add_input(struct vs_build_ctx
*vs
, uint16_t ndecl
)
308 const unsigned i
= vs
->num_inputs
++;
309 assert(i
< PIPE_MAX_ATTRIBS
);
310 vs
->input
[i
] = ndecl
;
311 return ureg_DECL_vs_input(vs
->ureg
, i
);
314 /* NOTE: dst may alias src */
316 ureg_normalize3(struct ureg_program
*ureg
,
317 struct ureg_dst dst
, struct ureg_src src
,
320 struct ureg_dst tmp_x
= ureg_writemask(tmp
, TGSI_WRITEMASK_X
);
322 ureg_DP3(ureg
, tmp_x
, src
, src
);
323 ureg_RSQ(ureg
, tmp_x
, _X(tmp
));
324 ureg_MUL(ureg
, dst
, src
, _X(tmp
));
328 nine_ff_build_vs(struct NineDevice9
*device
, struct vs_build_ctx
*vs
)
330 const struct nine_ff_vs_key
*key
= vs
->key
;
331 struct ureg_program
*ureg
= ureg_create(PIPE_SHADER_VERTEX
);
332 struct ureg_dst oPos
, oCol
[2], oPsz
, oFog
;
333 struct ureg_dst rVtx
, rNrm
;
334 struct ureg_dst r
[8];
336 struct ureg_dst tmp
, tmp_x
, tmp_y
, tmp_z
;
338 unsigned label
[32], l
= 0;
340 boolean need_rNrm
= key
->lighting
|| key
->pointscale
|| key
->passthrough
& (1 << NINE_DECLUSAGE_NORMAL
);
341 boolean need_rVtx
= key
->lighting
|| key
->fog_mode
;
342 const unsigned texcoord_sn
= get_texcoord_sn(device
->screen
);
346 /* Check which inputs we should transform. */
347 for (i
= 0; i
< 8 * 3; i
+= 3) {
348 switch ((key
->tc_gen
>> i
) & 0x3) {
349 case NINED3DTSS_TCI_CAMERASPACENORMAL
:
352 case NINED3DTSS_TCI_CAMERASPACEPOSITION
:
355 case NINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR
:
356 need_rVtx
= need_rNrm
= TRUE
;
363 /* Declare and record used inputs (needed for linkage with vertex format):
364 * (texture coordinates handled later)
366 vs
->aVtx
= build_vs_add_input(vs
,
367 key
->position_t
? NINE_DECLUSAGE_POSITIONT
: NINE_DECLUSAGE_POSITION
);
370 vs
->aNrm
= build_vs_add_input(vs
, NINE_DECLUSAGE_NORMAL
);
372 vs
->aCol
[0] = ureg_imm1f(ureg
, 1.0f
);
373 vs
->aCol
[1] = ureg_imm1f(ureg
, 1.0f
);
375 if (key
->lighting
|| key
->darkness
) {
376 const unsigned mask
= key
->mtl_diffuse
| key
->mtl_specular
|
377 key
->mtl_ambient
| key
->mtl_emissive
;
378 if ((mask
& 0x1) && !key
->color0in_one
)
379 vs
->aCol
[0] = build_vs_add_input(vs
, NINE_DECLUSAGE_i(COLOR
, 0));
380 if ((mask
& 0x2) && !key
->color1in_one
)
381 vs
->aCol
[1] = build_vs_add_input(vs
, NINE_DECLUSAGE_i(COLOR
, 1));
383 vs
->mtlD
= MATERIAL_CONST(1);
384 vs
->mtlA
= MATERIAL_CONST(2);
385 vs
->mtlS
= MATERIAL_CONST(3);
386 vs
->mtlE
= MATERIAL_CONST(5);
387 if (key
->mtl_diffuse
== 1) vs
->mtlD
= vs
->aCol
[0]; else
388 if (key
->mtl_diffuse
== 2) vs
->mtlD
= vs
->aCol
[1];
389 if (key
->mtl_ambient
== 1) vs
->mtlA
= vs
->aCol
[0]; else
390 if (key
->mtl_ambient
== 2) vs
->mtlA
= vs
->aCol
[1];
391 if (key
->mtl_specular
== 1) vs
->mtlS
= vs
->aCol
[0]; else
392 if (key
->mtl_specular
== 2) vs
->mtlS
= vs
->aCol
[1];
393 if (key
->mtl_emissive
== 1) vs
->mtlE
= vs
->aCol
[0]; else
394 if (key
->mtl_emissive
== 2) vs
->mtlE
= vs
->aCol
[1];
396 if (!key
->color0in_one
) vs
->aCol
[0] = build_vs_add_input(vs
, NINE_DECLUSAGE_i(COLOR
, 0));
397 if (!key
->color1in_one
) vs
->aCol
[1] = build_vs_add_input(vs
, NINE_DECLUSAGE_i(COLOR
, 1));
400 if (key
->vertexpointsize
)
401 vs
->aPsz
= build_vs_add_input(vs
, NINE_DECLUSAGE_PSIZE
);
403 if (key
->vertexblend_indexed
|| key
->passthrough
& (1 << NINE_DECLUSAGE_BLENDINDICES
))
404 vs
->aInd
= build_vs_add_input(vs
, NINE_DECLUSAGE_BLENDINDICES
);
405 if (key
->vertexblend
|| key
->passthrough
& (1 << NINE_DECLUSAGE_BLENDWEIGHT
))
406 vs
->aWgt
= build_vs_add_input(vs
, NINE_DECLUSAGE_BLENDWEIGHT
);
407 if (key
->vertextween
) {
408 vs
->aVtx1
= build_vs_add_input(vs
, NINE_DECLUSAGE_i(POSITION
,1));
409 vs
->aNrm1
= build_vs_add_input(vs
, NINE_DECLUSAGE_i(NORMAL
,1));
414 oPos
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0); /* HPOS */
415 oCol
[0] = ureg_saturate(ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0));
416 oCol
[1] = ureg_saturate(ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 1));
417 if (key
->fog
|| key
->passthrough
& (1 << NINE_DECLUSAGE_FOG
)) {
418 oFog
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_FOG
, 0);
419 oFog
= ureg_writemask(oFog
, TGSI_WRITEMASK_X
);
422 if (key
->vertexpointsize
|| key
->pointscale
) {
423 oPsz
= ureg_DECL_output_masked(ureg
, TGSI_SEMANTIC_PSIZE
, 0,
424 TGSI_WRITEMASK_X
, 0, 1);
425 oPsz
= ureg_writemask(oPsz
, TGSI_WRITEMASK_X
);
430 for (i
= 0; i
< num_r
; ++i
)
431 r
[i
] = ureg_DECL_local_temporary(ureg
);
433 tmp_x
= ureg_writemask(tmp
, TGSI_WRITEMASK_X
);
434 tmp_y
= ureg_writemask(tmp
, TGSI_WRITEMASK_Y
);
435 tmp_z
= ureg_writemask(tmp
, TGSI_WRITEMASK_Z
);
436 if (key
->lighting
|| key
->vertexblend
)
437 AR
= ureg_DECL_address(ureg
);
439 rVtx
= ureg_writemask(r
[1], TGSI_WRITEMASK_XYZ
);
440 rNrm
= ureg_writemask(r
[2], TGSI_WRITEMASK_XYZ
);
442 /* === Vertex transformation / vertex blending:
444 if (key
->vertextween
) {
445 assert(!key
->vertexblend
);
446 ureg_LRP(ureg
, r
[2], _XXXX(_CONST(30)), vs
->aVtx
, vs
->aVtx1
);
448 ureg_LRP(ureg
, r
[3], _XXXX(_CONST(30)), vs
->aNrm
, vs
->aNrm1
);
449 vs
->aVtx
= ureg_src(r
[2]);
450 vs
->aNrm
= ureg_src(r
[3]);
453 if (key
->vertexblend
) {
454 struct ureg_src cWM
[4];
456 for (i
= 224; i
<= 255; ++i
)
457 ureg_DECL_constant(ureg
, i
);
459 /* translate world matrix index to constant file index */
460 if (key
->vertexblend_indexed
) {
461 ureg_MAD(ureg
, tmp
, vs
->aInd
, ureg_imm1f(ureg
, 4.0f
), ureg_imm1f(ureg
, 224.0f
));
462 ureg_ARL(ureg
, AR
, ureg_src(tmp
));
465 ureg_MOV(ureg
, r
[2], ureg_imm4f(ureg
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
466 ureg_MOV(ureg
, r
[3], ureg_imm4f(ureg
, 1.0f
, 1.0f
, 1.0f
, 1.0f
));
468 for (i
= 0; i
< key
->vertexblend
; ++i
) {
469 for (c
= 0; c
< 4; ++c
) {
470 cWM
[c
] = ureg_src_register(TGSI_FILE_CONSTANT
, (224 + i
* 4) * !key
->vertexblend_indexed
+ c
);
471 if (key
->vertexblend_indexed
)
472 cWM
[c
] = ureg_src_indirect(cWM
[c
], ureg_scalar(ureg_src(AR
), i
));
474 /* multiply by WORLD(index) */
475 ureg_MUL(ureg
, tmp
, _XXXX(vs
->aVtx
), cWM
[0]);
476 ureg_MAD(ureg
, tmp
, _YYYY(vs
->aVtx
), cWM
[1], ureg_src(tmp
));
477 ureg_MAD(ureg
, tmp
, _ZZZZ(vs
->aVtx
), cWM
[2], ureg_src(tmp
));
478 ureg_MAD(ureg
, tmp
, _WWWW(vs
->aVtx
), cWM
[3], ureg_src(tmp
));
480 if (i
< (key
->vertexblend
- 1)) {
481 /* accumulate weighted position value */
482 ureg_MAD(ureg
, r
[2], ureg_src(tmp
), ureg_scalar(vs
->aWgt
, i
), ureg_src(r
[2]));
483 /* subtract weighted position value for last value */
484 ureg_SUB(ureg
, r
[3], ureg_src(r
[3]), ureg_scalar(vs
->aWgt
, i
));
488 /* the last weighted position is always 1 - sum_of_previous_weights */
489 ureg_MAD(ureg
, r
[2], ureg_src(tmp
), ureg_scalar(ureg_src(r
[3]), key
->vertexblend
- 1), ureg_src(r
[2]));
491 /* multiply by VIEW_PROJ */
492 ureg_MUL(ureg
, tmp
, _X(r
[2]), _CONST(8));
493 ureg_MAD(ureg
, tmp
, _Y(r
[2]), _CONST(9), ureg_src(tmp
));
494 ureg_MAD(ureg
, tmp
, _Z(r
[2]), _CONST(10), ureg_src(tmp
));
495 ureg_MAD(ureg
, oPos
, _W(r
[2]), _CONST(11), ureg_src(tmp
));
498 vs
->aVtx
= ureg_src(r
[2]);
500 if (key
->position_t
&& device
->driver_caps
.window_space_position_support
) {
501 ureg_MOV(ureg
, oPos
, vs
->aVtx
);
502 } else if (key
->position_t
) {
503 /* vs->aVtx contains the coordinates buffer wise.
504 * later in the pipeline, clipping, viewport and division
505 * by w (rhw = 1/w) are going to be applied, so do the reverse
506 * of these transformations (except clipping) to have the good
507 * position at the end.*/
508 ureg_MOV(ureg
, tmp
, vs
->aVtx
);
509 /* X from [X_min, X_min + width] to [-1, 1], same for Y. Z to [0, 1] */
510 ureg_SUB(ureg
, ureg_writemask(tmp
, TGSI_WRITEMASK_XYZ
), ureg_src(tmp
), _CONST(101));
511 ureg_MUL(ureg
, ureg_writemask(tmp
, TGSI_WRITEMASK_XYZ
), ureg_src(tmp
), _CONST(100));
512 ureg_SUB(ureg
, ureg_writemask(tmp
, TGSI_WRITEMASK_XY
), ureg_src(tmp
), ureg_imm1f(ureg
, 1.0f
));
513 /* Y needs to be reversed */
514 ureg_MOV(ureg
, ureg_writemask(tmp
, TGSI_WRITEMASK_Y
), ureg_negate(ureg_src(tmp
)));
516 ureg_RCP(ureg
, ureg_writemask(tmp
, TGSI_WRITEMASK_W
), _W(tmp
));
517 /* multiply X, Y, Z by w */
518 ureg_MUL(ureg
, ureg_writemask(tmp
, TGSI_WRITEMASK_XYZ
), ureg_src(tmp
), _W(tmp
));
519 ureg_MOV(ureg
, oPos
, ureg_src(tmp
));
521 /* position = vertex * WORLD_VIEW_PROJ */
522 ureg_MUL(ureg
, tmp
, _XXXX(vs
->aVtx
), _CONST(0));
523 ureg_MAD(ureg
, tmp
, _YYYY(vs
->aVtx
), _CONST(1), ureg_src(tmp
));
524 ureg_MAD(ureg
, tmp
, _ZZZZ(vs
->aVtx
), _CONST(2), ureg_src(tmp
));
525 ureg_MAD(ureg
, oPos
, _WWWW(vs
->aVtx
), _CONST(3), ureg_src(tmp
));
529 ureg_MUL(ureg
, rVtx
, _XXXX(vs
->aVtx
), _CONST(4));
530 ureg_MAD(ureg
, rVtx
, _YYYY(vs
->aVtx
), _CONST(5), ureg_src(rVtx
));
531 ureg_MAD(ureg
, rVtx
, _ZZZZ(vs
->aVtx
), _CONST(6), ureg_src(rVtx
));
532 ureg_MAD(ureg
, rVtx
, _WWWW(vs
->aVtx
), _CONST(7), ureg_src(rVtx
));
535 ureg_MUL(ureg
, rNrm
, _XXXX(vs
->aNrm
), _CONST(16));
536 ureg_MAD(ureg
, rNrm
, _YYYY(vs
->aNrm
), _CONST(17), ureg_src(rNrm
));
537 ureg_MAD(ureg
, rNrm
, _ZZZZ(vs
->aNrm
), _CONST(18), ureg_src(rNrm
));
538 ureg_normalize3(ureg
, rNrm
, ureg_src(rNrm
), tmp
);
540 /* NOTE: don't use vs->aVtx, vs->aNrm after this line */
542 /* === Process point size:
544 if (key
->vertexpointsize
) {
545 struct ureg_src cPsz1
= ureg_DECL_constant(ureg
, 26);
546 ureg_MAX(ureg
, tmp_x
, _XXXX(vs
->aPsz
), _XXXX(cPsz1
));
547 ureg_MIN(ureg
, oPsz
, _X(tmp
), _YYYY(cPsz1
));
548 } else if (key
->pointscale
) {
549 struct ureg_src cPsz1
= ureg_DECL_constant(ureg
, 26);
550 struct ureg_src cPsz2
= ureg_DECL_constant(ureg
, 27);
552 ureg_DP3(ureg
, tmp_x
, ureg_src(r
[1]), ureg_src(r
[1]));
553 ureg_RSQ(ureg
, tmp_y
, _X(tmp
));
554 ureg_MUL(ureg
, tmp_y
, _Y(tmp
), _X(tmp
));
555 ureg_CMP(ureg
, tmp_y
, ureg_negate(_Y(tmp
)), _Y(tmp
), ureg_imm1f(ureg
, 0.0f
));
556 ureg_MAD(ureg
, tmp_x
, _Y(tmp
), _YYYY(cPsz2
), _XXXX(cPsz2
));
557 ureg_MAD(ureg
, tmp_x
, _Y(tmp
), _X(tmp
), _WWWW(cPsz1
));
558 ureg_RCP(ureg
, tmp_x
, ureg_src(tmp
));
559 ureg_MUL(ureg
, tmp_x
, ureg_src(tmp
), _ZZZZ(cPsz1
));
560 ureg_MAX(ureg
, tmp_x
, _X(tmp
), _XXXX(cPsz1
));
561 ureg_MIN(ureg
, oPsz
, _X(tmp
), _YYYY(cPsz1
));
564 for (i
= 0; i
< 8; ++i
) {
565 struct ureg_dst oTex
, input_coord
, transformed
, t
;
566 unsigned c
, writemask
;
567 const unsigned tci
= (key
->tc_gen
>> (i
* 3)) & 0x7;
568 const unsigned idx
= (key
->tc_idx
>> (i
* 3)) & 0x7;
569 unsigned dim_input
= 1 + ((key
->tc_dim_input
>> (i
* 2)) & 0x3);
570 const unsigned dim_output
= (key
->tc_dim_output
>> (i
* 3)) & 0x7;
572 /* No texture output of index s */
573 if (tci
== NINED3DTSS_TCI_DISABLE
)
575 oTex
= ureg_DECL_output(ureg
, texcoord_sn
, i
);
579 /* Get the coordinate */
581 case NINED3DTSS_TCI_PASSTHRU
:
582 /* NINED3DTSS_TCI_PASSTHRU => Use texcoord coming from index idx *
583 * Else the idx is used only to determine wrapping mode. */
584 vs
->aTex
[idx
] = build_vs_add_input(vs
, NINE_DECLUSAGE_i(TEXCOORD
,idx
));
585 ureg_MOV(ureg
, input_coord
, vs
->aTex
[idx
]);
587 case NINED3DTSS_TCI_CAMERASPACENORMAL
:
588 ureg_MOV(ureg
, ureg_writemask(input_coord
, TGSI_WRITEMASK_XYZ
), ureg_src(rNrm
));
589 ureg_MOV(ureg
, ureg_writemask(input_coord
, TGSI_WRITEMASK_W
), ureg_imm1f(ureg
, 1.0f
));
592 case NINED3DTSS_TCI_CAMERASPACEPOSITION
:
593 ureg_MOV(ureg
, ureg_writemask(input_coord
, TGSI_WRITEMASK_XYZ
), ureg_src(rVtx
));
594 ureg_MOV(ureg
, ureg_writemask(input_coord
, TGSI_WRITEMASK_W
), ureg_imm1f(ureg
, 1.0f
));
597 case NINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR
:
598 tmp
.WriteMask
= TGSI_WRITEMASK_XYZ
;
599 ureg_DP3(ureg
, tmp_x
, ureg_src(rVtx
), ureg_src(rNrm
));
600 ureg_MUL(ureg
, tmp
, ureg_src(rNrm
), _X(tmp
));
601 ureg_ADD(ureg
, tmp
, ureg_src(tmp
), ureg_src(tmp
));
602 ureg_SUB(ureg
, ureg_writemask(input_coord
, TGSI_WRITEMASK_XYZ
), ureg_src(rVtx
), ureg_src(tmp
));
603 ureg_MOV(ureg
, ureg_writemask(input_coord
, TGSI_WRITEMASK_W
), ureg_imm1f(ureg
, 1.0f
));
605 tmp
.WriteMask
= TGSI_WRITEMASK_XYZW
;
607 case NINED3DTSS_TCI_SPHEREMAP
:
615 /* Apply the transformation */
616 /* dim_output == 0 => do not transform the components.
617 * XYZRHW also disables transformation */
618 if (!dim_output
|| key
->position_t
) {
619 transformed
= input_coord
;
620 writemask
= TGSI_WRITEMASK_XYZW
;
622 for (c
= 0; c
< dim_output
; c
++) {
623 t
= ureg_writemask(transformed
, 1 << c
);
625 /* dim_input = 1 2 3: -> we add trailing 1 to input*/
626 case 1: ureg_MAD(ureg
, t
, _X(input_coord
), _XXXX(_CONST(128 + i
* 4 + c
)), _YYYY(_CONST(128 + i
* 4 + c
)));
628 case 2: ureg_DP2(ureg
, t
, ureg_src(input_coord
), _CONST(128 + i
* 4 + c
));
629 ureg_ADD(ureg
, t
, ureg_src(transformed
), _ZZZZ(_CONST(128 + i
* 4 + c
)));
631 case 3: ureg_DP3(ureg
, t
, ureg_src(input_coord
), _CONST(128 + i
* 4 + c
));
632 ureg_ADD(ureg
, t
, ureg_src(transformed
), _WWWW(_CONST(128 + i
* 4 + c
)));
634 case 4: ureg_DP4(ureg
, t
, ureg_src(input_coord
), _CONST(128 + i
* 4 + c
)); break;
639 writemask
= (1 << dim_output
) - 1;
642 ureg_MOV(ureg
, ureg_writemask(oTex
, writemask
), ureg_src(transformed
));
647 * DIRECTIONAL: Light at infinite distance, parallel rays, no attenuation.
648 * POINT: Finite distance to scene, divergent rays, isotropic, attenuation.
649 * SPOT: Finite distance, divergent rays, angular dependence, attenuation.
651 * vec3 normal = normalize(in.Normal * NormalMatrix);
652 * vec3 hitDir = light.direction;
655 * if (light.type != DIRECTIONAL)
657 * vec3 hitVec = light.position - eyeVertex;
658 * float d = length(hitVec);
659 * hitDir = hitVec / d;
660 * atten = 1 / ((light.atten2 * d + light.atten1) * d + light.atten0);
663 * if (light.type == SPOTLIGHT)
665 * float rho = dp3(-hitVec, light.direction);
666 * if (rho < cos(light.phi / 2))
668 * if (rho < cos(light.theta / 2))
669 * atten *= pow(some_func(rho), light.falloff);
672 * float nDotHit = dp3_sat(normal, hitVec);
673 * float powFact = 0.0;
677 * vec3 midVec = normalize(hitDir + eye);
678 * float nDotMid = dp3_sat(normal, midVec);
679 * pFact = pow(nDotMid, material.power);
682 * ambient += light.ambient * atten;
683 * diffuse += light.diffuse * atten * nDotHit;
684 * specular += light.specular * atten * powFact;
687 struct ureg_dst rAtt
= ureg_writemask(r
[1], TGSI_WRITEMASK_W
);
688 struct ureg_dst rHit
= ureg_writemask(r
[3], TGSI_WRITEMASK_XYZ
);
689 struct ureg_dst rMid
= ureg_writemask(r
[4], TGSI_WRITEMASK_XYZ
);
691 struct ureg_dst rCtr
= ureg_writemask(r
[2], TGSI_WRITEMASK_W
);
693 struct ureg_dst AL
= ureg_writemask(AR
, TGSI_WRITEMASK_X
);
695 /* Light.*.Alpha is not used. */
696 struct ureg_dst rD
= ureg_writemask(r
[5], TGSI_WRITEMASK_XYZ
);
697 struct ureg_dst rA
= ureg_writemask(r
[6], TGSI_WRITEMASK_XYZ
);
698 struct ureg_dst rS
= ureg_writemask(r
[7], TGSI_WRITEMASK_XYZ
);
700 struct ureg_src mtlP
= _XXXX(MATERIAL_CONST(4));
702 struct ureg_src cLKind
= _XXXX(LIGHT_CONST(0));
703 struct ureg_src cLAtt0
= _YYYY(LIGHT_CONST(0));
704 struct ureg_src cLAtt1
= _ZZZZ(LIGHT_CONST(0));
705 struct ureg_src cLAtt2
= _WWWW(LIGHT_CONST(0));
706 struct ureg_src cLColD
= _XYZW(LIGHT_CONST(1));
707 struct ureg_src cLColS
= _XYZW(LIGHT_CONST(2));
708 struct ureg_src cLColA
= _XYZW(LIGHT_CONST(3));
709 struct ureg_src cLPos
= _XYZW(LIGHT_CONST(4));
710 struct ureg_src cLRng
= _WWWW(LIGHT_CONST(4));
711 struct ureg_src cLDir
= _XYZW(LIGHT_CONST(5));
712 struct ureg_src cLFOff
= _WWWW(LIGHT_CONST(5));
713 struct ureg_src cLTht
= _XXXX(LIGHT_CONST(6));
714 struct ureg_src cLPhi
= _YYYY(LIGHT_CONST(6));
715 struct ureg_src cLSDiv
= _ZZZZ(LIGHT_CONST(6));
716 struct ureg_src cLLast
= _WWWW(LIGHT_CONST(7));
718 const unsigned loop_label
= l
++;
720 ureg_MOV(ureg
, rCtr
, ureg_imm1f(ureg
, 32.0f
)); /* &lightconst(0) */
721 ureg_MOV(ureg
, rD
, ureg_imm1f(ureg
, 0.0f
));
722 ureg_MOV(ureg
, rA
, ureg_imm1f(ureg
, 0.0f
));
723 ureg_MOV(ureg
, rS
, ureg_imm1f(ureg
, 0.0f
));
724 rD
= ureg_saturate(rD
);
725 rA
= ureg_saturate(rA
);
726 rS
= ureg_saturate(rS
);
729 /* loop management */
730 ureg_BGNLOOP(ureg
, &label
[loop_label
]);
731 ureg_ARL(ureg
, AL
, _W(rCtr
));
733 /* if (not DIRECTIONAL light): */
734 ureg_SNE(ureg
, tmp_x
, cLKind
, ureg_imm1f(ureg
, D3DLIGHT_DIRECTIONAL
));
735 ureg_MOV(ureg
, rHit
, ureg_negate(cLDir
));
736 ureg_MOV(ureg
, rAtt
, ureg_imm1f(ureg
, 1.0f
));
737 ureg_IF(ureg
, _X(tmp
), &label
[l
++]);
739 /* hitDir = light.position - eyeVtx
742 ureg_SUB(ureg
, rHit
, cLPos
, ureg_src(rVtx
));
743 ureg_DP3(ureg
, tmp_x
, ureg_src(rHit
), ureg_src(rHit
));
744 ureg_RSQ(ureg
, tmp_y
, _X(tmp
));
745 ureg_MUL(ureg
, tmp_x
, _X(tmp
), _Y(tmp
)); /* length */
747 /* att = 1.0 / (light.att0 + (light.att1 + light.att2 * d) * d) */
748 ureg_MAD(ureg
, rAtt
, _X(tmp
), cLAtt2
, cLAtt1
);
749 ureg_MAD(ureg
, rAtt
, _X(tmp
), _W(rAtt
), cLAtt0
);
750 ureg_RCP(ureg
, rAtt
, _W(rAtt
));
751 /* cut-off if distance exceeds Light.Range */
752 ureg_SLT(ureg
, tmp_x
, _X(tmp
), cLRng
);
753 ureg_MUL(ureg
, rAtt
, _W(rAtt
), _X(tmp
));
755 ureg_fixup_label(ureg
, label
[l
-1], ureg_get_instruction_number(ureg
));
758 /* normalize hitDir */
759 ureg_normalize3(ureg
, rHit
, ureg_src(rHit
), tmp
);
761 /* if (SPOT light) */
762 ureg_SEQ(ureg
, tmp_x
, cLKind
, ureg_imm1f(ureg
, D3DLIGHT_SPOT
));
763 ureg_IF(ureg
, _X(tmp
), &label
[l
++]);
765 /* rho = dp3(-hitDir, light.spotDir)
767 * if (rho > light.ctht2) NOTE: 0 <= phi <= pi, 0 <= theta <= phi
770 * if (rho <= light.cphi2)
773 * spotAtt = (rho - light.cphi2) / (light.ctht2 - light.cphi2) ^ light.falloff
775 ureg_DP3(ureg
, tmp_y
, ureg_negate(ureg_src(rHit
)), cLDir
); /* rho */
776 ureg_SUB(ureg
, tmp_x
, _Y(tmp
), cLPhi
);
777 ureg_MUL(ureg
, tmp_x
, _X(tmp
), cLSDiv
);
778 ureg_POW(ureg
, tmp_x
, _X(tmp
), cLFOff
); /* spotAtten */
779 ureg_SGE(ureg
, tmp_z
, _Y(tmp
), cLTht
); /* if inside theta && phi */
780 ureg_SGE(ureg
, tmp_y
, _Y(tmp
), cLPhi
); /* if inside phi */
781 ureg_MAD(ureg
, ureg_saturate(tmp_x
), _X(tmp
), _Y(tmp
), _Z(tmp
));
782 ureg_MUL(ureg
, rAtt
, _W(rAtt
), _X(tmp
));
784 ureg_fixup_label(ureg
, label
[l
-1], ureg_get_instruction_number(ureg
));
787 /* directional factors, let's not use LIT because of clarity */
788 ureg_DP3(ureg
, ureg_saturate(tmp_x
), ureg_src(rNrm
), ureg_src(rHit
));
789 ureg_MOV(ureg
, tmp_y
, ureg_imm1f(ureg
, 0.0f
));
790 ureg_IF(ureg
, _X(tmp
), &label
[l
++]);
792 /* midVec = normalize(hitDir + eyeDir) */
793 if (key
->localviewer
) {
794 ureg_normalize3(ureg
, rMid
, ureg_src(rVtx
), tmp
);
795 ureg_SUB(ureg
, rMid
, ureg_src(rHit
), ureg_src(rMid
));
797 ureg_SUB(ureg
, rMid
, ureg_src(rHit
), ureg_imm3f(ureg
, 0.0f
, 0.0f
, 1.0f
));
799 ureg_normalize3(ureg
, rMid
, ureg_src(rMid
), tmp
);
800 ureg_DP3(ureg
, ureg_saturate(tmp_y
), ureg_src(rNrm
), ureg_src(rMid
));
801 ureg_POW(ureg
, tmp_y
, _Y(tmp
), mtlP
);
803 ureg_MUL(ureg
, tmp_x
, _W(rAtt
), _X(tmp
)); /* dp3(normal,hitDir) * att */
804 ureg_MUL(ureg
, tmp_y
, _W(rAtt
), _Y(tmp
)); /* power factor * att */
805 ureg_MAD(ureg
, rD
, cLColD
, _X(tmp
), ureg_src(rD
)); /* accumulate diffuse */
806 ureg_MAD(ureg
, rS
, cLColS
, _Y(tmp
), ureg_src(rS
)); /* accumulate specular */
808 ureg_fixup_label(ureg
, label
[l
-1], ureg_get_instruction_number(ureg
));
811 ureg_MAD(ureg
, rA
, cLColA
, _W(rAtt
), ureg_src(rA
)); /* accumulate ambient */
813 /* break if this was the last light */
814 ureg_IF(ureg
, cLLast
, &label
[l
++]);
817 ureg_fixup_label(ureg
, label
[l
-1], ureg_get_instruction_number(ureg
));
819 ureg_ADD(ureg
, rCtr
, _W(rCtr
), ureg_imm1f(ureg
, 8.0f
));
820 ureg_fixup_label(ureg
, label
[loop_label
], ureg_get_instruction_number(ureg
));
821 ureg_ENDLOOP(ureg
, &label
[loop_label
]);
823 /* Set alpha factors of illumination to 1.0 for the multiplications. */
824 rD
.WriteMask
= TGSI_WRITEMASK_W
; rD
.Saturate
= 0;
825 rS
.WriteMask
= TGSI_WRITEMASK_W
; rS
.Saturate
= 0;
826 rA
.WriteMask
= TGSI_WRITEMASK_W
; rA
.Saturate
= 0;
827 ureg_MOV(ureg
, rD
, ureg_imm1f(ureg
, 1.0f
));
828 ureg_MOV(ureg
, rS
, ureg_imm1f(ureg
, 1.0f
));
830 /* Apply to material:
832 * oCol[0] = (material.emissive + material.ambient * rs.ambient) +
833 * material.ambient * ambient +
834 * material.diffuse * diffuse +
835 * oCol[1] = material.specular * specular;
837 if (key
->mtl_emissive
== 0 && key
->mtl_ambient
== 0) {
838 ureg_MOV(ureg
, rA
, ureg_imm1f(ureg
, 1.0f
));
839 ureg_MAD(ureg
, tmp
, ureg_src(rA
), vs
->mtlA
, _CONST(19));
841 ureg_ADD(ureg
, ureg_writemask(tmp
, TGSI_WRITEMASK_XYZ
), ureg_src(rA
), _CONST(25));
842 ureg_MAD(ureg
, ureg_writemask(tmp
, TGSI_WRITEMASK_XYZ
), vs
->mtlA
, ureg_src(tmp
), vs
->mtlE
);
843 ureg_ADD(ureg
, ureg_writemask(tmp
, TGSI_WRITEMASK_W
), vs
->mtlA
, vs
->mtlE
);
846 if (key
->specular_enable
) {
847 /* add oCol[1] to oCol[0] */
848 ureg_MAD(ureg
, tmp
, ureg_src(rD
), vs
->mtlD
, ureg_src(tmp
));
849 ureg_MAD(ureg
, oCol
[0], ureg_src(rS
), vs
->mtlS
, ureg_src(tmp
));
851 ureg_MAD(ureg
, oCol
[0], ureg_src(rD
), vs
->mtlD
, ureg_src(tmp
));
853 ureg_MUL(ureg
, oCol
[1], ureg_src(rS
), vs
->mtlS
);
857 if (key
->mtl_emissive
== 0 && key
->mtl_ambient
== 0) {
858 ureg_MAD(ureg
, oCol
[0], vs
->mtlD
, ureg_imm4f(ureg
, 0.0f
, 0.0f
, 0.0f
, 1.0f
), _CONST(19));
860 ureg_MAD(ureg
, ureg_writemask(oCol
[0], TGSI_WRITEMASK_XYZ
), vs
->mtlA
, _CONST(25), vs
->mtlE
);
861 ureg_ADD(ureg
, ureg_writemask(tmp
, TGSI_WRITEMASK_W
), vs
->mtlA
, vs
->mtlE
);
862 ureg_ADD(ureg
, ureg_writemask(oCol
[0], TGSI_WRITEMASK_W
), vs
->mtlD
, _W(tmp
));
864 ureg_MUL(ureg
, oCol
[1], ureg_imm4f(ureg
, 0.0f
, 0.0f
, 0.0f
, 1.0f
), vs
->mtlS
);
866 ureg_MOV(ureg
, oCol
[0], vs
->aCol
[0]);
867 ureg_MOV(ureg
, oCol
[1], vs
->aCol
[1]);
872 * exp(x) = ex2(log2(e) * x)
875 if (key
->position_t
) {
876 ureg_MOV(ureg
, ureg_saturate(tmp_x
), ureg_scalar(vs
->aCol
[1], TGSI_SWIZZLE_W
));
878 if (key
->fog_range
) {
879 ureg_DP3(ureg
, tmp_x
, ureg_src(rVtx
), ureg_src(rVtx
));
880 ureg_RSQ(ureg
, tmp_z
, _X(tmp
));
881 ureg_MUL(ureg
, tmp_z
, _Z(tmp
), _X(tmp
));
883 ureg_MOV(ureg
, tmp_z
, ureg_abs(_Z(rVtx
)));
886 if (key
->fog_mode
== D3DFOG_EXP
) {
887 ureg_MUL(ureg
, tmp_x
, _Z(tmp
), _ZZZZ(_CONST(28)));
888 ureg_MUL(ureg
, tmp_x
, _X(tmp
), ureg_imm1f(ureg
, -1.442695f
));
889 ureg_EX2(ureg
, tmp_x
, _X(tmp
));
891 if (key
->fog_mode
== D3DFOG_EXP2
) {
892 ureg_MUL(ureg
, tmp_x
, _Z(tmp
), _ZZZZ(_CONST(28)));
893 ureg_MUL(ureg
, tmp_x
, _X(tmp
), _X(tmp
));
894 ureg_MUL(ureg
, tmp_x
, _X(tmp
), ureg_imm1f(ureg
, -1.442695f
));
895 ureg_EX2(ureg
, tmp_x
, _X(tmp
));
897 if (key
->fog_mode
== D3DFOG_LINEAR
&& !key
->position_t
) {
898 ureg_SUB(ureg
, tmp_x
, _XXXX(_CONST(28)), _Z(tmp
));
899 ureg_MUL(ureg
, ureg_saturate(tmp_x
), _X(tmp
), _YYYY(_CONST(28)));
901 ureg_MOV(ureg
, oFog
, _X(tmp
));
902 } else if (key
->fog
&& !(key
->passthrough
& (1 << NINE_DECLUSAGE_FOG
))) {
903 ureg_MOV(ureg
, oFog
, ureg_scalar(vs
->aCol
[1], TGSI_SWIZZLE_W
));
906 if (key
->passthrough
& (1 << NINE_DECLUSAGE_BLENDWEIGHT
)) {
907 struct ureg_src input
;
908 struct ureg_dst output
;
910 output
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_GENERIC
, 18);
911 ureg_MOV(ureg
, output
, input
);
913 if (key
->passthrough
& (1 << NINE_DECLUSAGE_BLENDINDICES
)) {
914 struct ureg_src input
;
915 struct ureg_dst output
;
917 output
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_GENERIC
, 19);
918 ureg_MOV(ureg
, output
, input
);
920 if (key
->passthrough
& (1 << NINE_DECLUSAGE_NORMAL
)) {
921 struct ureg_src input
;
922 struct ureg_dst output
;
924 output
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_GENERIC
, 20);
925 ureg_MOV(ureg
, output
, input
);
927 if (key
->passthrough
& (1 << NINE_DECLUSAGE_TANGENT
)) {
928 struct ureg_src input
;
929 struct ureg_dst output
;
930 input
= build_vs_add_input(vs
, NINE_DECLUSAGE_TANGENT
);
931 output
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_GENERIC
, 21);
932 ureg_MOV(ureg
, output
, input
);
934 if (key
->passthrough
& (1 << NINE_DECLUSAGE_BINORMAL
)) {
935 struct ureg_src input
;
936 struct ureg_dst output
;
937 input
= build_vs_add_input(vs
, NINE_DECLUSAGE_BINORMAL
);
938 output
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_GENERIC
, 22);
939 ureg_MOV(ureg
, output
, input
);
941 if (key
->passthrough
& (1 << NINE_DECLUSAGE_FOG
)) {
942 struct ureg_src input
;
943 struct ureg_dst output
;
944 input
= build_vs_add_input(vs
, NINE_DECLUSAGE_FOG
);
945 input
= ureg_scalar(input
, TGSI_SWIZZLE_X
);
947 ureg_MOV(ureg
, output
, input
);
949 if (key
->passthrough
& (1 << NINE_DECLUSAGE_DEPTH
)) {
950 (void) 0; /* TODO: replace z of position output ? */
954 if (key
->position_t
&& device
->driver_caps
.window_space_position_support
)
955 ureg_property(ureg
, TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION
, TRUE
);
958 nine_ureg_tgsi_dump(ureg
, FALSE
);
959 return ureg_create_shader_and_destroy(ureg
, device
->pipe
);
962 /* PS FF constants layout:
964 * CONST[ 0.. 7] stage[i].D3DTSS_CONSTANT
965 * CONST[ 8..15].x___ stage[i].D3DTSS_BUMPENVMAT00
966 * CONST[ 8..15]._y__ stage[i].D3DTSS_BUMPENVMAT01
967 * CONST[ 8..15].__z_ stage[i].D3DTSS_BUMPENVMAT10
968 * CONST[ 8..15].___w stage[i].D3DTSS_BUMPENVMAT11
969 * CONST[16..19].x_z_ stage[i].D3DTSS_BUMPENVLSCALE
970 * CONST[17..19]._y_w stage[i].D3DTSS_BUMPENVLOFFSET
972 * CONST[20] D3DRS_TEXTUREFACTOR
973 * CONST[21] D3DRS_FOGCOLOR
974 * CONST[22].x___ RS.FogEnd
975 * CONST[22]._y__ 1.0f / (RS.FogEnd - RS.FogStart)
976 * CONST[22].__z_ RS.FogDensity
980 struct ureg_program
*ureg
;
982 struct ureg_src vC
[2]; /* DIFFUSE, SPECULAR */
983 struct ureg_src vT
[8]; /* TEXCOORD[i] */
984 struct ureg_dst r
[6]; /* TEMPs */
985 struct ureg_dst rCur
; /* D3DTA_CURRENT */
986 struct ureg_dst rMod
;
987 struct ureg_src rCurSrc
;
988 struct ureg_dst rTmp
; /* D3DTA_TEMP */
989 struct ureg_src rTmpSrc
;
990 struct ureg_dst rTex
;
991 struct ureg_src rTexSrc
;
992 struct ureg_src cBEM
[8];
993 struct ureg_src s
[8];
997 unsigned index_pre_mod
;
1002 static struct ureg_src
1003 ps_get_ts_arg(struct ps_build_ctx
*ps
, unsigned ta
)
1005 struct ureg_src reg
;
1007 switch (ta
& D3DTA_SELECTMASK
) {
1008 case D3DTA_CONSTANT
:
1009 reg
= ureg_DECL_constant(ps
->ureg
, ps
->stage
.index
);
1012 reg
= (ps
->stage
.index
== ps
->stage
.index_pre_mod
) ? ureg_src(ps
->rMod
) : ps
->rCurSrc
;
1015 reg
= ureg_DECL_fs_input(ps
->ureg
, TGSI_SEMANTIC_COLOR
, 0, TGSI_INTERPOLATE_COLOR
);
1017 case D3DTA_SPECULAR
:
1018 reg
= ureg_DECL_fs_input(ps
->ureg
, TGSI_SEMANTIC_COLOR
, 1, TGSI_INTERPOLATE_COLOR
);
1027 reg
= ureg_DECL_constant(ps
->ureg
, 20);
1031 reg
= ureg_src_undef();
1034 if (ta
& D3DTA_COMPLEMENT
) {
1035 struct ureg_dst dst
= ps
->r
[ps
->stage
.num_regs
++];
1036 ureg_SUB(ps
->ureg
, dst
, ureg_imm1f(ps
->ureg
, 1.0f
), reg
);
1037 reg
= ureg_src(dst
);
1039 if (ta
& D3DTA_ALPHAREPLICATE
)
1044 static struct ureg_dst
1045 ps_get_ts_dst(struct ps_build_ctx
*ps
, unsigned ta
)
1047 assert(!(ta
& (D3DTA_COMPLEMENT
| D3DTA_ALPHAREPLICATE
)));
1049 switch (ta
& D3DTA_SELECTMASK
) {
1056 return ureg_dst_undef();
1060 static uint8_t ps_d3dtop_args_mask(D3DTEXTUREOP top
)
1063 case D3DTOP_DISABLE
:
1065 case D3DTOP_SELECTARG1
:
1066 case D3DTOP_PREMODULATE
:
1068 case D3DTOP_SELECTARG2
:
1070 case D3DTOP_MULTIPLYADD
:
1078 static inline boolean
1079 is_MOV_no_op(struct ureg_dst dst
, struct ureg_src src
)
1081 return !dst
.WriteMask
||
1082 (dst
.File
== src
.File
&&
1083 dst
.Index
== src
.Index
&&
1089 (!(dst
.WriteMask
& TGSI_WRITEMASK_X
) || (src
.SwizzleX
== TGSI_SWIZZLE_X
)) &&
1090 (!(dst
.WriteMask
& TGSI_WRITEMASK_Y
) || (src
.SwizzleY
== TGSI_SWIZZLE_Y
)) &&
1091 (!(dst
.WriteMask
& TGSI_WRITEMASK_Z
) || (src
.SwizzleZ
== TGSI_SWIZZLE_Z
)) &&
1092 (!(dst
.WriteMask
& TGSI_WRITEMASK_W
) || (src
.SwizzleW
== TGSI_SWIZZLE_W
)));
1097 ps_do_ts_op(struct ps_build_ctx
*ps
, unsigned top
, struct ureg_dst dst
, struct ureg_src
*arg
)
1099 struct ureg_program
*ureg
= ps
->ureg
;
1100 struct ureg_dst tmp
= ps
->r
[ps
->stage
.num_regs
];
1101 struct ureg_dst tmp2
= ps
->r
[ps
->stage
.num_regs
+1];
1102 struct ureg_dst tmp_x
= ureg_writemask(tmp
, TGSI_WRITEMASK_X
);
1104 tmp
.WriteMask
= dst
.WriteMask
;
1106 if (top
!= D3DTOP_SELECTARG1
&& top
!= D3DTOP_SELECTARG2
&&
1107 top
!= D3DTOP_MODULATE
&& top
!= D3DTOP_PREMODULATE
&&
1108 top
!= D3DTOP_BLENDDIFFUSEALPHA
&& top
!= D3DTOP_BLENDTEXTUREALPHA
&&
1109 top
!= D3DTOP_BLENDFACTORALPHA
&& top
!= D3DTOP_BLENDCURRENTALPHA
&&
1110 top
!= D3DTOP_BUMPENVMAP
&& top
!= D3DTOP_BUMPENVMAPLUMINANCE
&&
1112 dst
= ureg_saturate(dst
);
1115 case D3DTOP_SELECTARG1
:
1116 if (!is_MOV_no_op(dst
, arg
[1]))
1117 ureg_MOV(ureg
, dst
, arg
[1]);
1119 case D3DTOP_SELECTARG2
:
1120 if (!is_MOV_no_op(dst
, arg
[2]))
1121 ureg_MOV(ureg
, dst
, arg
[2]);
1123 case D3DTOP_MODULATE
:
1124 ureg_MUL(ureg
, dst
, arg
[1], arg
[2]);
1126 case D3DTOP_MODULATE2X
:
1127 ureg_MUL(ureg
, tmp
, arg
[1], arg
[2]);
1128 ureg_ADD(ureg
, dst
, ureg_src(tmp
), ureg_src(tmp
));
1130 case D3DTOP_MODULATE4X
:
1131 ureg_MUL(ureg
, tmp
, arg
[1], arg
[2]);
1132 ureg_MUL(ureg
, dst
, ureg_src(tmp
), ureg_imm1f(ureg
, 4.0f
));
1135 ureg_ADD(ureg
, dst
, arg
[1], arg
[2]);
1137 case D3DTOP_ADDSIGNED
:
1138 ureg_ADD(ureg
, tmp
, arg
[1], arg
[2]);
1139 ureg_SUB(ureg
, dst
, ureg_src(tmp
), ureg_imm1f(ureg
, 0.5f
));
1141 case D3DTOP_ADDSIGNED2X
:
1142 ureg_ADD(ureg
, tmp
, arg
[1], arg
[2]);
1143 ureg_MAD(ureg
, dst
, ureg_src(tmp
), ureg_imm1f(ureg
, 2.0f
), ureg_imm1f(ureg
, -1.0f
));
1145 case D3DTOP_SUBTRACT
:
1146 ureg_SUB(ureg
, dst
, arg
[1], arg
[2]);
1148 case D3DTOP_ADDSMOOTH
:
1149 ureg_SUB(ureg
, tmp
, ureg_imm1f(ureg
, 1.0f
), arg
[1]);
1150 ureg_MAD(ureg
, dst
, ureg_src(tmp
), arg
[2], arg
[1]);
1152 case D3DTOP_BLENDDIFFUSEALPHA
:
1153 ureg_LRP(ureg
, dst
, _WWWW(ps
->vC
[0]), arg
[1], arg
[2]);
1155 case D3DTOP_BLENDTEXTUREALPHA
:
1156 /* XXX: alpha taken from previous stage, texture or result ? */
1157 ureg_LRP(ureg
, dst
, _W(ps
->rTex
), arg
[1], arg
[2]);
1159 case D3DTOP_BLENDFACTORALPHA
:
1160 ureg_LRP(ureg
, dst
, _WWWW(_CONST(20)), arg
[1], arg
[2]);
1162 case D3DTOP_BLENDTEXTUREALPHAPM
:
1163 ureg_SUB(ureg
, tmp_x
, ureg_imm1f(ureg
, 1.0f
), _W(ps
->rTex
));
1164 ureg_MAD(ureg
, dst
, arg
[2], _X(tmp
), arg
[1]);
1166 case D3DTOP_BLENDCURRENTALPHA
:
1167 ureg_LRP(ureg
, dst
, _WWWW(ps
->rCurSrc
), arg
[1], arg
[2]);
1169 case D3DTOP_PREMODULATE
:
1170 ureg_MOV(ureg
, dst
, arg
[1]);
1171 ps
->stage
.index_pre_mod
= ps
->stage
.index
+ 1;
1173 case D3DTOP_MODULATEALPHA_ADDCOLOR
:
1174 ureg_MAD(ureg
, dst
, _WWWW(arg
[1]), arg
[2], arg
[1]);
1176 case D3DTOP_MODULATECOLOR_ADDALPHA
:
1177 ureg_MAD(ureg
, dst
, arg
[1], arg
[2], _WWWW(arg
[1]));
1179 case D3DTOP_MODULATEINVALPHA_ADDCOLOR
:
1180 ureg_SUB(ureg
, tmp_x
, ureg_imm1f(ureg
, 1.0f
), _WWWW(arg
[1]));
1181 ureg_MAD(ureg
, dst
, _X(tmp
), arg
[2], arg
[1]);
1183 case D3DTOP_MODULATEINVCOLOR_ADDALPHA
:
1184 ureg_SUB(ureg
, tmp
, ureg_imm1f(ureg
, 1.0f
), arg
[1]);
1185 ureg_MAD(ureg
, dst
, ureg_src(tmp
), arg
[2], _WWWW(arg
[1]));
1187 case D3DTOP_BUMPENVMAP
:
1189 case D3DTOP_BUMPENVMAPLUMINANCE
:
1191 case D3DTOP_DOTPRODUCT3
:
1192 ureg_SUB(ureg
, tmp
, arg
[1], ureg_imm4f(ureg
,0.5,0.5,0.5,0.5));
1193 ureg_SUB(ureg
, tmp2
, arg
[2] , ureg_imm4f(ureg
,0.5,0.5,0.5,0.5));
1194 ureg_DP3(ureg
, tmp
, ureg_src(tmp
), ureg_src(tmp2
));
1195 ureg_MUL(ureg
, ureg_saturate(dst
), ureg_src(tmp
), ureg_imm4f(ureg
,4.0,4.0,4.0,4.0));
1197 case D3DTOP_MULTIPLYADD
:
1198 ureg_MAD(ureg
, dst
, arg
[1], arg
[2], arg
[0]);
1201 ureg_LRP(ureg
, dst
, arg
[0], arg
[1], arg
[2]);
1203 case D3DTOP_DISABLE
:
1207 assert(!"invalid D3DTOP");
1213 nine_ff_build_ps(struct NineDevice9
*device
, struct nine_ff_ps_key
*key
)
1215 struct ps_build_ctx ps
;
1216 struct ureg_program
*ureg
= ureg_create(PIPE_SHADER_FRAGMENT
);
1217 struct ureg_dst oCol
;
1219 const unsigned texcoord_sn
= get_texcoord_sn(device
->screen
);
1221 memset(&ps
, 0, sizeof(ps
));
1223 ps
.stage
.index_pre_mod
= -1;
1225 ps
.vC
[0] = ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 0, TGSI_INTERPOLATE_COLOR
);
1227 /* Declare all TEMPs we might need, serious drivers have a register allocator. */
1228 for (i
= 0; i
< Elements(ps
.r
); ++i
)
1229 ps
.r
[i
] = ureg_DECL_local_temporary(ureg
);
1233 ps
.rCurSrc
= ureg_src(ps
.rCur
);
1234 ps
.rTmpSrc
= ureg_src(ps
.rTmp
);
1235 ps
.rTexSrc
= ureg_src(ps
.rTex
);
1237 for (s
= 0; s
< 8; ++s
) {
1238 ps
.s
[s
] = ureg_src_undef();
1240 if (key
->ts
[s
].colorop
!= D3DTOP_DISABLE
) {
1241 if (key
->ts
[s
].colorarg0
== D3DTA_SPECULAR
||
1242 key
->ts
[s
].colorarg1
== D3DTA_SPECULAR
||
1243 key
->ts
[s
].colorarg2
== D3DTA_SPECULAR
)
1244 ps
.vC
[1] = ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 1, TGSI_INTERPOLATE_COLOR
);
1246 if (key
->ts
[s
].colorarg0
== D3DTA_TEXTURE
||
1247 key
->ts
[s
].colorarg1
== D3DTA_TEXTURE
||
1248 key
->ts
[s
].colorarg2
== D3DTA_TEXTURE
) {
1249 ps
.s
[s
] = ureg_DECL_sampler(ureg
, s
);
1250 ps
.vT
[s
] = ureg_DECL_fs_input(ureg
, texcoord_sn
, s
, TGSI_INTERPOLATE_PERSPECTIVE
);
1252 if (s
&& (key
->ts
[s
- 1].colorop
== D3DTOP_PREMODULATE
||
1253 key
->ts
[s
- 1].alphaop
== D3DTOP_PREMODULATE
))
1254 ps
.s
[s
] = ureg_DECL_sampler(ureg
, s
);
1257 if (key
->ts
[s
].alphaop
!= D3DTOP_DISABLE
) {
1258 if (key
->ts
[s
].alphaarg0
== D3DTA_SPECULAR
||
1259 key
->ts
[s
].alphaarg1
== D3DTA_SPECULAR
||
1260 key
->ts
[s
].alphaarg2
== D3DTA_SPECULAR
)
1261 ps
.vC
[1] = ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 1, TGSI_INTERPOLATE_COLOR
);
1263 if (key
->ts
[s
].alphaarg0
== D3DTA_TEXTURE
||
1264 key
->ts
[s
].alphaarg1
== D3DTA_TEXTURE
||
1265 key
->ts
[s
].alphaarg2
== D3DTA_TEXTURE
) {
1266 ps
.s
[s
] = ureg_DECL_sampler(ureg
, s
);
1267 ps
.vT
[s
] = ureg_DECL_fs_input(ureg
, texcoord_sn
, s
, TGSI_INTERPOLATE_PERSPECTIVE
);
1272 ps
.vC
[1] = ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 1, TGSI_INTERPOLATE_COLOR
);
1274 oCol
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
1276 if (key
->ts
[0].colorop
== D3DTOP_DISABLE
&&
1277 key
->ts
[0].alphaop
== D3DTOP_DISABLE
)
1278 ureg_MOV(ureg
, ps
.rCur
, ps
.vC
[0]);
1279 /* Or is it undefined then ? */
1283 for (s
= 0; s
< 8; ++s
) {
1284 unsigned colorarg
[3];
1285 unsigned alphaarg
[3];
1286 const uint8_t used_c
= ps_d3dtop_args_mask(key
->ts
[s
].colorop
);
1287 const uint8_t used_a
= ps_d3dtop_args_mask(key
->ts
[s
].alphaop
);
1288 struct ureg_dst dst
;
1289 struct ureg_src arg
[3];
1291 if (key
->ts
[s
].colorop
== D3DTOP_DISABLE
&&
1292 key
->ts
[s
].alphaop
== D3DTOP_DISABLE
)
1295 ps
.stage
.num_regs
= 3;
1297 DBG("STAGE[%u]: colorop=%s alphaop=%s\n", s
,
1298 nine_D3DTOP_to_str(key
->ts
[s
].colorop
),
1299 nine_D3DTOP_to_str(key
->ts
[s
].alphaop
));
1301 if (!ureg_src_is_undef(ps
.s
[s
])) {
1303 switch (key
->ts
[s
].textarget
) {
1304 case 0: target
= TGSI_TEXTURE_1D
; break;
1305 case 1: target
= TGSI_TEXTURE_2D
; break;
1306 case 2: target
= TGSI_TEXTURE_3D
; break;
1307 case 3: target
= TGSI_TEXTURE_CUBE
; break;
1308 /* this is a 2 bit bitfield, do I really need a default case ? */
1311 /* sample the texture */
1312 if (key
->ts
[s
].colorop
== D3DTOP_BUMPENVMAP
||
1313 key
->ts
[s
].colorop
== D3DTOP_BUMPENVMAPLUMINANCE
) {
1315 if (key
->projected
& (3 << (s
*2))) {
1316 unsigned dim
= 1 + ((key
->projected
>> (2 * s
)) & 3);
1318 ureg_TXP(ureg
, ps
.rTex
, target
, ps
.vT
[s
], ps
.s
[s
]);
1320 ureg_RCP(ureg
, ureg_writemask(ps
.rTmp
, TGSI_WRITEMASK_X
), ureg_scalar(ps
.vT
[s
], dim
-1));
1321 ureg_MUL(ureg
, ps
.rTmp
, _XXXX(ps
.rTmpSrc
), ps
.vT
[s
]);
1322 ureg_TEX(ureg
, ps
.rTex
, target
, ps
.rTmpSrc
, ps
.s
[s
]);
1325 ureg_TEX(ureg
, ps
.rTex
, target
, ps
.vT
[s
], ps
.s
[s
]);
1330 (key
->ts
[0].resultarg
!= 0 /* not current */ ||
1331 key
->ts
[0].colorop
== D3DTOP_DISABLE
||
1332 key
->ts
[0].alphaop
== D3DTOP_DISABLE
||
1333 key
->ts
[0].colorop
== D3DTOP_BLENDCURRENTALPHA
||
1334 key
->ts
[0].alphaop
== D3DTOP_BLENDCURRENTALPHA
||
1335 key
->ts
[0].colorarg0
== D3DTA_CURRENT
||
1336 key
->ts
[0].colorarg1
== D3DTA_CURRENT
||
1337 key
->ts
[0].colorarg2
== D3DTA_CURRENT
||
1338 key
->ts
[0].alphaarg0
== D3DTA_CURRENT
||
1339 key
->ts
[0].alphaarg1
== D3DTA_CURRENT
||
1340 key
->ts
[0].alphaarg2
== D3DTA_CURRENT
)
1342 /* Initialize D3DTA_CURRENT.
1343 * (Yes we can do this before the loop but not until
1344 * NVE4 has an instruction scheduling pass.)
1346 ureg_MOV(ureg
, ps
.rCur
, ps
.vC
[0]);
1349 dst
= ps_get_ts_dst(&ps
, key
->ts
[s
].resultarg
? D3DTA_TEMP
: D3DTA_CURRENT
);
1351 if (ps
.stage
.index_pre_mod
== ps
.stage
.index
) {
1352 ps
.rMod
= ps
.r
[ps
.stage
.num_regs
++];
1353 ureg_MUL(ureg
, ps
.rMod
, ps
.rCurSrc
, ps
.rTexSrc
);
1356 colorarg
[0] = (key
->ts
[s
].colorarg0
| ((key
->colorarg_b4
[0] >> s
) << 4) | ((key
->colorarg_b5
[0] >> s
) << 5)) & 0x3f;
1357 colorarg
[1] = (key
->ts
[s
].colorarg1
| ((key
->colorarg_b4
[1] >> s
) << 4) | ((key
->colorarg_b5
[1] >> s
) << 5)) & 0x3f;
1358 colorarg
[2] = (key
->ts
[s
].colorarg2
| ((key
->colorarg_b4
[2] >> s
) << 4) | ((key
->colorarg_b5
[2] >> s
) << 5)) & 0x3f;
1359 alphaarg
[0] = (key
->ts
[s
].alphaarg0
| ((key
->alphaarg_b4
[0] >> s
) << 4)) & 0x1f;
1360 alphaarg
[1] = (key
->ts
[s
].alphaarg1
| ((key
->alphaarg_b4
[1] >> s
) << 4)) & 0x1f;
1361 alphaarg
[2] = (key
->ts
[s
].alphaarg2
| ((key
->alphaarg_b4
[2] >> s
) << 4)) & 0x1f;
1363 if (key
->ts
[s
].colorop
!= key
->ts
[s
].alphaop
||
1364 colorarg
[0] != alphaarg
[0] ||
1365 colorarg
[1] != alphaarg
[1] ||
1366 colorarg
[2] != alphaarg
[2])
1367 dst
.WriteMask
= TGSI_WRITEMASK_XYZ
;
1369 /* Special DOTPRODUCT behaviour (see wine tests) */
1370 if (key
->ts
[s
].colorop
== D3DTOP_DOTPRODUCT3
)
1371 dst
.WriteMask
= TGSI_WRITEMASK_XYZW
;
1373 if (used_c
& 0x1) arg
[0] = ps_get_ts_arg(&ps
, colorarg
[0]);
1374 if (used_c
& 0x2) arg
[1] = ps_get_ts_arg(&ps
, colorarg
[1]);
1375 if (used_c
& 0x4) arg
[2] = ps_get_ts_arg(&ps
, colorarg
[2]);
1376 ps_do_ts_op(&ps
, key
->ts
[s
].colorop
, dst
, arg
);
1378 if (dst
.WriteMask
!= TGSI_WRITEMASK_XYZW
) {
1379 dst
.WriteMask
= TGSI_WRITEMASK_W
;
1381 if (used_a
& 0x1) arg
[0] = ps_get_ts_arg(&ps
, alphaarg
[0]);
1382 if (used_a
& 0x2) arg
[1] = ps_get_ts_arg(&ps
, alphaarg
[1]);
1383 if (used_a
& 0x4) arg
[2] = ps_get_ts_arg(&ps
, alphaarg
[2]);
1384 ps_do_ts_op(&ps
, key
->ts
[s
].alphaop
, dst
, arg
);
1389 ureg_ADD(ureg
, ps
.rCur
, ps
.rCurSrc
, ps
.vC
[1]);
1393 if (key
->fog_mode
) {
1394 struct ureg_src vPos
;
1395 if (device
->screen
->get_param(device
->screen
,
1396 PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL
)) {
1397 vPos
= ureg_DECL_system_value(ureg
, TGSI_SEMANTIC_POSITION
, 0);
1399 vPos
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_POSITION
, 0,
1400 TGSI_INTERPOLATE_LINEAR
);
1403 struct ureg_dst rFog
= ureg_writemask(ps
.rTmp
, TGSI_WRITEMASK_X
);
1404 if (key
->fog_mode
== D3DFOG_EXP
) {
1405 ureg_MUL(ureg
, rFog
, _ZZZZ(vPos
), _ZZZZ(_CONST(22)));
1406 ureg_MUL(ureg
, rFog
, _X(rFog
), ureg_imm1f(ureg
, -1.442695f
));
1407 ureg_EX2(ureg
, rFog
, _X(rFog
));
1409 if (key
->fog_mode
== D3DFOG_EXP2
) {
1410 ureg_MUL(ureg
, rFog
, _ZZZZ(vPos
), _ZZZZ(_CONST(22)));
1411 ureg_MUL(ureg
, rFog
, _X(rFog
), _X(rFog
));
1412 ureg_MUL(ureg
, rFog
, _X(rFog
), ureg_imm1f(ureg
, -1.442695f
));
1413 ureg_EX2(ureg
, rFog
, _X(rFog
));
1415 if (key
->fog_mode
== D3DFOG_LINEAR
) {
1416 ureg_SUB(ureg
, rFog
, _XXXX(_CONST(22)), _ZZZZ(vPos
));
1417 ureg_MUL(ureg
, ureg_saturate(rFog
), _X(rFog
), _YYYY(_CONST(22)));
1419 ureg_LRP(ureg
, ureg_writemask(oCol
, TGSI_WRITEMASK_XYZ
), _X(rFog
), ps
.rCurSrc
, _CONST(21));
1420 ureg_MOV(ureg
, ureg_writemask(oCol
, TGSI_WRITEMASK_W
), ps
.rCurSrc
);
1423 struct ureg_src vFog
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_FOG
, 0, TGSI_INTERPOLATE_PERSPECTIVE
);
1424 ureg_LRP(ureg
, ureg_writemask(oCol
, TGSI_WRITEMASK_XYZ
), _XXXX(vFog
), ps
.rCurSrc
, _CONST(21));
1425 ureg_MOV(ureg
, ureg_writemask(oCol
, TGSI_WRITEMASK_W
), ps
.rCurSrc
);
1427 ureg_MOV(ureg
, oCol
, ps
.rCurSrc
);
1431 nine_ureg_tgsi_dump(ureg
, FALSE
);
1432 return ureg_create_shader_and_destroy(ureg
, device
->pipe
);
1435 static struct NineVertexShader9
*
1436 nine_ff_get_vs(struct NineDevice9
*device
)
1438 const struct nine_state
*state
= &device
->state
;
1439 struct NineVertexShader9
*vs
;
1440 enum pipe_error err
;
1441 struct vs_build_ctx bld
;
1442 struct nine_ff_vs_key key
;
1444 char input_texture_coord
[8];
1446 assert(sizeof(key
) <= sizeof(key
.value32
));
1448 memset(&key
, 0, sizeof(key
));
1449 memset(&bld
, 0, sizeof(bld
));
1450 memset(&input_texture_coord
, 0, sizeof(input_texture_coord
));
1454 /* FIXME: this shouldn't be NULL, but it is on init */
1456 key
.color0in_one
= 1;
1457 key
.color1in_one
= 1;
1458 for (i
= 0; i
< state
->vdecl
->nelems
; i
++) {
1459 uint16_t usage
= state
->vdecl
->usage_map
[i
];
1460 if (usage
== NINE_DECLUSAGE_POSITIONT
)
1462 else if (usage
== NINE_DECLUSAGE_i(COLOR
, 0))
1463 key
.color0in_one
= 0;
1464 else if (usage
== NINE_DECLUSAGE_i(COLOR
, 1))
1465 key
.color1in_one
= 0;
1466 else if (usage
== NINE_DECLUSAGE_PSIZE
)
1467 key
.vertexpointsize
= 1;
1468 else if (usage
% NINE_DECLUSAGE_COUNT
== NINE_DECLUSAGE_TEXCOORD
) {
1469 s
= usage
/ NINE_DECLUSAGE_COUNT
;
1471 input_texture_coord
[s
] = nine_decltype_get_dim(state
->vdecl
->decls
[i
].Type
);
1473 DBG("FF given texture coordinate >= 8. Ignoring\n");
1474 } else if (usage
< NINE_DECLUSAGE_NONE
)
1475 key
.passthrough
|= 1 << usage
;
1478 /* ff vs + ps 3.0: some elements are passed to the ps (wine test).
1479 * We do restrict to indices 0 */
1480 key
.passthrough
&= ~((1 << NINE_DECLUSAGE_POSITION
) | (1 << NINE_DECLUSAGE_PSIZE
) |
1481 (1 << NINE_DECLUSAGE_TEXCOORD
) | (1 << NINE_DECLUSAGE_POSITIONT
) |
1482 (1 << NINE_DECLUSAGE_TESSFACTOR
) | (1 << NINE_DECLUSAGE_SAMPLE
));
1483 if (!key
.vertexpointsize
)
1484 key
.pointscale
= !!state
->rs
[D3DRS_POINTSCALEENABLE
];
1486 key
.lighting
= !!state
->rs
[D3DRS_LIGHTING
] && state
->ff
.num_lights_active
;
1487 key
.darkness
= !!state
->rs
[D3DRS_LIGHTING
] && !state
->ff
.num_lights_active
;
1488 if (key
.position_t
) {
1489 key
.darkness
= 0; /* |= key.lighting; */ /* XXX ? */
1492 if ((key
.lighting
| key
.darkness
) && state
->rs
[D3DRS_COLORVERTEX
]) {
1493 key
.mtl_diffuse
= state
->rs
[D3DRS_DIFFUSEMATERIALSOURCE
];
1494 key
.mtl_ambient
= state
->rs
[D3DRS_AMBIENTMATERIALSOURCE
];
1495 key
.mtl_specular
= state
->rs
[D3DRS_SPECULARMATERIALSOURCE
];
1496 key
.mtl_emissive
= state
->rs
[D3DRS_EMISSIVEMATERIALSOURCE
];
1498 key
.fog
= !!state
->rs
[D3DRS_FOGENABLE
];
1499 key
.fog_mode
= state
->rs
[D3DRS_FOGENABLE
] ? state
->rs
[D3DRS_FOGVERTEXMODE
] : 0;
1501 key
.fog_range
= !key
.position_t
&& state
->rs
[D3DRS_RANGEFOGENABLE
];
1503 key
.localviewer
= !!state
->rs
[D3DRS_LOCALVIEWER
];
1504 key
.specular_enable
= !!state
->rs
[D3DRS_SPECULARENABLE
];
1506 if (state
->rs
[D3DRS_VERTEXBLEND
] != D3DVBF_DISABLE
) {
1507 key
.vertexblend_indexed
= !!state
->rs
[D3DRS_INDEXEDVERTEXBLENDENABLE
];
1509 switch (state
->rs
[D3DRS_VERTEXBLEND
]) {
1510 case D3DVBF_0WEIGHTS
: key
.vertexblend
= key
.vertexblend_indexed
; break;
1511 case D3DVBF_1WEIGHTS
: key
.vertexblend
= 2; break;
1512 case D3DVBF_2WEIGHTS
: key
.vertexblend
= 3; break;
1513 case D3DVBF_3WEIGHTS
: key
.vertexblend
= 4; break;
1514 case D3DVBF_TWEENING
: key
.vertextween
= 1; break;
1516 assert(!"invalid D3DVBF");
1521 for (s
= 0; s
< 8; ++s
) {
1522 unsigned gen
= (state
->ff
.tex_stage
[s
][D3DTSS_TEXCOORDINDEX
] >> 16) + 1;
1525 if (key
.position_t
&& gen
> NINED3DTSS_TCI_PASSTHRU
)
1526 gen
= NINED3DTSS_TCI_PASSTHRU
;
1528 if (!input_texture_coord
[s
] && gen
== NINED3DTSS_TCI_PASSTHRU
)
1529 gen
= NINED3DTSS_TCI_DISABLE
;
1531 key
.tc_gen
|= gen
<< (s
* 3);
1532 key
.tc_idx
|= (state
->ff
.tex_stage
[s
][D3DTSS_TEXCOORDINDEX
] & 7) << (s
* 3);
1533 key
.tc_dim_input
|= ((input_texture_coord
[s
]-1) & 0x3) << (s
* 2);
1535 dim
= state
->ff
.tex_stage
[s
][D3DTSS_TEXTURETRANSFORMFLAGS
] & 0x7;
1537 dim
= input_texture_coord
[s
];
1538 if (dim
== 1) /* NV behaviour */
1540 key
.tc_dim_output
|= dim
<< (s
* 3);
1543 vs
= util_hash_table_get(device
->ff
.ht_vs
, &key
);
1546 NineVertexShader9_new(device
, &vs
, NULL
, nine_ff_build_vs(device
, &bld
));
1548 nine_ff_prune_vs(device
);
1552 memcpy(&vs
->ff_key
, &key
, sizeof(vs
->ff_key
));
1554 err
= util_hash_table_set(device
->ff
.ht_vs
, &vs
->ff_key
, vs
);
1556 assert(err
== PIPE_OK
);
1557 device
->ff
.num_vs
++;
1558 NineUnknown_ConvertRefToBind(NineUnknown(vs
));
1560 vs
->num_inputs
= bld
.num_inputs
;
1561 for (n
= 0; n
< bld
.num_inputs
; ++n
)
1562 vs
->input_map
[n
].ndecl
= bld
.input
[n
];
1564 vs
->position_t
= key
.position_t
;
1565 vs
->point_size
= key
.vertexpointsize
| key
.pointscale
;
1570 static struct NinePixelShader9
*
1571 nine_ff_get_ps(struct NineDevice9
*device
)
1573 struct nine_state
*state
= &device
->state
;
1574 struct NinePixelShader9
*ps
;
1575 enum pipe_error err
;
1576 struct nine_ff_ps_key key
;
1578 uint8_t sampler_mask
= 0;
1580 assert(sizeof(key
) <= sizeof(key
.value32
));
1582 memset(&key
, 0, sizeof(key
));
1583 for (s
= 0; s
< 8; ++s
) {
1584 key
.ts
[s
].colorop
= state
->ff
.tex_stage
[s
][D3DTSS_COLOROP
];
1585 key
.ts
[s
].alphaop
= state
->ff
.tex_stage
[s
][D3DTSS_ALPHAOP
];
1586 /* MSDN says D3DTOP_DISABLE disables this and all subsequent stages. */
1587 /* ALPHAOP cannot be disabled if COLOROP is enabled. */
1588 if (key
.ts
[s
].colorop
== D3DTOP_DISABLE
) {
1589 key
.ts
[s
].alphaop
= D3DTOP_DISABLE
; /* DISABLE == 1, avoid degenerate keys */
1593 if (!state
->texture
[s
] &&
1594 state
->ff
.tex_stage
[s
][D3DTSS_COLORARG1
] == D3DTA_TEXTURE
) {
1595 /* This should also disable the stage. */
1596 key
.ts
[s
].colorop
= key
.ts
[s
].alphaop
= D3DTOP_DISABLE
;
1600 if (state
->ff
.tex_stage
[s
][D3DTSS_COLORARG1
] == D3DTA_TEXTURE
)
1601 sampler_mask
|= (1 << s
);
1603 if (key
.ts
[s
].colorop
!= D3DTOP_DISABLE
) {
1604 uint8_t used_c
= ps_d3dtop_args_mask(key
.ts
[s
].colorop
);
1605 if (used_c
& 0x1) key
.ts
[s
].colorarg0
= state
->ff
.tex_stage
[s
][D3DTSS_COLORARG0
];
1606 if (used_c
& 0x2) key
.ts
[s
].colorarg1
= state
->ff
.tex_stage
[s
][D3DTSS_COLORARG1
];
1607 if (used_c
& 0x4) key
.ts
[s
].colorarg2
= state
->ff
.tex_stage
[s
][D3DTSS_COLORARG2
];
1608 if (used_c
& 0x1) key
.colorarg_b4
[0] |= (state
->ff
.tex_stage
[s
][D3DTSS_COLORARG0
] >> 4) << s
;
1609 if (used_c
& 0x1) key
.colorarg_b5
[0] |= (state
->ff
.tex_stage
[s
][D3DTSS_COLORARG0
] >> 5) << s
;
1610 if (used_c
& 0x2) key
.colorarg_b4
[1] |= (state
->ff
.tex_stage
[s
][D3DTSS_COLORARG1
] >> 4) << s
;
1611 if (used_c
& 0x2) key
.colorarg_b5
[1] |= (state
->ff
.tex_stage
[s
][D3DTSS_COLORARG1
] >> 5) << s
;
1612 if (used_c
& 0x4) key
.colorarg_b4
[2] |= (state
->ff
.tex_stage
[s
][D3DTSS_COLORARG2
] >> 4) << s
;
1613 if (used_c
& 0x4) key
.colorarg_b5
[2] |= (state
->ff
.tex_stage
[s
][D3DTSS_COLORARG2
] >> 5) << s
;
1615 if (key
.ts
[s
].alphaop
!= D3DTOP_DISABLE
) {
1616 uint8_t used_a
= ps_d3dtop_args_mask(key
.ts
[s
].alphaop
);
1617 if (used_a
& 0x1) key
.ts
[s
].alphaarg0
= state
->ff
.tex_stage
[s
][D3DTSS_ALPHAARG0
];
1618 if (used_a
& 0x2) key
.ts
[s
].alphaarg1
= state
->ff
.tex_stage
[s
][D3DTSS_ALPHAARG1
];
1619 if (used_a
& 0x4) key
.ts
[s
].alphaarg2
= state
->ff
.tex_stage
[s
][D3DTSS_ALPHAARG2
];
1620 if (used_a
& 0x1) key
.alphaarg_b4
[0] |= (state
->ff
.tex_stage
[s
][D3DTSS_ALPHAARG0
] >> 4) << s
;
1621 if (used_a
& 0x2) key
.alphaarg_b4
[1] |= (state
->ff
.tex_stage
[s
][D3DTSS_ALPHAARG1
] >> 4) << s
;
1622 if (used_a
& 0x4) key
.alphaarg_b4
[2] |= (state
->ff
.tex_stage
[s
][D3DTSS_ALPHAARG2
] >> 4) << s
;
1624 key
.ts
[s
].resultarg
= state
->ff
.tex_stage
[s
][D3DTSS_RESULTARG
] == D3DTA_TEMP
;
1626 if (state
->texture
[s
]) {
1627 switch (state
->texture
[s
]->base
.type
) {
1628 case D3DRTYPE_TEXTURE
: key
.ts
[s
].textarget
= 1; break;
1629 case D3DRTYPE_VOLUMETEXTURE
: key
.ts
[s
].textarget
= 2; break;
1630 case D3DRTYPE_CUBETEXTURE
: key
.ts
[s
].textarget
= 3; break;
1632 assert(!"unexpected texture type");
1636 key
.ts
[s
].textarget
= 1;
1640 key
.projected
= nine_ff_get_projected_key(state
);
1643 key
.ts
[s
].colorop
= key
.ts
[s
].alphaop
= D3DTOP_DISABLE
;
1644 if (state
->rs
[D3DRS_FOGENABLE
])
1645 key
.fog_mode
= state
->rs
[D3DRS_FOGTABLEMODE
];
1646 key
.fog
= !!state
->rs
[D3DRS_FOGENABLE
];
1648 ps
= util_hash_table_get(device
->ff
.ht_ps
, &key
);
1651 NinePixelShader9_new(device
, &ps
, NULL
, nine_ff_build_ps(device
, &key
));
1653 nine_ff_prune_ps(device
);
1655 memcpy(&ps
->ff_key
, &key
, sizeof(ps
->ff_key
));
1657 err
= util_hash_table_set(device
->ff
.ht_ps
, &ps
->ff_key
, ps
);
1659 assert(err
== PIPE_OK
);
1660 device
->ff
.num_ps
++;
1661 NineUnknown_ConvertRefToBind(NineUnknown(ps
));
1664 ps
->sampler_mask
= sampler_mask
;
1669 #define GET_D3DTS(n) nine_state_access_transform(state, D3DTS_##n, FALSE)
1670 #define IS_D3DTS_DIRTY(s,n) ((s)->ff.changed.transform[(D3DTS_##n) / 32] & (1 << ((D3DTS_##n) % 32)))
1672 nine_ff_load_vs_transforms(struct NineDevice9
*device
)
1674 struct nine_state
*state
= &device
->state
;
1676 D3DMATRIX
*M
= (D3DMATRIX
*)device
->ff
.vs_const
;
1679 /* TODO: make this nicer, and only upload the ones we need */
1680 /* TODO: use ff.vs_const as storage of W, V, P matrices */
1682 if (IS_D3DTS_DIRTY(state
, WORLD
) ||
1683 IS_D3DTS_DIRTY(state
, VIEW
) ||
1684 IS_D3DTS_DIRTY(state
, PROJECTION
)) {
1685 /* WVP, WV matrices */
1686 nine_d3d_matrix_matrix_mul(&M
[1], GET_D3DTS(WORLD
), GET_D3DTS(VIEW
));
1687 nine_d3d_matrix_matrix_mul(&M
[0], &M
[1], GET_D3DTS(PROJECTION
));
1689 /* normal matrix == transpose(inverse(WV)) */
1690 nine_d3d_matrix_inverse_3x3(&T
, &M
[1]);
1691 nine_d3d_matrix_transpose(&M
[4], &T
);
1694 nine_d3d_matrix_matrix_mul(&M
[2], GET_D3DTS(VIEW
), GET_D3DTS(PROJECTION
));
1696 /* V and W matrix */
1697 M
[3] = *GET_D3DTS(VIEW
);
1698 M
[56] = *GET_D3DTS(WORLD
);
1701 if (state
->rs
[D3DRS_VERTEXBLEND
] != D3DVBF_DISABLE
) {
1702 /* load other world matrices */
1703 for (i
= 1; i
<= 7; ++i
)
1704 M
[56 + i
] = *GET_D3DTS(WORLDMATRIX(i
));
1707 device
->ff
.vs_const
[30 * 4] = asfloat(state
->rs
[D3DRS_TWEENFACTOR
]);
1711 nine_ff_load_lights(struct NineDevice9
*device
)
1713 struct nine_state
*state
= &device
->state
;
1714 struct fvec4
*dst
= (struct fvec4
*)device
->ff
.vs_const
;
1717 if (state
->changed
.group
& NINE_STATE_FF_MATERIAL
) {
1718 const D3DMATERIAL9
*mtl
= &state
->ff
.material
;
1720 memcpy(&dst
[20], &mtl
->Diffuse
, 4 * sizeof(float));
1721 memcpy(&dst
[21], &mtl
->Ambient
, 4 * sizeof(float));
1722 memcpy(&dst
[22], &mtl
->Specular
, 4 * sizeof(float));
1723 dst
[23].x
= mtl
->Power
;
1724 memcpy(&dst
[24], &mtl
->Emissive
, 4 * sizeof(float));
1725 d3dcolor_to_rgba(&dst
[25].x
, state
->rs
[D3DRS_AMBIENT
]);
1726 dst
[19].x
= dst
[25].x
* mtl
->Ambient
.r
+ mtl
->Emissive
.r
;
1727 dst
[19].y
= dst
[25].y
* mtl
->Ambient
.g
+ mtl
->Emissive
.g
;
1728 dst
[19].z
= dst
[25].z
* mtl
->Ambient
.b
+ mtl
->Emissive
.b
;
1729 dst
[19].w
= mtl
->Ambient
.a
+ mtl
->Emissive
.a
;
1732 if (!(state
->changed
.group
& NINE_STATE_FF_LIGHTING
))
1735 for (l
= 0; l
< state
->ff
.num_lights_active
; ++l
) {
1736 const D3DLIGHT9
*light
= &state
->ff
.light
[state
->ff
.active_light
[l
]];
1738 dst
[32 + l
* 8].x
= light
->Type
;
1739 dst
[32 + l
* 8].y
= light
->Attenuation0
;
1740 dst
[32 + l
* 8].z
= light
->Attenuation1
;
1741 dst
[32 + l
* 8].w
= light
->Attenuation2
;
1742 memcpy(&dst
[33 + l
* 8].x
, &light
->Diffuse
, sizeof(light
->Diffuse
));
1743 memcpy(&dst
[34 + l
* 8].x
, &light
->Specular
, sizeof(light
->Specular
));
1744 memcpy(&dst
[35 + l
* 8].x
, &light
->Ambient
, sizeof(light
->Ambient
));
1745 nine_d3d_vector4_matrix_mul((D3DVECTOR
*)&dst
[36 + l
* 8].x
, &light
->Position
, GET_D3DTS(VIEW
));
1746 nine_d3d_vector3_matrix_mul((D3DVECTOR
*)&dst
[37 + l
* 8].x
, &light
->Direction
, GET_D3DTS(VIEW
));
1747 dst
[36 + l
* 8].w
= light
->Type
== D3DLIGHT_DIRECTIONAL
? 1e9f
: light
->Range
;
1748 dst
[37 + l
* 8].w
= light
->Falloff
;
1749 dst
[38 + l
* 8].x
= cosf(light
->Theta
* 0.5f
);
1750 dst
[38 + l
* 8].y
= cosf(light
->Phi
* 0.5f
);
1751 dst
[38 + l
* 8].z
= 1.0f
/ (dst
[38 + l
* 8].x
- dst
[38 + l
* 8].y
);
1752 dst
[39 + l
* 8].w
= (l
+ 1) == state
->ff
.num_lights_active
;
1757 nine_ff_load_point_and_fog_params(struct NineDevice9
*device
)
1759 const struct nine_state
*state
= &device
->state
;
1760 struct fvec4
*dst
= (struct fvec4
*)device
->ff
.vs_const
;
1762 if (!(state
->changed
.group
& NINE_STATE_FF_OTHER
))
1764 dst
[26].x
= asfloat(state
->rs
[D3DRS_POINTSIZE_MIN
]);
1765 dst
[26].y
= asfloat(state
->rs
[D3DRS_POINTSIZE_MAX
]);
1766 dst
[26].z
= asfloat(state
->rs
[D3DRS_POINTSIZE
]);
1767 dst
[26].w
= asfloat(state
->rs
[D3DRS_POINTSCALE_A
]);
1768 dst
[27].x
= asfloat(state
->rs
[D3DRS_POINTSCALE_B
]);
1769 dst
[27].y
= asfloat(state
->rs
[D3DRS_POINTSCALE_C
]);
1770 dst
[28].x
= asfloat(state
->rs
[D3DRS_FOGEND
]);
1771 dst
[28].y
= 1.0f
/ (asfloat(state
->rs
[D3DRS_FOGEND
]) - asfloat(state
->rs
[D3DRS_FOGSTART
]));
1772 if (isinf(dst
[28].y
))
1774 dst
[28].z
= asfloat(state
->rs
[D3DRS_FOGDENSITY
]);
1778 nine_ff_load_tex_matrices(struct NineDevice9
*device
)
1780 struct nine_state
*state
= &device
->state
;
1781 D3DMATRIX
*M
= (D3DMATRIX
*)device
->ff
.vs_const
;
1784 if (!(state
->ff
.changed
.transform
[0] & 0xff0000))
1786 for (s
= 0; s
< 8; ++s
) {
1787 if (IS_D3DTS_DIRTY(state
, TEXTURE0
+ s
))
1788 nine_d3d_matrix_transpose(&M
[32 + s
], nine_state_access_transform(state
, D3DTS_TEXTURE0
+ s
, FALSE
));
1793 nine_ff_load_ps_params(struct NineDevice9
*device
)
1795 const struct nine_state
*state
= &device
->state
;
1796 struct fvec4
*dst
= (struct fvec4
*)device
->ff
.ps_const
;
1799 if (!(state
->changed
.group
& (NINE_STATE_FF_PSSTAGES
| NINE_STATE_FF_OTHER
)))
1802 for (s
= 0; s
< 8; ++s
)
1803 d3dcolor_to_rgba(&dst
[s
].x
, state
->ff
.tex_stage
[s
][D3DTSS_CONSTANT
]);
1805 for (s
= 0; s
< 8; ++s
) {
1806 dst
[8 + s
].x
= asfloat(state
->ff
.tex_stage
[s
][D3DTSS_BUMPENVMAT00
]);
1807 dst
[8 + s
].y
= asfloat(state
->ff
.tex_stage
[s
][D3DTSS_BUMPENVMAT01
]);
1808 dst
[8 + s
].z
= asfloat(state
->ff
.tex_stage
[s
][D3DTSS_BUMPENVMAT10
]);
1809 dst
[8 + s
].w
= asfloat(state
->ff
.tex_stage
[s
][D3DTSS_BUMPENVMAT11
]);
1811 dst
[8 + s
/ 2].z
= asfloat(state
->ff
.tex_stage
[s
][D3DTSS_BUMPENVLSCALE
]);
1812 dst
[8 + s
/ 2].w
= asfloat(state
->ff
.tex_stage
[s
][D3DTSS_BUMPENVLOFFSET
]);
1814 dst
[8 + s
/ 2].x
= asfloat(state
->ff
.tex_stage
[s
][D3DTSS_BUMPENVLSCALE
]);
1815 dst
[8 + s
/ 2].y
= asfloat(state
->ff
.tex_stage
[s
][D3DTSS_BUMPENVLOFFSET
]);
1819 d3dcolor_to_rgba(&dst
[20].x
, state
->rs
[D3DRS_TEXTUREFACTOR
]);
1820 d3dcolor_to_rgba(&dst
[21].x
, state
->rs
[D3DRS_FOGCOLOR
]);
1821 dst
[22].x
= asfloat(state
->rs
[D3DRS_FOGEND
]);
1822 dst
[22].y
= 1.0f
/ (asfloat(state
->rs
[D3DRS_FOGEND
]) - asfloat(state
->rs
[D3DRS_FOGSTART
]));
1823 dst
[22].z
= asfloat(state
->rs
[D3DRS_FOGDENSITY
]);
1827 nine_ff_load_viewport_info(struct NineDevice9
*device
)
1829 D3DVIEWPORT9
*viewport
= &device
->state
.viewport
;
1830 struct fvec4
*dst
= (struct fvec4
*)device
->ff
.vs_const
;
1831 float diffZ
= viewport
->MaxZ
- viewport
->MinZ
;
1833 /* Note: the other functions avoids to fill the const again if nothing changed.
1834 * But we don't have much to fill, and adding code to allow that may be complex
1835 * so just fill it always */
1836 dst
[100].x
= 2.0f
/ (float)(viewport
->Width
);
1837 dst
[100].y
= 2.0f
/ (float)(viewport
->Height
);
1838 dst
[100].z
= (diffZ
== 0.0f
) ? 0.0f
: (1.0f
/ diffZ
);
1839 dst
[101].x
= (float)(viewport
->X
);
1840 dst
[101].y
= (float)(viewport
->Y
);
1841 dst
[101].z
= (float)(viewport
->MinZ
);
1845 nine_ff_update(struct NineDevice9
*device
)
1847 struct nine_state
*state
= &device
->state
;
1848 struct pipe_constant_buffer cb
;
1850 DBG("vs=%p ps=%p\n", device
->state
.vs
, device
->state
.ps
);
1852 /* NOTE: the only reference belongs to the hash table */
1853 if (!state
->programmable_vs
) {
1854 device
->ff
.vs
= nine_ff_get_vs(device
);
1855 device
->state
.changed
.group
|= NINE_STATE_VS
;
1857 if (!device
->state
.ps
) {
1858 device
->ff
.ps
= nine_ff_get_ps(device
);
1859 device
->state
.changed
.group
|= NINE_STATE_PS
;
1862 if (!state
->programmable_vs
) {
1863 nine_ff_load_vs_transforms(device
);
1864 nine_ff_load_tex_matrices(device
);
1865 nine_ff_load_lights(device
);
1866 nine_ff_load_point_and_fog_params(device
);
1867 nine_ff_load_viewport_info(device
);
1869 memset(state
->ff
.changed
.transform
, 0, sizeof(state
->ff
.changed
.transform
));
1871 cb
.buffer_offset
= 0;
1873 cb
.user_buffer
= device
->ff
.vs_const
;
1874 cb
.buffer_size
= NINE_FF_NUM_VS_CONST
* 4 * sizeof(float);
1876 if (!device
->driver_caps
.user_cbufs
) {
1877 u_upload_data(device
->constbuf_uploader
,
1880 device
->constbuf_alignment
,
1884 u_upload_unmap(device
->constbuf_uploader
);
1885 cb
.user_buffer
= NULL
;
1887 state
->pipe
.cb_vs_ff
= cb
;
1888 state
->commit
|= NINE_STATE_COMMIT_CONST_VS
;
1891 if (!device
->state
.ps
) {
1892 nine_ff_load_ps_params(device
);
1894 cb
.buffer_offset
= 0;
1896 cb
.user_buffer
= device
->ff
.ps_const
;
1897 cb
.buffer_size
= NINE_FF_NUM_PS_CONST
* 4 * sizeof(float);
1899 if (!device
->driver_caps
.user_cbufs
) {
1900 u_upload_data(device
->constbuf_uploader
,
1903 device
->constbuf_alignment
,
1907 u_upload_unmap(device
->constbuf_uploader
);
1908 cb
.user_buffer
= NULL
;
1910 state
->pipe
.cb_ps_ff
= cb
;
1911 state
->commit
|= NINE_STATE_COMMIT_CONST_PS
;
1914 device
->state
.changed
.group
&= ~NINE_STATE_FF
;
1919 nine_ff_init(struct NineDevice9
*device
)
1921 device
->ff
.ht_vs
= util_hash_table_create(nine_ff_vs_key_hash
,
1922 nine_ff_vs_key_comp
);
1923 device
->ff
.ht_ps
= util_hash_table_create(nine_ff_ps_key_hash
,
1924 nine_ff_ps_key_comp
);
1926 device
->ff
.ht_fvf
= util_hash_table_create(nine_ff_fvf_key_hash
,
1927 nine_ff_fvf_key_comp
);
1929 device
->ff
.vs_const
= CALLOC(NINE_FF_NUM_VS_CONST
, 4 * sizeof(float));
1930 device
->ff
.ps_const
= CALLOC(NINE_FF_NUM_PS_CONST
, 4 * sizeof(float));
1932 return device
->ff
.ht_vs
&& device
->ff
.ht_ps
&&
1933 device
->ff
.ht_fvf
&&
1934 device
->ff
.vs_const
&& device
->ff
.ps_const
;
1937 static enum pipe_error
nine_ff_ht_delete_cb(void *key
, void *value
, void *data
)
1939 NineUnknown_Unbind(NineUnknown(value
));
1944 nine_ff_fini(struct NineDevice9
*device
)
1946 if (device
->ff
.ht_vs
) {
1947 util_hash_table_foreach(device
->ff
.ht_vs
, nine_ff_ht_delete_cb
, NULL
);
1948 util_hash_table_destroy(device
->ff
.ht_vs
);
1950 if (device
->ff
.ht_ps
) {
1951 util_hash_table_foreach(device
->ff
.ht_ps
, nine_ff_ht_delete_cb
, NULL
);
1952 util_hash_table_destroy(device
->ff
.ht_ps
);
1954 if (device
->ff
.ht_fvf
) {
1955 util_hash_table_foreach(device
->ff
.ht_fvf
, nine_ff_ht_delete_cb
, NULL
);
1956 util_hash_table_destroy(device
->ff
.ht_fvf
);
1958 device
->ff
.vs
= NULL
; /* destroyed by unbinding from hash table */
1959 device
->ff
.ps
= NULL
;
1961 FREE(device
->ff
.vs_const
);
1962 FREE(device
->ff
.ps_const
);
1966 nine_ff_prune_vs(struct NineDevice9
*device
)
1968 if (device
->ff
.num_vs
> 100) {
1969 /* could destroy the bound one here, so unbind */
1970 device
->pipe
->bind_vs_state(device
->pipe
, NULL
);
1971 util_hash_table_foreach(device
->ff
.ht_vs
, nine_ff_ht_delete_cb
, NULL
);
1972 util_hash_table_clear(device
->ff
.ht_vs
);
1973 device
->ff
.num_vs
= 0;
1974 device
->state
.changed
.group
|= NINE_STATE_VS
;
1978 nine_ff_prune_ps(struct NineDevice9
*device
)
1980 if (device
->ff
.num_ps
> 100) {
1981 /* could destroy the bound one here, so unbind */
1982 device
->pipe
->bind_fs_state(device
->pipe
, NULL
);
1983 util_hash_table_foreach(device
->ff
.ht_ps
, nine_ff_ht_delete_cb
, NULL
);
1984 util_hash_table_clear(device
->ff
.ht_ps
);
1985 device
->ff
.num_ps
= 0;
1986 device
->state
.changed
.group
|= NINE_STATE_PS
;
1990 /* ========================================================================== */
1992 /* Matrix multiplication:
1994 * in memory: 0 1 2 3 (row major)
2000 * r0 = (r0 * cA) (r0 * cB) . .
2001 * r1 = (r1 * cA) (r1 * cB)
2005 * r: (11) (12) (13) (14)
2006 * (21) (22) (23) (24)
2007 * (31) (32) (33) (34)
2008 * (41) (42) (43) (44)
2016 * t.xyzw = MUL(v.xxxx, r[0]);
2017 * t.xyzw = MAD(v.yyyy, r[1], t.xyzw);
2018 * t.xyzw = MAD(v.zzzz, r[2], t.xyzw);
2019 * v.xyzw = MAD(v.wwww, r[3], t.xyzw);
2021 * v.x = DP4(v, c[0]);
2022 * v.y = DP4(v, c[1]);
2023 * v.z = DP4(v, c[2]);
2024 * v.w = DP4(v, c[3]) = 1
2029 nine_D3DMATRIX_print(const D3DMATRIX *M)
2031 DBG("\n(%f %f %f %f)\n"
2035 M->m[0][0], M->m[0][1], M->m[0][2], M->m[0][3],
2036 M->m[1][0], M->m[1][1], M->m[1][2], M->m[1][3],
2037 M->m[2][0], M->m[2][1], M->m[2][2], M->m[2][3],
2038 M->m[3][0], M->m[3][1], M->m[3][2], M->m[3][3]);
2043 nine_DP4_row_col(const D3DMATRIX
*A
, int r
, const D3DMATRIX
*B
, int c
)
2045 return A
->m
[r
][0] * B
->m
[0][c
] +
2046 A
->m
[r
][1] * B
->m
[1][c
] +
2047 A
->m
[r
][2] * B
->m
[2][c
] +
2048 A
->m
[r
][3] * B
->m
[3][c
];
2052 nine_DP4_vec_col(const D3DVECTOR
*v
, const D3DMATRIX
*M
, int c
)
2054 return v
->x
* M
->m
[0][c
] +
2061 nine_DP3_vec_col(const D3DVECTOR
*v
, const D3DMATRIX
*M
, int c
)
2063 return v
->x
* M
->m
[0][c
] +
2069 nine_d3d_matrix_matrix_mul(D3DMATRIX
*D
, const D3DMATRIX
*L
, const D3DMATRIX
*R
)
2071 D
->_11
= nine_DP4_row_col(L
, 0, R
, 0);
2072 D
->_12
= nine_DP4_row_col(L
, 0, R
, 1);
2073 D
->_13
= nine_DP4_row_col(L
, 0, R
, 2);
2074 D
->_14
= nine_DP4_row_col(L
, 0, R
, 3);
2076 D
->_21
= nine_DP4_row_col(L
, 1, R
, 0);
2077 D
->_22
= nine_DP4_row_col(L
, 1, R
, 1);
2078 D
->_23
= nine_DP4_row_col(L
, 1, R
, 2);
2079 D
->_24
= nine_DP4_row_col(L
, 1, R
, 3);
2081 D
->_31
= nine_DP4_row_col(L
, 2, R
, 0);
2082 D
->_32
= nine_DP4_row_col(L
, 2, R
, 1);
2083 D
->_33
= nine_DP4_row_col(L
, 2, R
, 2);
2084 D
->_34
= nine_DP4_row_col(L
, 2, R
, 3);
2086 D
->_41
= nine_DP4_row_col(L
, 3, R
, 0);
2087 D
->_42
= nine_DP4_row_col(L
, 3, R
, 1);
2088 D
->_43
= nine_DP4_row_col(L
, 3, R
, 2);
2089 D
->_44
= nine_DP4_row_col(L
, 3, R
, 3);
2093 nine_d3d_vector4_matrix_mul(D3DVECTOR
*d
, const D3DVECTOR
*v
, const D3DMATRIX
*M
)
2095 d
->x
= nine_DP4_vec_col(v
, M
, 0);
2096 d
->y
= nine_DP4_vec_col(v
, M
, 1);
2097 d
->z
= nine_DP4_vec_col(v
, M
, 2);
2101 nine_d3d_vector3_matrix_mul(D3DVECTOR
*d
, const D3DVECTOR
*v
, const D3DMATRIX
*M
)
2103 d
->x
= nine_DP3_vec_col(v
, M
, 0);
2104 d
->y
= nine_DP3_vec_col(v
, M
, 1);
2105 d
->z
= nine_DP3_vec_col(v
, M
, 2);
2109 nine_d3d_matrix_transpose(D3DMATRIX
*D
, const D3DMATRIX
*M
)
2112 for (i
= 0; i
< 4; ++i
)
2113 for (j
= 0; j
< 4; ++j
)
2114 D
->m
[i
][j
] = M
->m
[j
][i
];
2117 #define _M_ADD_PROD_1i_2j_3k_4l(i,j,k,l) do { \
2118 float t = M->_1##i * M->_2##j * M->_3##k * M->_4##l; \
2119 if (t > 0.0f) pos += t; else neg += t; } while(0)
2121 #define _M_SUB_PROD_1i_2j_3k_4l(i,j,k,l) do { \
2122 float t = M->_1##i * M->_2##j * M->_3##k * M->_4##l; \
2123 if (t > 0.0f) neg -= t; else pos -= t; } while(0)
2125 nine_d3d_matrix_det(const D3DMATRIX
*M
)
2130 _M_ADD_PROD_1i_2j_3k_4l(1, 2, 3, 4);
2131 _M_ADD_PROD_1i_2j_3k_4l(1, 3, 4, 2);
2132 _M_ADD_PROD_1i_2j_3k_4l(1, 4, 2, 3);
2134 _M_ADD_PROD_1i_2j_3k_4l(2, 1, 4, 3);
2135 _M_ADD_PROD_1i_2j_3k_4l(2, 3, 1, 4);
2136 _M_ADD_PROD_1i_2j_3k_4l(2, 4, 3, 1);
2138 _M_ADD_PROD_1i_2j_3k_4l(3, 1, 2, 4);
2139 _M_ADD_PROD_1i_2j_3k_4l(3, 2, 4, 1);
2140 _M_ADD_PROD_1i_2j_3k_4l(3, 4, 1, 2);
2142 _M_ADD_PROD_1i_2j_3k_4l(4, 1, 3, 2);
2143 _M_ADD_PROD_1i_2j_3k_4l(4, 2, 1, 3);
2144 _M_ADD_PROD_1i_2j_3k_4l(4, 3, 2, 1);
2146 _M_SUB_PROD_1i_2j_3k_4l(1, 2, 4, 3);
2147 _M_SUB_PROD_1i_2j_3k_4l(1, 3, 2, 4);
2148 _M_SUB_PROD_1i_2j_3k_4l(1, 4, 3, 2);
2150 _M_SUB_PROD_1i_2j_3k_4l(2, 1, 3, 4);
2151 _M_SUB_PROD_1i_2j_3k_4l(2, 3, 4, 1);
2152 _M_SUB_PROD_1i_2j_3k_4l(2, 4, 1, 3);
2154 _M_SUB_PROD_1i_2j_3k_4l(3, 1, 4, 2);
2155 _M_SUB_PROD_1i_2j_3k_4l(3, 2, 1, 4);
2156 _M_SUB_PROD_1i_2j_3k_4l(3, 4, 2, 1);
2158 _M_SUB_PROD_1i_2j_3k_4l(4, 1, 2, 3);
2159 _M_SUB_PROD_1i_2j_3k_4l(4, 2, 3, 1);
2160 _M_SUB_PROD_1i_2j_3k_4l(4, 3, 1, 2);
2165 /* XXX: Probably better to just use src/mesa/math/m_matrix.c because
2166 * I have no idea where this code came from.
2169 nine_d3d_matrix_inverse(D3DMATRIX
*D
, const D3DMATRIX
*M
)
2175 M
->m
[1][1] * M
->m
[2][2] * M
->m
[3][3] -
2176 M
->m
[1][1] * M
->m
[3][2] * M
->m
[2][3] -
2177 M
->m
[1][2] * M
->m
[2][1] * M
->m
[3][3] +
2178 M
->m
[1][2] * M
->m
[3][1] * M
->m
[2][3] +
2179 M
->m
[1][3] * M
->m
[2][1] * M
->m
[3][2] -
2180 M
->m
[1][3] * M
->m
[3][1] * M
->m
[2][2];
2183 -M
->m
[0][1] * M
->m
[2][2] * M
->m
[3][3] +
2184 M
->m
[0][1] * M
->m
[3][2] * M
->m
[2][3] +
2185 M
->m
[0][2] * M
->m
[2][1] * M
->m
[3][3] -
2186 M
->m
[0][2] * M
->m
[3][1] * M
->m
[2][3] -
2187 M
->m
[0][3] * M
->m
[2][1] * M
->m
[3][2] +
2188 M
->m
[0][3] * M
->m
[3][1] * M
->m
[2][2];
2191 M
->m
[0][1] * M
->m
[1][2] * M
->m
[3][3] -
2192 M
->m
[0][1] * M
->m
[3][2] * M
->m
[1][3] -
2193 M
->m
[0][2] * M
->m
[1][1] * M
->m
[3][3] +
2194 M
->m
[0][2] * M
->m
[3][1] * M
->m
[1][3] +
2195 M
->m
[0][3] * M
->m
[1][1] * M
->m
[3][2] -
2196 M
->m
[0][3] * M
->m
[3][1] * M
->m
[1][2];
2199 -M
->m
[0][1] * M
->m
[1][2] * M
->m
[2][3] +
2200 M
->m
[0][1] * M
->m
[2][2] * M
->m
[1][3] +
2201 M
->m
[0][2] * M
->m
[1][1] * M
->m
[2][3] -
2202 M
->m
[0][2] * M
->m
[2][1] * M
->m
[1][3] -
2203 M
->m
[0][3] * M
->m
[1][1] * M
->m
[2][2] +
2204 M
->m
[0][3] * M
->m
[2][1] * M
->m
[1][2];
2207 -M
->m
[1][0] * M
->m
[2][2] * M
->m
[3][3] +
2208 M
->m
[1][0] * M
->m
[3][2] * M
->m
[2][3] +
2209 M
->m
[1][2] * M
->m
[2][0] * M
->m
[3][3] -
2210 M
->m
[1][2] * M
->m
[3][0] * M
->m
[2][3] -
2211 M
->m
[1][3] * M
->m
[2][0] * M
->m
[3][2] +
2212 M
->m
[1][3] * M
->m
[3][0] * M
->m
[2][2];
2215 M
->m
[0][0] * M
->m
[2][2] * M
->m
[3][3] -
2216 M
->m
[0][0] * M
->m
[3][2] * M
->m
[2][3] -
2217 M
->m
[0][2] * M
->m
[2][0] * M
->m
[3][3] +
2218 M
->m
[0][2] * M
->m
[3][0] * M
->m
[2][3] +
2219 M
->m
[0][3] * M
->m
[2][0] * M
->m
[3][2] -
2220 M
->m
[0][3] * M
->m
[3][0] * M
->m
[2][2];
2223 -M
->m
[0][0] * M
->m
[1][2] * M
->m
[3][3] +
2224 M
->m
[0][0] * M
->m
[3][2] * M
->m
[1][3] +
2225 M
->m
[0][2] * M
->m
[1][0] * M
->m
[3][3] -
2226 M
->m
[0][2] * M
->m
[3][0] * M
->m
[1][3] -
2227 M
->m
[0][3] * M
->m
[1][0] * M
->m
[3][2] +
2228 M
->m
[0][3] * M
->m
[3][0] * M
->m
[1][2];
2231 M
->m
[0][0] * M
->m
[1][2] * M
->m
[2][3] -
2232 M
->m
[0][0] * M
->m
[2][2] * M
->m
[1][3] -
2233 M
->m
[0][2] * M
->m
[1][0] * M
->m
[2][3] +
2234 M
->m
[0][2] * M
->m
[2][0] * M
->m
[1][3] +
2235 M
->m
[0][3] * M
->m
[1][0] * M
->m
[2][2] -
2236 M
->m
[0][3] * M
->m
[2][0] * M
->m
[1][2];
2239 M
->m
[1][0] * M
->m
[2][1] * M
->m
[3][3] -
2240 M
->m
[1][0] * M
->m
[3][1] * M
->m
[2][3] -
2241 M
->m
[1][1] * M
->m
[2][0] * M
->m
[3][3] +
2242 M
->m
[1][1] * M
->m
[3][0] * M
->m
[2][3] +
2243 M
->m
[1][3] * M
->m
[2][0] * M
->m
[3][1] -
2244 M
->m
[1][3] * M
->m
[3][0] * M
->m
[2][1];
2247 -M
->m
[0][0] * M
->m
[2][1] * M
->m
[3][3] +
2248 M
->m
[0][0] * M
->m
[3][1] * M
->m
[2][3] +
2249 M
->m
[0][1] * M
->m
[2][0] * M
->m
[3][3] -
2250 M
->m
[0][1] * M
->m
[3][0] * M
->m
[2][3] -
2251 M
->m
[0][3] * M
->m
[2][0] * M
->m
[3][1] +
2252 M
->m
[0][3] * M
->m
[3][0] * M
->m
[2][1];
2255 M
->m
[0][0] * M
->m
[1][1] * M
->m
[3][3] -
2256 M
->m
[0][0] * M
->m
[3][1] * M
->m
[1][3] -
2257 M
->m
[0][1] * M
->m
[1][0] * M
->m
[3][3] +
2258 M
->m
[0][1] * M
->m
[3][0] * M
->m
[1][3] +
2259 M
->m
[0][3] * M
->m
[1][0] * M
->m
[3][1] -
2260 M
->m
[0][3] * M
->m
[3][0] * M
->m
[1][1];
2263 -M
->m
[0][0] * M
->m
[1][1] * M
->m
[2][3] +
2264 M
->m
[0][0] * M
->m
[2][1] * M
->m
[1][3] +
2265 M
->m
[0][1] * M
->m
[1][0] * M
->m
[2][3] -
2266 M
->m
[0][1] * M
->m
[2][0] * M
->m
[1][3] -
2267 M
->m
[0][3] * M
->m
[1][0] * M
->m
[2][1] +
2268 M
->m
[0][3] * M
->m
[2][0] * M
->m
[1][1];
2271 -M
->m
[1][0] * M
->m
[2][1] * M
->m
[3][2] +
2272 M
->m
[1][0] * M
->m
[3][1] * M
->m
[2][2] +
2273 M
->m
[1][1] * M
->m
[2][0] * M
->m
[3][2] -
2274 M
->m
[1][1] * M
->m
[3][0] * M
->m
[2][2] -
2275 M
->m
[1][2] * M
->m
[2][0] * M
->m
[3][1] +
2276 M
->m
[1][2] * M
->m
[3][0] * M
->m
[2][1];
2279 M
->m
[0][0] * M
->m
[2][1] * M
->m
[3][2] -
2280 M
->m
[0][0] * M
->m
[3][1] * M
->m
[2][2] -
2281 M
->m
[0][1] * M
->m
[2][0] * M
->m
[3][2] +
2282 M
->m
[0][1] * M
->m
[3][0] * M
->m
[2][2] +
2283 M
->m
[0][2] * M
->m
[2][0] * M
->m
[3][1] -
2284 M
->m
[0][2] * M
->m
[3][0] * M
->m
[2][1];
2287 -M
->m
[0][0] * M
->m
[1][1] * M
->m
[3][2] +
2288 M
->m
[0][0] * M
->m
[3][1] * M
->m
[1][2] +
2289 M
->m
[0][1] * M
->m
[1][0] * M
->m
[3][2] -
2290 M
->m
[0][1] * M
->m
[3][0] * M
->m
[1][2] -
2291 M
->m
[0][2] * M
->m
[1][0] * M
->m
[3][1] +
2292 M
->m
[0][2] * M
->m
[3][0] * M
->m
[1][1];
2295 M
->m
[0][0] * M
->m
[1][1] * M
->m
[2][2] -
2296 M
->m
[0][0] * M
->m
[2][1] * M
->m
[1][2] -
2297 M
->m
[0][1] * M
->m
[1][0] * M
->m
[2][2] +
2298 M
->m
[0][1] * M
->m
[2][0] * M
->m
[1][2] +
2299 M
->m
[0][2] * M
->m
[1][0] * M
->m
[2][1] -
2300 M
->m
[0][2] * M
->m
[2][0] * M
->m
[1][1];
2303 M
->m
[0][0] * D
->m
[0][0] +
2304 M
->m
[1][0] * D
->m
[0][1] +
2305 M
->m
[2][0] * D
->m
[0][2] +
2306 M
->m
[3][0] * D
->m
[0][3];
2310 for (i
= 0; i
< 4; i
++)
2311 for (k
= 0; k
< 4; k
++)
2318 nine_d3d_matrix_matrix_mul(&I
, D
, M
);
2320 for (i
= 0; i
< 4; ++i
)
2321 for (k
= 0; k
< 4; ++k
)
2322 if (fabsf(I
.m
[i
][k
] - (float)(i
== k
)) > 1e-3)
2323 DBG("Matrix inversion check FAILED !\n");
2328 /* TODO: don't use 4x4 inverse, unless this gets all nicely inlined ? */
2330 nine_d3d_matrix_inverse_3x3(D3DMATRIX
*D
, const D3DMATRIX
*M
)
2335 for (i
= 0; i
< 3; ++i
)
2336 for (j
= 0; j
< 3; ++j
)
2337 T
.m
[i
][j
] = M
->m
[i
][j
];
2338 for (i
= 0; i
< 3; ++i
) {
2344 nine_d3d_matrix_inverse(D
, &T
);