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 "util/u_memory.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_pt.h"
36 struct draw_context
*draw
;
38 boolean (*run
)( struct pt_post_vs
*pvs
,
39 struct vertex_header
*vertices
,
47 dot4(const float *a
, const float *b
)
57 static INLINE
unsigned
58 compute_clipmask_gl(const float *clip
, /*const*/ float plane
[][4], unsigned nr
)
64 debug_printf("compute clipmask %f %f %f %f\n",
65 clip
[0], clip
[1], clip
[2], clip
[3]);
66 assert(clip
[3] != 0.0);
69 /* Do the hardwired planes first:
71 if (-clip
[0] + clip
[3] < 0) mask
|= (1<<0);
72 if ( clip
[0] + clip
[3] < 0) mask
|= (1<<1);
73 if (-clip
[1] + clip
[3] < 0) mask
|= (1<<2);
74 if ( clip
[1] + clip
[3] < 0) mask
|= (1<<3);
75 if ( clip
[2] + clip
[3] < 0) mask
|= (1<<4); /* match mesa clipplane numbering - for now */
76 if (-clip
[2] + clip
[3] < 0) mask
|= (1<<5); /* match mesa clipplane numbering - for now */
78 /* Followed by any remaining ones:
80 for (i
= 6; i
< nr
; i
++) {
81 if (dot4(clip
, plane
[i
]) < 0)
89 /* The normal case - cliptest, rhw divide, viewport transform.
91 * Also handle identity viewport here at the expense of a few wasted
94 static boolean
post_vs_cliptest_viewport_gl( struct pt_post_vs
*pvs
,
95 struct vertex_header
*vertices
,
99 struct vertex_header
*out
= vertices
;
100 const float *scale
= pvs
->draw
->viewport
.scale
;
101 const float *trans
= pvs
->draw
->viewport
.translate
;
102 const unsigned pos
= draw_current_shader_position_output(pvs
->draw
);
103 unsigned clipped
= 0;
106 if (0) debug_printf("%s\n", __FUNCTION__
);
108 for (j
= 0; j
< count
; j
++) {
109 float *position
= out
->data
[pos
];
111 out
->clip
[0] = position
[0];
112 out
->clip
[1] = position
[1];
113 out
->clip
[2] = position
[2];
114 out
->clip
[3] = position
[3];
116 out
->vertex_id
= 0xffff;
117 out
->clipmask
= compute_clipmask_gl(out
->clip
,
119 pvs
->draw
->nr_planes
);
120 clipped
+= out
->clipmask
;
122 if (out
->clipmask
== 0)
125 float w
= 1.0f
/ position
[3];
127 /* Viewport mapping */
128 position
[0] = position
[0] * w
* scale
[0] + trans
[0];
129 position
[1] = position
[1] * w
* scale
[1] + trans
[1];
130 position
[2] = position
[2] * w
* scale
[2] + trans
[2];
133 debug_printf("post viewport: %f %f %f %f\n",
141 out
= (struct vertex_header
*)( (char *)out
+ stride
);
149 /* As above plus edgeflags
152 post_vs_cliptest_viewport_gl_edgeflag(struct pt_post_vs
*pvs
,
153 struct vertex_header
*vertices
,
160 needpipe
= post_vs_cliptest_viewport_gl( pvs
, vertices
, count
, stride
);
162 /* If present, copy edgeflag VS output into vertex header.
163 * Otherwise, leave header as is.
165 if (pvs
->draw
->vs
.edgeflag_output
) {
166 struct vertex_header
*out
= vertices
;
167 int ef
= pvs
->draw
->vs
.edgeflag_output
;
169 for (j
= 0; j
< count
; j
++) {
170 const float *edgeflag
= out
->data
[ef
];
171 out
->edgeflag
= !(edgeflag
[0] != 1.0f
);
172 needpipe
|= !out
->edgeflag
;
173 out
= (struct vertex_header
*)( (char *)out
+ stride
);
182 /* If bypass_clipping is set, skip cliptest and rhw divide.
184 static boolean
post_vs_viewport( struct pt_post_vs
*pvs
,
185 struct vertex_header
*vertices
,
189 struct vertex_header
*out
= vertices
;
190 const float *scale
= pvs
->draw
->viewport
.scale
;
191 const float *trans
= pvs
->draw
->viewport
.translate
;
192 const unsigned pos
= draw_current_shader_position_output(pvs
->draw
);
195 if (0) debug_printf("%s\n", __FUNCTION__
);
196 for (j
= 0; j
< count
; j
++) {
197 float *position
= out
->data
[pos
];
199 /* Viewport mapping only, no cliptest/rhw divide
201 position
[0] = position
[0] * scale
[0] + trans
[0];
202 position
[1] = position
[1] * scale
[1] + trans
[1];
203 position
[2] = position
[2] * scale
[2] + trans
[2];
205 out
= (struct vertex_header
*)((char *)out
+ stride
);
212 /* If bypass_clipping is set and we have an identity viewport, nothing
215 static boolean
post_vs_none( struct pt_post_vs
*pvs
,
216 struct vertex_header
*vertices
,
220 if (0) debug_printf("%s\n", __FUNCTION__
);
224 boolean
draw_pt_post_vs_run( struct pt_post_vs
*pvs
,
225 struct vertex_header
*pipeline_verts
,
229 return pvs
->run( pvs
, pipeline_verts
, count
, stride
);
233 void draw_pt_post_vs_prepare( struct pt_post_vs
*pvs
,
234 boolean bypass_clipping
,
235 boolean bypass_viewport
,
237 boolean need_edgeflags
)
239 if (!need_edgeflags
) {
240 if (bypass_clipping
) {
242 pvs
->run
= post_vs_none
;
244 pvs
->run
= post_vs_viewport
;
248 pvs
->run
= post_vs_cliptest_viewport_gl
;
252 /* If we need to copy edgeflags to the vertex header, it should
253 * mean we're running the primitive pipeline. Hence the bypass
254 * flags should be false.
256 assert(!bypass_clipping
);
257 assert(!bypass_viewport
);
258 pvs
->run
= post_vs_cliptest_viewport_gl_edgeflag
;
263 struct pt_post_vs
*draw_pt_post_vs_create( struct draw_context
*draw
)
265 struct pt_post_vs
*pvs
= CALLOC_STRUCT( pt_post_vs
);
274 void draw_pt_post_vs_destroy( struct pt_post_vs
*pvs
)