1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "pipe/p_config.h"
32 #include "pipe/p_util.h"
33 #include "pipe/p_shader_tokens.h"
34 #include "tgsi/tgsi_parse.h"
35 #include "tgsi/tgsi_util.h"
36 #include "tgsi/tgsi_exec.h"
38 #include "draw_vs_aos.h"
39 #include "draw_vertex.h"
43 #include "rtasm/rtasm_x86sse.h"
46 #define X87_CW_EXCEPTION_INV_OP (1<<0)
47 #define X87_CW_EXCEPTION_DENORM_OP (1<<1)
48 #define X87_CW_EXCEPTION_ZERO_DIVIDE (1<<2)
49 #define X87_CW_EXCEPTION_OVERFLOW (1<<3)
50 #define X87_CW_EXCEPTION_UNDERFLOW (1<<4)
51 #define X87_CW_EXCEPTION_PRECISION (1<<5)
52 #define X87_CW_PRECISION_SINGLE (0<<8)
53 #define X87_CW_PRECISION_RESERVED (1<<8)
54 #define X87_CW_PRECISION_DOUBLE (2<<8)
55 #define X87_CW_PRECISION_DOUBLE_EXT (3<<8)
56 #define X87_CW_PRECISION_MASK (3<<8)
57 #define X87_CW_ROUND_NEAREST (0<<10)
58 #define X87_CW_ROUND_DOWN (1<<10)
59 #define X87_CW_ROUND_UP (2<<10)
60 #define X87_CW_ROUND_ZERO (3<<10)
61 #define X87_CW_ROUND_MASK (3<<10)
62 #define X87_CW_INFINITY (1<<12)
65 void PIPE_CDECL
aos_do_lit( struct aos_machine
*machine
,
81 const float epsilon
= 1.0F
/ 256.0F
;
82 float exponent
= CLAMP(in
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
85 result
[2] = powf(in
[1], exponent
);
99 static void PIPE_CDECL
do_lit_lut( struct aos_machine
*machine
,
115 if (machine
->lit_info
[count
].shine_tab
->exponent
!= in
[3]) {
116 machine
->lit_info
[count
].func
= aos_do_lit
;
122 const float *tab
= machine
->lit_info
[count
].shine_tab
->values
;
123 float f
= in
[1] * 256;
125 float frac
= f
- (float)k
;
129 result
[2] = tab
[k
] + frac
*(tab
[k
+1]-tab
[k
]);
136 const float epsilon
= 1.0F
/ 256.0F
;
137 float exponent
= CLAMP(in
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
140 result
[2] = powf(in
[1], exponent
);
154 static void do_populate_lut( struct shine_tab
*tab
,
155 float unclamped_exponent
)
157 const float epsilon
= 1.0F
/ 256.0F
;
158 float exponent
= CLAMP(unclamped_exponent
, -(128.0F
- epsilon
), (128.0F
- epsilon
));
161 tab
->exponent
= unclamped_exponent
; /* for later comparison */
165 for (i
= 1; i
< 258; i
++) {
166 tab
->values
[i
] = 1.0;
170 for (i
= 1; i
< 258; i
++) {
171 tab
->values
[i
] = powf((float)i
* epsilon
, exponent
);
179 static void PIPE_CDECL
populate_lut( struct aos_machine
*machine
,
186 /* Search for an existing table for this value. Note that without
187 * static analysis we don't really know if in[3] will be constant,
188 * but it usually is...
190 for (tab
= 0; tab
< 4; tab
++) {
191 if (machine
->shine_tab
[tab
].exponent
== in
[3]) {
196 for (tab
= 0, i
= 1; i
< 4; i
++) {
197 if (machine
->shine_tab
[i
].last_used
< machine
->shine_tab
[tab
].last_used
)
201 if (machine
->shine_tab
[tab
].last_used
== machine
->now
) {
202 /* No unused tables (this is not a ffvertex program...). Just
203 * call pow each time:
205 machine
->lit_info
[count
].func
= aos_do_lit
;
206 machine
->lit_info
[count
].func( machine
, result
, in
, count
);
210 do_populate_lut( &machine
->shine_tab
[tab
], in
[3] );
214 machine
->shine_tab
[tab
].last_used
= machine
->now
;
215 machine
->lit_info
[count
].shine_tab
= &machine
->shine_tab
[tab
];
216 machine
->lit_info
[count
].func
= do_lit_lut
;
217 machine
->lit_info
[count
].func( machine
, result
, in
, count
);
221 void draw_vs_aos_machine_constants( struct aos_machine
*machine
,
222 const float (*constants
)[4] )
224 machine
->constants
= constants
;
228 for (i
= 0; i
< MAX_LIT_INFO
; i
++) {
229 machine
->lit_info
[i
].func
= populate_lut
;
236 void draw_vs_aos_machine_viewport( struct aos_machine
*machine
,
237 const struct pipe_viewport_state
*viewport
)
239 memcpy(machine
->scale
, viewport
->scale
, 4 * sizeof(float));
240 memcpy(machine
->translate
, viewport
->translate
, 4 * sizeof(float));
245 void draw_vs_aos_machine_destroy( struct aos_machine
*machine
)
250 struct aos_machine
*draw_vs_aos_machine( void )
252 struct aos_machine
*machine
;
254 float inv
= 1.0f
/255.0f
;
257 machine
= align_malloc(sizeof(struct aos_machine
), 16);
261 memset(machine
, 0, sizeof(*machine
));
263 ASSIGN_4V(machine
->internal
[IMM_SWZ
], 1.0f
, -1.0f
, 0.0f
, 1.0f
);
264 *(unsigned *)&machine
->internal
[IMM_SWZ
][3] = 0xffffffff;
266 ASSIGN_4V(machine
->internal
[IMM_ONES
], 1.0f
, 1.0f
, 1.0f
, 1.0f
);
267 ASSIGN_4V(machine
->internal
[IMM_NEGS
], -1.0f
, -1.0f
, -1.0f
, -1.0f
);
268 ASSIGN_4V(machine
->internal
[IMM_IDENTITY
], 0.0f
, 0.0f
, 0.0f
, 1.0f
);
269 ASSIGN_4V(machine
->internal
[IMM_INV_255
], inv
, inv
, inv
, inv
);
270 ASSIGN_4V(machine
->internal
[IMM_255
], f255
, f255
, f255
, f255
);
271 ASSIGN_4V(machine
->internal
[IMM_RSQ
], -.5f
, 1.5f
, 0.0f
, 0.0f
);
274 machine
->fpu_rnd_nearest
= (X87_CW_EXCEPTION_INV_OP
|
275 X87_CW_EXCEPTION_DENORM_OP
|
276 X87_CW_EXCEPTION_ZERO_DIVIDE
|
277 X87_CW_EXCEPTION_OVERFLOW
|
278 X87_CW_EXCEPTION_UNDERFLOW
|
279 X87_CW_EXCEPTION_PRECISION
|
281 X87_CW_ROUND_NEAREST
|
282 X87_CW_PRECISION_DOUBLE_EXT
);
284 assert(machine
->fpu_rnd_nearest
== 0x37f);
286 machine
->fpu_rnd_neg_inf
= (X87_CW_EXCEPTION_INV_OP
|
287 X87_CW_EXCEPTION_DENORM_OP
|
288 X87_CW_EXCEPTION_ZERO_DIVIDE
|
289 X87_CW_EXCEPTION_OVERFLOW
|
290 X87_CW_EXCEPTION_UNDERFLOW
|
291 X87_CW_EXCEPTION_PRECISION
|
294 X87_CW_PRECISION_DOUBLE_EXT
);
296 for (i
= 0; i
< MAX_SHINE_TAB
; i
++)
297 do_populate_lut( &machine
->shine_tab
[i
], 1.0f
);
304 void draw_vs_aos_machine_viewport( struct aos_machine
*machine
,
305 const struct pipe_viewport_state
*viewport
)
309 void draw_vs_aos_machine_constants( struct aos_machine
*machine
,
310 const float (*constants
)[4] )
314 void draw_vs_aos_machine_destroy( struct aos_machine
*machine
)
318 struct aos_machine
*draw_vs_aos_machine( void )