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>
36 #if defined(__i386__) || defined(__386__)
38 #include "pipe/p_util.h"
39 #include "pipe/p_shader_tokens.h"
41 #include "draw_private.h"
42 #include "draw_context.h"
44 #include "rtasm/rtasm_x86sse.h"
45 #include "tgsi/exec/tgsi_sse2.h"
46 #include "tgsi/util/tgsi_parse.h"
48 #define SSE_MAX_VERTICES 4
50 typedef void (XSTDCALL
*codegen_function
) (
51 const struct tgsi_exec_vector
*input
,
52 struct tgsi_exec_vector
*output
,
54 struct tgsi_exec_vector
*temporary
,
55 float (*immediates
)[4] );
58 struct draw_sse_vertex_shader
{
59 struct draw_vertex_shader base
;
60 struct x86_function sse2_program
;
61 codegen_function func
;
62 float immediates
[TGSI_EXEC_NUM_IMMEDIATES
][4];
67 vs_sse_prepare( struct draw_vertex_shader
*base
,
68 struct draw_context
*draw
)
70 draw_update_vertex_fetch( draw
);
74 * Transform vertices with the current vertex program/shader
75 * Up to four vertices can be shaded at a time.
76 * \param vbuffer the input vertex data
77 * \param elts indexes of four input vertices
78 * \param count number of vertices to shade [1..4]
79 * \param vOut array of pointers to four output vertices
82 vs_sse_run( struct draw_vertex_shader
*base
,
83 struct draw_context
*draw
,
88 struct draw_sse_vertex_shader
*shader
= (struct draw_sse_vertex_shader
*)base
;
89 struct tgsi_exec_machine
*machine
= &draw
->machine
;
91 unsigned int clipped
= 0;
93 ALIGN16_DECL(struct tgsi_exec_vector
, inputs
, PIPE_MAX_ATTRIBS
);
94 ALIGN16_DECL(struct tgsi_exec_vector
, outputs
, PIPE_MAX_ATTRIBS
);
95 const float *scale
= draw
->viewport
.scale
;
96 const float *trans
= draw
->viewport
.translate
;
98 assert(draw
->vertex_shader
->info
.output_semantic_name
[0]
99 == TGSI_SEMANTIC_POSITION
);
101 /* Consts does not require 16 byte alignment. */
102 machine
->Consts
= (float (*)[4]) draw
->user
.constants
;
103 machine
->Inputs
= ALIGN16_ASSIGN(inputs
);
104 if (draw
->rasterizer
->bypass_vs
) {
105 /* outputs are just the inputs */
106 machine
->Outputs
= machine
->Inputs
;
109 machine
->Outputs
= ALIGN16_ASSIGN(outputs
);
112 for (i
= 0; i
< count
; i
+= SSE_MAX_VERTICES
) {
113 unsigned int max_vertices
= MIN2(SSE_MAX_VERTICES
, count
- i
);
114 /* Fetch vertices. This may at some point be integrated into the
115 * compiled shader -- that would require a reorganization where
116 * multiple versions of the compiled shader might exist,
117 * specialized for each fetch state.
119 draw
->vertex_fetch
.fetch_func(draw
, machine
, &elts
[i
], max_vertices
);
121 if (!draw
->rasterizer
->bypass_vs
) {
122 /* run compiled shader
124 shader
->func(machine
->Inputs
,
131 /* XXX: Computing the clipmask and emitting results should be done
132 * in the vertex program as a set of instructions appended to
133 * the user-provided code.
135 for (j
= 0; j
< max_vertices
; j
++) {
138 struct vertex_header
*out
=
139 draw_header_from_block(vOut
, i
+ j
);
141 x
= out
->clip
[0] = machine
->Outputs
[0].xyzw
[0].f
[j
];
142 y
= out
->clip
[1] = machine
->Outputs
[0].xyzw
[1].f
[j
];
143 z
= out
->clip
[2] = machine
->Outputs
[0].xyzw
[2].f
[j
];
144 w
= out
->clip
[3] = machine
->Outputs
[0].xyzw
[3].f
[j
];
146 if (!draw
->rasterizer
->bypass_clipping
) {
147 out
->clipmask
= compute_clipmask(out
->clip
, draw
->plane
,
149 clipped
+= out
->clipmask
;
161 out
->vertex_id
= UNDEFINED_VERTEX_ID
;
163 if (!draw
->identity_viewport
) {
164 /* Viewport mapping */
165 out
->data
[0][0] = x
* scale
[0] + trans
[0];
166 out
->data
[0][1] = y
* scale
[1] + trans
[1];
167 out
->data
[0][2] = z
* scale
[2] + trans
[2];
177 /* Remaining attributes are packed into sequential post-transform
178 * vertex attrib slots.
180 for (slot
= 1; slot
< draw
->num_vs_outputs
; slot
++) {
181 out
->data
[slot
][0] = machine
->Outputs
[slot
].xyzw
[0].f
[j
];
182 out
->data
[slot
][1] = machine
->Outputs
[slot
].xyzw
[1].f
[j
];
183 out
->data
[slot
][2] = machine
->Outputs
[slot
].xyzw
[2].f
[j
];
184 out
->data
[slot
][3] = machine
->Outputs
[slot
].xyzw
[3].f
[j
];
187 printf("%d) Post xform vert:\n", i
+ j
);
188 for (slot
= 0; slot
< draw
->num_vs_outputs
; slot
++) {
189 printf("\t%d: %f %f %f %f\n", slot
,
204 vs_sse_delete( struct draw_vertex_shader
*base
)
206 struct draw_sse_vertex_shader
*shader
= (struct draw_sse_vertex_shader
*)base
;
208 x86_release_func( &shader
->sse2_program
);
210 FREE( (void*) shader
->base
.state
.tokens
);
215 struct draw_vertex_shader
*
216 draw_create_vs_sse(struct draw_context
*draw
,
217 const struct pipe_shader_state
*templ
)
219 struct draw_sse_vertex_shader
*vs
;
220 uint nt
= tgsi_num_tokens(templ
->tokens
);
225 vs
= CALLOC_STRUCT( draw_sse_vertex_shader
);
229 /* we make a private copy of the tokens */
230 vs
->base
.state
.tokens
= mem_dup(templ
->tokens
, nt
* sizeof(templ
->tokens
[0]));
231 vs
->base
.prepare
= vs_sse_prepare
;
232 vs
->base
.run
= vs_sse_run
;
233 vs
->base
.delete = vs_sse_delete
;
235 x86_init_func( &vs
->sse2_program
);
237 if (!tgsi_emit_sse2( (struct tgsi_token
*) vs
->base
.state
.tokens
,
238 &vs
->sse2_program
, vs
->immediates
))
241 vs
->func
= (codegen_function
) x86_get_func( &vs
->sse2_program
);
246 fprintf(stderr
, "tgsi_emit_sse2() failed, falling back to interpreter\n");
248 x86_release_func( &vs
->sse2_program
);
258 struct draw_vertex_shader
*
259 draw_create_vs_sse( struct draw_context
*draw
,
260 const struct pipe_shader_state
*templ
)