1 /**************************************************************************
3 * Copyright 2008 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 **************************************************************************/
28 #include "pipe/p_util.h"
29 #include "pipe/p_context.h"
30 #include "draw/draw_context.h"
31 #include "draw/draw_private.h"
32 #include "draw/draw_vbuf.h"
33 #include "draw/draw_vertex.h"
34 #include "draw/draw_pt.h"
37 struct draw_context
*draw
;
39 boolean (*run
)( struct pt_post_vs
*pvs
,
40 struct vertex_header
*vertices
,
48 dot4(const float *a
, const float *b
)
58 static INLINE
unsigned
59 compute_clipmask_gl(const float *clip
, /*const*/ float plane
[][4], unsigned nr
)
65 debug_printf("compute clipmask %f %f %f %f\n",
66 clip
[0], clip
[1], clip
[2], clip
[3]);
67 assert(clip
[3] != 0.0);
70 /* Do the hardwired planes first:
72 if (-clip
[0] + clip
[3] < 0) mask
|= (1<<0);
73 if ( clip
[0] + clip
[3] < 0) mask
|= (1<<1);
74 if (-clip
[1] + clip
[3] < 0) mask
|= (1<<2);
75 if ( clip
[1] + clip
[3] < 0) mask
|= (1<<3);
76 if ( clip
[2] + clip
[3] < 0) mask
|= (1<<4); /* match mesa clipplane numbering - for now */
77 if (-clip
[2] + clip
[3] < 0) mask
|= (1<<5); /* match mesa clipplane numbering - for now */
79 /* Followed by any remaining ones:
81 for (i
= 6; i
< nr
; i
++) {
82 if (dot4(clip
, plane
[i
]) < 0)
90 /* The normal case - cliptest, rhw divide, viewport transform.
92 * Also handle identity viewport here at the expense of a few wasted
95 static boolean
post_vs_cliptest_viewport_gl( struct pt_post_vs
*pvs
,
96 struct vertex_header
*vertices
,
100 struct vertex_header
*out
= vertices
;
101 const float *scale
= pvs
->draw
->viewport
.scale
;
102 const float *trans
= pvs
->draw
->viewport
.translate
;
103 unsigned clipped
= 0;
106 if (0) debug_printf("%s\n");
108 for (j
= 0; j
< count
; j
++) {
109 out
->clip
[0] = out
->data
[0][0];
110 out
->clip
[1] = out
->data
[0][1];
111 out
->clip
[2] = out
->data
[0][2];
112 out
->clip
[3] = out
->data
[0][3];
114 out
->vertex_id
= 0xffff;
115 out
->clipmask
= compute_clipmask_gl(out
->clip
,
117 pvs
->draw
->nr_planes
);
118 clipped
+= out
->clipmask
;
120 if (out
->clipmask
== 0)
123 float w
= 1.0f
/ out
->data
[0][3];
125 /* Viewport mapping */
126 out
->data
[0][0] = out
->data
[0][0] * w
* scale
[0] + trans
[0];
127 out
->data
[0][1] = out
->data
[0][1] * w
* scale
[1] + trans
[1];
128 out
->data
[0][2] = out
->data
[0][2] * w
* scale
[2] + trans
[2];
131 debug_printf("post viewport: %f %f %f %f\n",
139 out
= (struct vertex_header
*)( (char *)out
+ stride
);
147 /* If bypass_clipping is set, skip cliptest and rhw divide.
149 static boolean
post_vs_viewport( struct pt_post_vs
*pvs
,
150 struct vertex_header
*vertices
,
154 struct vertex_header
*out
= vertices
;
155 const float *scale
= pvs
->draw
->viewport
.scale
;
156 const float *trans
= pvs
->draw
->viewport
.translate
;
159 if (0) debug_printf("%s\n", __FUNCTION__
);
160 for (j
= 0; j
< count
; j
++) {
161 /* Viewport mapping only, no cliptest/rhw divide
163 out
->data
[0][0] = out
->data
[0][0] * scale
[0] + trans
[0];
164 out
->data
[0][1] = out
->data
[0][1] * scale
[1] + trans
[1];
165 out
->data
[0][2] = out
->data
[0][2] * scale
[2] + trans
[2];
167 out
= (struct vertex_header
*)((char *)out
+ stride
);
174 /* If bypass_clipping is set and we have an identity viewport, nothing
177 static boolean
post_vs_none( struct pt_post_vs
*pvs
,
178 struct vertex_header
*vertices
,
182 if (0) debug_printf("%s\n", __FUNCTION__
);
186 boolean
draw_pt_post_vs_run( struct pt_post_vs
*pvs
,
187 struct vertex_header
*pipeline_verts
,
191 return pvs
->run( pvs
, pipeline_verts
, count
, stride
);
195 void draw_pt_post_vs_prepare( struct pt_post_vs
*pvs
,
196 boolean bypass_clipping
,
197 boolean identity_viewport
,
200 if (bypass_clipping
) {
201 if (identity_viewport
)
202 pvs
->run
= post_vs_none
;
204 pvs
->run
= post_vs_viewport
;
208 pvs
->run
= post_vs_cliptest_viewport_gl
;
213 struct pt_post_vs
*draw_pt_post_vs_create( struct draw_context
*draw
)
215 struct pt_post_vs
*pvs
= CALLOC_STRUCT( pt_post_vs
);
224 void draw_pt_post_vs_destroy( struct pt_post_vs
*pvs
)