• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2009 VMware, Inc.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22  * USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 
26 /*
27  * NOTE: This file is not compiled by itself.  It's actually #included
28  * by the generated u_unfilled_gen.c file!
29  */
30 
31 #include "u_indices.h"
32 #include "u_indices_priv.h"
33 #include "util/u_prim.h"
34 
35 
translate_ubyte_ushort(const void * in,unsigned start,unsigned in_nr,unsigned out_nr,unsigned restart_index,void * out)36 static void translate_ubyte_ushort( const void *in,
37                                     unsigned start,
38                                     unsigned in_nr,
39                                     unsigned out_nr,
40                                     unsigned restart_index,
41                                     void *out )
42 {
43    const ubyte *in_ub = (const ubyte *)in;
44    ushort *out_us = (ushort *)out;
45    unsigned i;
46    for (i = 0; i < out_nr; i++)
47       out_us[i] = (ushort) in_ub[i+start];
48 }
49 
translate_memcpy_ushort(const void * in,unsigned start,unsigned in_nr,unsigned out_nr,unsigned restart_index,void * out)50 static void translate_memcpy_ushort( const void *in,
51                                      unsigned start,
52                                      unsigned in_nr,
53                                      unsigned out_nr,
54                                      unsigned restart_index,
55                                      void *out )
56 {
57    memcpy(out, &((short *)in)[start], out_nr*sizeof(short));
58 }
59 
translate_memcpy_uint(const void * in,unsigned start,unsigned in_nr,unsigned out_nr,unsigned restart_index,void * out)60 static void translate_memcpy_uint( const void *in,
61                                    unsigned start,
62                                    unsigned in_nr,
63                                    unsigned out_nr,
64                                    unsigned restart_index,
65                                    void *out )
66 {
67    memcpy(out, &((int *)in)[start], out_nr*sizeof(int));
68 }
69 
70 
generate_linear_ushort(unsigned start,unsigned nr,void * out)71 static void generate_linear_ushort( unsigned start,
72                                     unsigned nr,
73                                     void *out )
74 {
75    ushort *out_us = (ushort *)out;
76    unsigned i;
77    for (i = 0; i < nr; i++)
78       out_us[i] = (ushort)(i + start);
79 }
80 
generate_linear_uint(unsigned start,unsigned nr,void * out)81 static void generate_linear_uint( unsigned start,
82                                   unsigned nr,
83                                   void *out )
84 {
85    unsigned *out_ui = (unsigned *)out;
86    unsigned i;
87    for (i = 0; i < nr; i++)
88       out_ui[i] = i + start;
89 }
90 
91 
92 /**
93  * Given a primitive type and number of vertices, return the number of vertices
94  * needed to draw the primitive with fill mode = PIPE_POLYGON_MODE_LINE using
95  * separate lines (PIPE_PRIM_LINES).
96  */
97 static unsigned
nr_lines(enum pipe_prim_type prim,unsigned nr)98 nr_lines(enum pipe_prim_type prim, unsigned nr)
99 {
100    switch (prim) {
101    case PIPE_PRIM_TRIANGLES:
102       return (nr / 3) * 6;
103    case PIPE_PRIM_TRIANGLE_STRIP:
104       return (nr - 2) * 6;
105    case PIPE_PRIM_TRIANGLE_FAN:
106       return (nr - 2)  * 6;
107    case PIPE_PRIM_QUADS:
108       return (nr / 4) * 8;
109    case PIPE_PRIM_QUAD_STRIP:
110       return (nr - 2) / 2 * 8;
111    case PIPE_PRIM_POLYGON:
112       return 2 * nr; /* a line (two verts) for each polygon edge */
113    /* Note: these cases can't really be handled since drawing lines instead
114     * of triangles would also require changing the GS.  But if there's no GS,
115     * this should work.
116     */
117    case PIPE_PRIM_TRIANGLES_ADJACENCY:
118       return (nr / 6) * 6;
119    case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
120       return ((nr - 4) / 2) * 6;
121    default:
122       assert(0);
123       return 0;
124    }
125 }
126 
127 
128 enum indices_mode
u_unfilled_translator(enum pipe_prim_type prim,unsigned in_index_size,unsigned nr,unsigned unfilled_mode,enum pipe_prim_type * out_prim,unsigned * out_index_size,unsigned * out_nr,u_translate_func * out_translate)129 u_unfilled_translator(enum pipe_prim_type prim,
130                       unsigned in_index_size,
131                       unsigned nr,
132                       unsigned unfilled_mode,
133                       enum pipe_prim_type *out_prim,
134                       unsigned *out_index_size,
135                       unsigned *out_nr,
136                       u_translate_func *out_translate)
137 {
138    unsigned in_idx;
139    unsigned out_idx;
140 
141    assert(u_reduced_prim(prim) == PIPE_PRIM_TRIANGLES);
142 
143    u_unfilled_init();
144 
145    in_idx = in_size_idx(in_index_size);
146    *out_index_size = (in_index_size == 4) ? 4 : 2;
147    out_idx = out_size_idx(*out_index_size);
148 
149    if (unfilled_mode == PIPE_POLYGON_MODE_POINT) {
150       *out_prim = PIPE_PRIM_POINTS;
151       *out_nr = nr;
152 
153       switch (in_index_size) {
154       case 1:
155          *out_translate = translate_ubyte_ushort;
156          return U_TRANSLATE_NORMAL;
157       case 2:
158          *out_translate = translate_memcpy_uint;
159          return U_TRANSLATE_MEMCPY;
160       case 4:
161          *out_translate = translate_memcpy_ushort;
162          return U_TRANSLATE_MEMCPY;
163       default:
164          *out_translate = translate_memcpy_uint;
165          *out_nr = 0;
166          assert(0);
167          return U_TRANSLATE_ERROR;
168       }
169    }
170    else {
171       assert(unfilled_mode == PIPE_POLYGON_MODE_LINE);
172       *out_prim = PIPE_PRIM_LINES;
173       *out_translate = translate_line[in_idx][out_idx][prim];
174       *out_nr = nr_lines( prim, nr );
175       return U_TRANSLATE_NORMAL;
176    }
177 }
178 
179 
180 /**
181  * Utility for converting unfilled polygons into points, lines, triangles.
182  * Few drivers have direct support for OpenGL's glPolygonMode.
183  * This function helps with converting triangles into points or lines
184  * when the front and back fill modes are the same.  When there's
185  * different front/back fill modes, that can be handled with the
186  * 'draw' module.
187  */
188 enum indices_mode
u_unfilled_generator(enum pipe_prim_type prim,unsigned start,unsigned nr,unsigned unfilled_mode,enum pipe_prim_type * out_prim,unsigned * out_index_size,unsigned * out_nr,u_generate_func * out_generate)189 u_unfilled_generator(enum pipe_prim_type prim,
190                      unsigned start,
191                      unsigned nr,
192                      unsigned unfilled_mode,
193                      enum pipe_prim_type *out_prim,
194                      unsigned *out_index_size,
195                      unsigned *out_nr,
196                      u_generate_func *out_generate)
197 {
198    unsigned out_idx;
199 
200    assert(u_reduced_prim(prim) == PIPE_PRIM_TRIANGLES);
201 
202    u_unfilled_init();
203 
204    *out_index_size = ((start + nr) > 0xfffe) ? 4 : 2;
205    out_idx = out_size_idx(*out_index_size);
206 
207    if (unfilled_mode == PIPE_POLYGON_MODE_POINT) {
208       if (*out_index_size == 4)
209          *out_generate = generate_linear_uint;
210       else
211          *out_generate = generate_linear_ushort;
212 
213       *out_prim = PIPE_PRIM_POINTS;
214       *out_nr = nr;
215       return U_GENERATE_LINEAR;
216    }
217    else {
218       assert(unfilled_mode == PIPE_POLYGON_MODE_LINE);
219       *out_prim = PIPE_PRIM_LINES;
220       *out_generate = generate_line[out_idx][prim];
221       *out_nr = nr_lines( prim, nr );
222 
223       return U_GENERATE_REUSABLE;
224    }
225 }
226