gallium: use the same bypass_clipping logic on all vs paths
[mesa.git] / src / gallium / auxiliary / draw / draw_vs_sse.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 */
33
34 #include "draw_vs.h"
35
36 #if defined(__i386__) || defined(__386__)
37
38 #include "pipe/p_util.h"
39 #include "pipe/p_shader_tokens.h"
40
41 #include "draw_private.h"
42 #include "draw_context.h"
43
44 #include "rtasm/rtasm_x86sse.h"
45 #include "tgsi/exec/tgsi_sse2.h"
46
47
48 typedef void (XSTDCALL *codegen_function) (
49 const struct tgsi_exec_vector *input,
50 struct tgsi_exec_vector *output,
51 float (*constant)[4],
52 struct tgsi_exec_vector *temporary );
53
54
55 struct draw_sse_vertex_shader {
56 struct draw_vertex_shader base;
57 struct x86_function sse2_program;
58 codegen_function func;
59 };
60
61
62 /* Should be part of the generated shader:
63 */
64 static INLINE unsigned
65 compute_clipmask(const float *clip, /*const*/ float plane[][4], unsigned nr)
66 {
67 unsigned mask = 0;
68 unsigned i;
69
70 /* Do the hardwired planes first:
71 */
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;
78
79 /* Followed by any remaining ones:
80 */
81 for (i = 6; i < nr; i++) {
82 if (dot4(clip, plane[i]) < 0)
83 mask |= (1<<i);
84 }
85
86 return mask;
87 }
88
89
90 static void
91 vs_sse_prepare( struct draw_vertex_shader *base,
92 struct draw_context *draw )
93 {
94 draw_update_vertex_fetch( draw );
95 }
96
97 /**
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
104 */
105 static void
106 vs_sse_run( struct draw_vertex_shader *base,
107 struct draw_context *draw,
108 const unsigned *elts,
109 unsigned count,
110 struct vertex_header *vOut[] )
111 {
112 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
113 struct tgsi_exec_machine *machine = &draw->machine;
114 unsigned int j;
115
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;
120
121 assert(count <= 4);
122 assert(draw->vertex_shader->info.output_semantic_name[0]
123 == TGSI_SEMANTIC_POSITION);
124
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);
129
130
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.
135 */
136 draw->vertex_fetch.fetch_func( draw, machine, elts, count );
137
138
139 /* run compiled shader
140 */
141 shader->func(
142 machine->Inputs,
143 machine->Outputs,
144 machine->Consts,
145 machine->Temps );
146
147
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.
151 */
152 for (j = 0; j < count; j++) {
153 unsigned slot;
154 float x, y, z, w;
155
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];
160
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;
164
165 /* divide by w */
166 w = 1.0f / w;
167 x *= w;
168 y *= w;
169 z *= w;
170
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;
176 }
177 else {
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;
184 }
185
186 /* Remaining attributes are packed into sequential post-transform
187 * vertex attrib slots.
188 */
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];
194 }
195 }
196 }
197
198
199
200 static void
201 vs_sse_delete( struct draw_vertex_shader *base )
202 {
203 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
204
205 x86_release_func( &shader->sse2_program );
206
207 FREE( shader );
208 }
209
210
211 struct draw_vertex_shader *
212 draw_create_vs_sse(struct draw_context *draw,
213 const struct pipe_shader_state *templ)
214 {
215 struct draw_sse_vertex_shader *vs;
216
217 if (!draw->use_sse)
218 return NULL;
219
220 vs = CALLOC_STRUCT( draw_sse_vertex_shader );
221 if (vs == NULL)
222 return NULL;
223
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;
228
229 x86_init_func( &vs->sse2_program );
230
231 if (!tgsi_emit_sse2( (struct tgsi_token *) vs->base.state->tokens,
232 &vs->sse2_program ))
233 goto fail;
234
235 vs->func = (codegen_function) x86_get_func( &vs->sse2_program );
236
237 return &vs->base;
238
239 fail:
240 fprintf(stderr, "tgsi_emit_sse2() failed, falling back to interpreter\n");
241
242 x86_release_func( &vs->sse2_program );
243
244 FREE(vs);
245 return NULL;
246 }
247
248
249
250 #else
251
252 struct draw_vertex_shader *
253 draw_create_vs_sse( struct draw_context *draw,
254 const struct pipe_shader_state *templ )
255 {
256 return (void *) 0;
257 }
258
259
260 #endif
261