1 /**************************************************************************
2  *
3  * Copyright 2008 VMware, Inc.
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 VMWARE 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 #include "util/u_memory.h"
29 #include "util/u_math.h"
30 #include "util/u_prim.h"
31 #include "pipe/p_context.h"
32 #include "draw/draw_context.h"
33 #include "draw/draw_private.h"
34 #include "draw/draw_pt.h"
35 #include "draw/draw_vs.h"
36 
37 #define DO_CLIP_XY           0x1
38 #define DO_CLIP_FULL_Z       0x2
39 #define DO_CLIP_HALF_Z       0x4
40 #define DO_CLIP_USER         0x8
41 #define DO_VIEWPORT          0x10
42 #define DO_EDGEFLAG          0x20
43 #define DO_CLIP_XY_GUARD_BAND 0x40
44 
45 
46 struct pt_post_vs {
47    struct draw_context *draw;
48 
49    unsigned flags;
50 
51    boolean (*run)( struct pt_post_vs *pvs,
52                    struct draw_vertex_info *info,
53                    const struct draw_prim_info *prim_info );
54 };
55 
56 static inline void
initialize_vertex_header(struct vertex_header *header)57 initialize_vertex_header(struct vertex_header *header)
58 {
59    header->clipmask = 0;
60    header->edgeflag = 1;
61    header->pad = 0;
62    header->vertex_id = UNDEFINED_VERTEX_ID;
63 }
64 
65 static inline float
dot4(const float *a, const float *b)66 dot4(const float *a, const float *b)
67 {
68    return (a[0]*b[0] +
69            a[1]*b[1] +
70            a[2]*b[2] +
71            a[3]*b[3]);
72 }
73 
74 #define FLAGS (0)
75 #define TAG(x) x##_none
76 #include "draw_cliptest_tmp.h"
77 
78 #define FLAGS (DO_CLIP_XY | DO_CLIP_FULL_Z | DO_VIEWPORT)
79 #define TAG(x) x##_xy_fullz_viewport
80 #include "draw_cliptest_tmp.h"
81 
82 #define FLAGS (DO_CLIP_XY | DO_CLIP_HALF_Z | DO_VIEWPORT)
83 #define TAG(x) x##_xy_halfz_viewport
84 #include "draw_cliptest_tmp.h"
85 
86 #define FLAGS (DO_CLIP_XY_GUARD_BAND | DO_CLIP_HALF_Z | DO_VIEWPORT)
87 #define TAG(x) x##_xy_gb_halfz_viewport
88 #include "draw_cliptest_tmp.h"
89 
90 #define FLAGS (DO_CLIP_XY_GUARD_BAND | DO_CLIP_FULL_Z | DO_VIEWPORT)
91 #define TAG(x) x##_xy_gb_fullz_viewport
92 #include "draw_cliptest_tmp.h"
93 
94 #define FLAGS (DO_CLIP_FULL_Z | DO_VIEWPORT)
95 #define TAG(x) x##_fullz_viewport
96 #include "draw_cliptest_tmp.h"
97 
98 #define FLAGS (DO_CLIP_HALF_Z | DO_VIEWPORT)
99 #define TAG(x) x##_halfz_viewport
100 #include "draw_cliptest_tmp.h"
101 
102 #define FLAGS (DO_CLIP_XY | DO_CLIP_FULL_Z | DO_CLIP_USER | DO_VIEWPORT)
103 #define TAG(x) x##_xy_fullz_user_viewport
104 #include "draw_cliptest_tmp.h"
105 
106 #define FLAGS (DO_CLIP_XY | DO_CLIP_FULL_Z | DO_CLIP_USER | DO_VIEWPORT | DO_EDGEFLAG)
107 #define TAG(x) x##_xy_fullz_user_viewport_edgeflag
108 #include "draw_cliptest_tmp.h"
109 
110 
111 
112 /* Don't want to create 64 versions of this function, so catch the
113  * less common ones here.  This is looking like something which should
114  * be code-generated, perhaps appended to the end of the vertex
115  * shader.
116  */
117 #define FLAGS (pvs->flags)
118 #define TAG(x) x##_generic
119 #include "draw_cliptest_tmp.h"
120 
121 
122 
draw_pt_post_vs_run( struct pt_post_vs *pvs, struct draw_vertex_info *info, const struct draw_prim_info *prim_info )123 boolean draw_pt_post_vs_run( struct pt_post_vs *pvs,
124                              struct draw_vertex_info *info,
125                              const struct draw_prim_info *prim_info )
126 {
127    return pvs->run( pvs, info, prim_info );
128 }
129 
130 
draw_pt_post_vs_prepare( struct pt_post_vs *pvs, boolean clip_xy, boolean clip_z, boolean clip_user, boolean guard_band, boolean bypass_viewport, boolean clip_halfz, boolean need_edgeflags )131 void draw_pt_post_vs_prepare( struct pt_post_vs *pvs,
132 			      boolean clip_xy,
133 			      boolean clip_z,
134                               boolean clip_user,
135                               boolean guard_band,
136 			      boolean bypass_viewport,
137                               boolean clip_halfz,
138 			      boolean need_edgeflags )
139 {
140    pvs->flags = 0;
141 
142    if (clip_xy && !guard_band) {
143       pvs->flags |= DO_CLIP_XY;
144       ASSIGN_4V( pvs->draw->plane[0], -1,  0,  0, 1 );
145       ASSIGN_4V( pvs->draw->plane[1],  1,  0,  0, 1 );
146       ASSIGN_4V( pvs->draw->plane[2],  0, -1,  0, 1 );
147       ASSIGN_4V( pvs->draw->plane[3],  0,  1,  0, 1 );
148    }
149    else if (clip_xy && guard_band) {
150       pvs->flags |= DO_CLIP_XY_GUARD_BAND;
151       ASSIGN_4V( pvs->draw->plane[0], -0.5,  0,  0, 1 );
152       ASSIGN_4V( pvs->draw->plane[1],  0.5,  0,  0, 1 );
153       ASSIGN_4V( pvs->draw->plane[2],  0, -0.5,  0, 1 );
154       ASSIGN_4V( pvs->draw->plane[3],  0,  0.5,  0, 1 );
155    }
156 
157    if (clip_z) {
158       if (clip_halfz) {
159          pvs->flags |= DO_CLIP_HALF_Z;
160          ASSIGN_4V( pvs->draw->plane[4],  0,  0,  1, 0 );
161       } else {
162          pvs->flags |= DO_CLIP_FULL_Z;
163          ASSIGN_4V( pvs->draw->plane[4],  0,  0,  1, 1 );
164       }
165    }
166 
167    if (clip_user)
168       pvs->flags |= DO_CLIP_USER;
169 
170    if (!bypass_viewport)
171       pvs->flags |= DO_VIEWPORT;
172 
173    if (need_edgeflags)
174       pvs->flags |= DO_EDGEFLAG;
175 
176    /* Now select the relevant function:
177     */
178    switch (pvs->flags) {
179    case 0:
180       pvs->run = do_cliptest_none;
181       break;
182 
183    case DO_CLIP_XY | DO_CLIP_FULL_Z | DO_VIEWPORT:
184       pvs->run = do_cliptest_xy_fullz_viewport;
185       break;
186 
187    case DO_CLIP_XY | DO_CLIP_HALF_Z | DO_VIEWPORT:
188       pvs->run = do_cliptest_xy_halfz_viewport;
189       break;
190 
191    case DO_CLIP_XY_GUARD_BAND | DO_CLIP_HALF_Z | DO_VIEWPORT:
192       pvs->run = do_cliptest_xy_gb_halfz_viewport;
193       break;
194 
195    case DO_CLIP_XY_GUARD_BAND | DO_CLIP_FULL_Z | DO_VIEWPORT:
196       pvs->run = do_cliptest_xy_gb_fullz_viewport;
197       break;
198 
199    case DO_CLIP_FULL_Z | DO_VIEWPORT:
200       pvs->run = do_cliptest_fullz_viewport;
201       break;
202 
203    case DO_CLIP_HALF_Z | DO_VIEWPORT:
204       pvs->run = do_cliptest_halfz_viewport;
205       break;
206 
207    case DO_CLIP_XY | DO_CLIP_FULL_Z | DO_CLIP_USER | DO_VIEWPORT:
208       pvs->run = do_cliptest_xy_fullz_user_viewport;
209       break;
210 
211    case (DO_CLIP_XY | DO_CLIP_FULL_Z | DO_CLIP_USER |
212          DO_VIEWPORT | DO_EDGEFLAG):
213       pvs->run = do_cliptest_xy_fullz_user_viewport_edgeflag;
214       break;
215 
216    default:
217       pvs->run = do_cliptest_generic;
218       break;
219    }
220 }
221 
222 
draw_pt_post_vs_create( struct draw_context *draw )223 struct pt_post_vs *draw_pt_post_vs_create( struct draw_context *draw )
224 {
225    struct pt_post_vs *pvs = CALLOC_STRUCT( pt_post_vs );
226    if (!pvs)
227       return NULL;
228 
229    pvs->draw = draw;
230 
231    return pvs;
232 }
233 
draw_pt_post_vs_destroy( struct pt_post_vs *pvs )234 void draw_pt_post_vs_destroy( struct pt_post_vs *pvs )
235 {
236    FREE(pvs);
237 }
238