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"
48 typedef void (XSTDCALL
*codegen_function
) (
49 const struct tgsi_exec_vector
*input
,
50 struct tgsi_exec_vector
*output
,
52 struct tgsi_exec_vector
*temporary
);
55 struct draw_sse_vertex_shader
{
56 struct draw_vertex_shader base
;
57 struct x86_function sse2_program
;
58 codegen_function func
;
62 /* Should be part of the generated shader:
64 static INLINE
unsigned
65 compute_clipmask(const float *clip
, /*const*/ float plane
[][4], unsigned nr
)
70 /* Do the hardwired planes first:
72 if (-clip
[0] + clip
[3] < 0) mask
|= CLIP_RIGHT_BIT
;
73 if ( clip
[0] + clip
[3] < 0) mask
|= CLIP_LEFT_BIT
;
74 if (-clip
[1] + clip
[3] < 0) mask
|= CLIP_TOP_BIT
;
75 if ( clip
[1] + clip
[3] < 0) mask
|= CLIP_BOTTOM_BIT
;
76 if (-clip
[2] + clip
[3] < 0) mask
|= CLIP_FAR_BIT
;
77 if ( clip
[2] + clip
[3] < 0) mask
|= CLIP_NEAR_BIT
;
79 /* Followed by any remaining ones:
81 for (i
= 6; i
< nr
; i
++) {
82 if (dot4(clip
, plane
[i
]) < 0)
91 vs_sse_prepare( struct draw_vertex_shader
*base
,
92 struct draw_context
*draw
)
94 draw_update_vertex_fetch( draw
);
98 * Transform vertices with the current vertex program/shader
99 * Up to four vertices can be shaded at a time.
100 * \param vbuffer the input vertex data
101 * \param elts indexes of four input vertices
102 * \param count number of vertices to shade [1..4]
103 * \param vOut array of pointers to four output vertices
106 vs_sse_run( struct draw_vertex_shader
*base
,
107 struct draw_context
*draw
,
108 const unsigned *elts
,
110 struct vertex_header
*vOut
[] )
112 struct draw_sse_vertex_shader
*shader
= (struct draw_sse_vertex_shader
*)base
;
113 struct tgsi_exec_machine
*machine
= &draw
->machine
;
116 ALIGN16_DECL(struct tgsi_exec_vector
, inputs
, PIPE_ATTRIB_MAX
);
117 ALIGN16_DECL(struct tgsi_exec_vector
, outputs
, PIPE_ATTRIB_MAX
);
118 const float *scale
= draw
->viewport
.scale
;
119 const float *trans
= draw
->viewport
.translate
;
122 assert(draw
->vertex_shader
->info
.output_semantic_name
[0]
123 == TGSI_SEMANTIC_POSITION
);
125 /* Consts does not require 16 byte alignment. */
126 machine
->Consts
= (float (*)[4]) draw
->user
.constants
;
127 machine
->Inputs
= ALIGN16_ASSIGN(inputs
);
128 machine
->Outputs
= ALIGN16_ASSIGN(outputs
);
131 /* Fetch vertices. This may at some point be integrated into the
132 * compiled shader -- that would require a reorganization where
133 * multiple versions of the compiled shader might exist,
134 * specialized for each fetch state.
136 draw
->vertex_fetch
.fetch_func( draw
, machine
, elts
, count
);
139 /* run compiled shader
148 /* XXX: Computing the clipmask and emitting results should be done
149 * in the vertex program as a set of instructions appended to
150 * the user-provided code.
152 for (j
= 0; j
< count
; j
++) {
156 x
= vOut
[j
]->clip
[0] = machine
->Outputs
[0].xyzw
[0].f
[j
];
157 y
= vOut
[j
]->clip
[1] = machine
->Outputs
[0].xyzw
[1].f
[j
];
158 z
= vOut
[j
]->clip
[2] = machine
->Outputs
[0].xyzw
[2].f
[j
];
159 w
= vOut
[j
]->clip
[3] = machine
->Outputs
[0].xyzw
[3].f
[j
];
161 if (!draw
->rasterizer
->bypass_clipping
) {
162 vOut
[j
]->clipmask
= compute_clipmask(vOut
[j
]->clip
, draw
->plane
, draw
->nr_planes
);
163 vOut
[j
]->edgeflag
= 1;
171 /* Viewport mapping */
172 vOut
[j
]->data
[0][0] = x
* scale
[0] + trans
[0];
173 vOut
[j
]->data
[0][1] = y
* scale
[1] + trans
[1];
174 vOut
[j
]->data
[0][2] = z
* scale
[2] + trans
[2];
175 vOut
[j
]->data
[0][3] = w
;
178 vOut
[j
]->clipmask
= 0;
179 vOut
[j
]->edgeflag
= 1;
180 vOut
[j
]->data
[0][0] = x
;
181 vOut
[j
]->data
[0][1] = y
;
182 vOut
[j
]->data
[0][2] = z
;
183 vOut
[j
]->data
[0][3] = w
;
186 /* Remaining attributes are packed into sequential post-transform
187 * vertex attrib slots.
189 for (slot
= 1; slot
< draw
->num_vs_outputs
; slot
++) {
190 vOut
[j
]->data
[slot
][0] = machine
->Outputs
[slot
].xyzw
[0].f
[j
];
191 vOut
[j
]->data
[slot
][1] = machine
->Outputs
[slot
].xyzw
[1].f
[j
];
192 vOut
[j
]->data
[slot
][2] = machine
->Outputs
[slot
].xyzw
[2].f
[j
];
193 vOut
[j
]->data
[slot
][3] = machine
->Outputs
[slot
].xyzw
[3].f
[j
];
201 vs_sse_delete( struct draw_vertex_shader
*base
)
203 struct draw_sse_vertex_shader
*shader
= (struct draw_sse_vertex_shader
*)base
;
205 x86_release_func( &shader
->sse2_program
);
211 struct draw_vertex_shader
*
212 draw_create_vs_sse(struct draw_context
*draw
,
213 const struct pipe_shader_state
*templ
)
215 struct draw_sse_vertex_shader
*vs
;
220 vs
= CALLOC_STRUCT( draw_sse_vertex_shader
);
224 vs
->base
.state
= templ
;
225 vs
->base
.prepare
= vs_sse_prepare
;
226 vs
->base
.run
= vs_sse_run
;
227 vs
->base
.delete = vs_sse_delete
;
229 x86_init_func( &vs
->sse2_program
);
231 if (!tgsi_emit_sse2( (struct tgsi_token
*) vs
->base
.state
->tokens
,
235 vs
->func
= (codegen_function
) x86_get_func( &vs
->sse2_program
);
240 fprintf(stderr
, "tgsi_emit_sse2() failed, falling back to interpreter\n");
242 x86_release_func( &vs
->sse2_program
);
252 struct draw_vertex_shader
*
253 draw_create_vs_sse( struct draw_context
*draw
,
254 const struct pipe_shader_state
*templ
)