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 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "pipe/p_config.h"
38 #if defined(PIPE_ARCH_X86)
40 #include "pipe/p_util.h"
41 #include "pipe/p_shader_tokens.h"
43 #include "draw_private.h"
44 #include "draw_context.h"
46 #include "rtasm/rtasm_cpu.h"
47 #include "rtasm/rtasm_x86sse.h"
48 #include "tgsi/exec/tgsi_sse2.h"
49 #include "tgsi/util/tgsi_parse.h"
51 #define SSE_MAX_VERTICES 4
52 #define SSE_SWIZZLES 1
55 typedef void (XSTDCALL
*codegen_function
) (
56 const struct tgsi_exec_vector
*input
, /* 1 */
57 struct tgsi_exec_vector
*output
, /* 2 */
58 float (*constant
)[4], /* 3 */
59 struct tgsi_exec_vector
*temporary
, /* 4 */
60 float (*immediates
)[4], /* 5 */
61 const float (*aos_input
)[4], /* 6 */
62 uint num_inputs
, /* 7 */
63 uint input_stride
, /* 8 */
64 float (*aos_output
)[4], /* 9 */
65 uint num_outputs
, /* 10 */
66 uint output_stride
); /* 11 */
68 typedef void (XSTDCALL
*codegen_function
) (
69 const struct tgsi_exec_vector
*input
,
70 struct tgsi_exec_vector
*output
,
72 struct tgsi_exec_vector
*temporary
,
73 float (*immediates
)[4] );
76 struct draw_sse_vertex_shader
{
77 struct draw_vertex_shader base
;
78 struct x86_function sse2_program
;
80 codegen_function func
;
82 struct tgsi_exec_machine
*machine
;
84 float immediates
[TGSI_EXEC_NUM_IMMEDIATES
][4];
89 vs_sse_prepare( struct draw_vertex_shader
*base
,
90 struct draw_context
*draw
)
96 /* Simplified vertex shader interface for the pt paths. Given the
97 * complexity of code-generating all the above operations together,
98 * it's time to try doing all the other stuff separately.
101 vs_sse_run_linear( struct draw_vertex_shader
*base
,
102 const float (*input
)[4],
104 const float (*constants
)[4],
106 unsigned input_stride
,
107 unsigned output_stride
)
109 struct draw_sse_vertex_shader
*shader
= (struct draw_sse_vertex_shader
*)base
;
110 struct tgsi_exec_machine
*machine
= shader
->machine
;
113 for (i
= 0; i
< count
; i
+= MAX_TGSI_VERTICES
) {
114 unsigned int max_vertices
= MIN2(MAX_TGSI_VERTICES
, count
- i
);
117 /* run compiled shader
119 shader
->func(machine
->Inputs
,
121 (float (*)[4])constants
,
125 base
->info
.num_inputs
,
128 base
->info
.num_outputs
,
131 input
= (const float (*)[4])((const char *)input
+ input_stride
* max_vertices
);
132 output
= (float (*)[4])((char *)output
+ output_stride
* max_vertices
);
134 unsigned int j
, slot
;
138 for (j
= 0; j
< max_vertices
; j
++) {
139 for (slot
= 0; slot
< base
->info
.num_inputs
; slot
++) {
140 machine
->Inputs
[slot
].xyzw
[0].f
[j
] = input
[slot
][0];
141 machine
->Inputs
[slot
].xyzw
[1].f
[j
] = input
[slot
][1];
142 machine
->Inputs
[slot
].xyzw
[2].f
[j
] = input
[slot
][2];
143 machine
->Inputs
[slot
].xyzw
[3].f
[j
] = input
[slot
][3];
146 input
= (const float (*)[4])((const char *)input
+ input_stride
);
149 /* run compiled shader
151 shader
->func(machine
->Inputs
,
153 (float (*)[4])constants
,
157 /* Unswizzle all output results.
159 for (j
= 0; j
< max_vertices
; j
++) {
160 for (slot
= 0; slot
< base
->info
.num_outputs
; slot
++) {
161 output
[slot
][0] = machine
->Outputs
[slot
].xyzw
[0].f
[j
];
162 output
[slot
][1] = machine
->Outputs
[slot
].xyzw
[1].f
[j
];
163 output
[slot
][2] = machine
->Outputs
[slot
].xyzw
[2].f
[j
];
164 output
[slot
][3] = machine
->Outputs
[slot
].xyzw
[3].f
[j
];
167 output
= (float (*)[4])((char *)output
+ output_stride
);
177 vs_sse_delete( struct draw_vertex_shader
*base
)
179 struct draw_sse_vertex_shader
*shader
= (struct draw_sse_vertex_shader
*)base
;
181 x86_release_func( &shader
->sse2_program
);
183 FREE( (void*) shader
->base
.state
.tokens
);
188 struct draw_vertex_shader
*
189 draw_create_vs_sse(struct draw_context
*draw
,
190 const struct pipe_shader_state
*templ
)
192 struct draw_sse_vertex_shader
*vs
;
194 if (!rtasm_cpu_has_sse2())
197 vs
= CALLOC_STRUCT( draw_sse_vertex_shader
);
201 /* we make a private copy of the tokens */
202 vs
->base
.state
.tokens
= tgsi_dup_tokens(templ
->tokens
);
203 if (!vs
->base
.state
.tokens
)
206 tgsi_scan_shader(templ
->tokens
, &vs
->base
.info
);
208 vs
->base
.prepare
= vs_sse_prepare
;
209 vs
->base
.run_linear
= vs_sse_run_linear
;
210 vs
->base
.delete = vs_sse_delete
;
211 vs
->machine
= &draw
->machine
;
213 x86_init_func( &vs
->sse2_program
);
215 if (!tgsi_emit_sse2( (struct tgsi_token
*) vs
->base
.state
.tokens
,
216 &vs
->sse2_program
, vs
->immediates
, SSE_SWIZZLES
))
219 vs
->func
= (codegen_function
) x86_get_func( &vs
->sse2_program
);
227 fprintf(stderr
, "tgsi_emit_sse2() failed, falling back to interpreter\n");
229 x86_release_func( &vs
->sse2_program
);
239 struct draw_vertex_shader
*
240 draw_create_vs_sse( struct draw_context
*draw
,
241 const struct pipe_shader_state
*templ
)