gallivm: Fix Altivec pack intrinsics for little-endian
[mesa.git] / src / gallium / auxiliary / indices / u_unfilled_indices.c
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 #include "u_indices.h"
26 #include "u_indices_priv.h"
27
28
29 static void translate_ubyte_ushort( const void *in,
30 unsigned start,
31 unsigned nr,
32 void *out )
33 {
34 const ubyte *in_ub = (const ubyte *)in;
35 ushort *out_us = (ushort *)out;
36 unsigned i;
37 for (i = 0; i < nr; i++)
38 out_us[i] = (ushort) in_ub[i+start];
39 }
40
41 static void translate_memcpy_ushort( const void *in,
42 unsigned start,
43 unsigned nr,
44 void *out )
45 {
46 memcpy(out, &((short *)in)[start], nr*sizeof(short));
47 }
48
49 static void translate_memcpy_uint( const void *in,
50 unsigned start,
51 unsigned nr,
52 void *out )
53 {
54 memcpy(out, &((int *)in)[start], nr*sizeof(int));
55 }
56
57
58 static void generate_linear_ushort( unsigned start,
59 unsigned nr,
60 void *out )
61 {
62 ushort *out_us = (ushort *)out;
63 unsigned i;
64 for (i = 0; i < nr; i++)
65 out_us[i] = (ushort)(i + start);
66 }
67
68 static void generate_linear_uint( unsigned start,
69 unsigned nr,
70 void *out )
71 {
72 unsigned *out_ui = (unsigned *)out;
73 unsigned i;
74 for (i = 0; i < nr; i++)
75 out_ui[i] = i + start;
76 }
77
78
79 /**
80 * Given a primitive type and number of vertices, return the number of vertices
81 * needed to draw the primitive with fill mode = PIPE_POLYGON_MODE_LINE using
82 * separate lines (PIPE_PRIM_LINES).
83 */
84 static unsigned nr_lines( unsigned prim,
85 unsigned nr )
86 {
87 switch (prim) {
88 case PIPE_PRIM_TRIANGLES:
89 return (nr / 3) * 6;
90 case PIPE_PRIM_TRIANGLE_STRIP:
91 return (nr - 2) * 6;
92 case PIPE_PRIM_TRIANGLE_FAN:
93 return (nr - 2) * 6;
94 case PIPE_PRIM_QUADS:
95 return (nr / 4) * 8;
96 case PIPE_PRIM_QUAD_STRIP:
97 return (nr - 2) / 2 * 8;
98 case PIPE_PRIM_POLYGON:
99 return 2 * nr; /* a line (two verts) for each polygon edge */
100 default:
101 assert(0);
102 return 0;
103 }
104 }
105
106
107
108 int u_unfilled_translator( unsigned prim,
109 unsigned in_index_size,
110 unsigned nr,
111 unsigned unfilled_mode,
112 unsigned *out_prim,
113 unsigned *out_index_size,
114 unsigned *out_nr,
115 u_translate_func *out_translate )
116 {
117 unsigned in_idx;
118 unsigned out_idx;
119
120 u_unfilled_init();
121
122 in_idx = in_size_idx(in_index_size);
123 *out_index_size = (in_index_size == 4) ? 4 : 2;
124 out_idx = out_size_idx(*out_index_size);
125
126 if (unfilled_mode == PIPE_POLYGON_MODE_POINT)
127 {
128 *out_prim = PIPE_PRIM_POINTS;
129 *out_nr = nr;
130
131 switch (in_index_size)
132 {
133 case 1:
134 *out_translate = translate_ubyte_ushort;
135 return U_TRANSLATE_NORMAL;
136 case 2:
137 *out_translate = translate_memcpy_uint;
138 return U_TRANSLATE_MEMCPY;
139 case 4:
140 *out_translate = translate_memcpy_ushort;
141 return U_TRANSLATE_MEMCPY;
142 default:
143 *out_translate = translate_memcpy_uint;
144 *out_nr = 0;
145 assert(0);
146 return U_TRANSLATE_ERROR;
147 }
148 }
149 else {
150 assert(unfilled_mode == PIPE_POLYGON_MODE_LINE);
151 *out_prim = PIPE_PRIM_LINES;
152 *out_translate = translate_line[in_idx][out_idx][prim];
153 *out_nr = nr_lines( prim, nr );
154 return U_TRANSLATE_NORMAL;
155 }
156 }
157
158
159 /**
160 * Utility for converting unfilled polygons into points, lines, triangles.
161 * Few drivers have direct support for OpenGL's glPolygonMode.
162 * This function helps with converting triangles into points or lines
163 * when the front and back fill modes are the same. When there's
164 * different front/back fill modes, that can be handled with the
165 * 'draw' module.
166 */
167 int u_unfilled_generator( unsigned prim,
168 unsigned start,
169 unsigned nr,
170 unsigned unfilled_mode,
171 unsigned *out_prim,
172 unsigned *out_index_size,
173 unsigned *out_nr,
174 u_generate_func *out_generate )
175 {
176 unsigned out_idx;
177
178 u_unfilled_init();
179
180 *out_index_size = ((start + nr) > 0xfffe) ? 4 : 2;
181 out_idx = out_size_idx(*out_index_size);
182
183 if (unfilled_mode == PIPE_POLYGON_MODE_POINT) {
184
185 if (*out_index_size == 4)
186 *out_generate = generate_linear_uint;
187 else
188 *out_generate = generate_linear_ushort;
189
190 *out_prim = PIPE_PRIM_POINTS;
191 *out_nr = nr;
192 return U_GENERATE_LINEAR;
193 }
194 else {
195 assert(unfilled_mode == PIPE_POLYGON_MODE_LINE);
196 *out_prim = PIPE_PRIM_LINES;
197 *out_generate = generate_line[out_idx][prim];
198 *out_nr = nr_lines( prim, nr );
199
200 return U_GENERATE_REUSABLE;
201 }
202 }
203