Merge branch 'lp-offset-twoside'
[mesa.git] / src / gallium / tests / unit / translate_test.c
1 /**************************************************************************
2 *
3 * Copyright © 2010 Luca Barbieri
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 **************************************************************************/
25
26 #include <stdio.h>
27 #include <translate/translate.h>
28 #include <util/u_memory.h>
29 #include <util/u_format.h>
30 #include <util/u_cpu_detect.h>
31 #include <rtasm/rtasm_cpu.h>
32
33 /* don't use this for serious use */
34 static double rand_double()
35 {
36 const double rm = (double)RAND_MAX + 1;
37 double div = 1;
38 double v = 0;
39 unsigned i;
40 for(i = 0; i < 4; ++i)
41 {
42 div *= rm;
43 v += (double)rand() / div;
44 }
45 return v;
46 }
47
48 int main(int argc, char** argv)
49 {
50 struct translate *(*create_fn)(const struct translate_key *key) = 0;
51
52 struct translate_key key;
53 unsigned output_format;
54 unsigned input_format;
55 unsigned buffer_size = 4096;
56 unsigned char* buffer[5];
57 unsigned char* byte_buffer;
58 float* float_buffer;
59 double* double_buffer;
60 unsigned count = 4;
61 unsigned i, j, k;
62 unsigned passed = 0;
63 unsigned total = 0;
64 const float error = 0.03125;
65
66 create_fn = 0;
67
68 util_cpu_detect();
69
70 if(argc <= 1)
71 {}
72 else if (!strcmp(argv[1], "generic"))
73 create_fn = translate_generic_create;
74 else if (!strcmp(argv[1], "x86"))
75 create_fn = translate_sse2_create;
76 else if (!strcmp(argv[1], "nosse"))
77 {
78 util_cpu_caps.has_sse = 0;
79 util_cpu_caps.has_sse2 = 0;
80 util_cpu_caps.has_sse3 = 0;
81 util_cpu_caps.has_sse4_1 = 0;
82 create_fn = translate_sse2_create;
83 }
84 else if (!strcmp(argv[1], "sse"))
85 {
86 if(!util_cpu_caps.has_sse || !rtasm_cpu_has_sse())
87 {
88 printf("Error: CPU doesn't support SSE (test with qemu)\n");
89 return 2;
90 }
91 util_cpu_caps.has_sse2 = 0;
92 util_cpu_caps.has_sse3 = 0;
93 util_cpu_caps.has_sse4_1 = 0;
94 create_fn = translate_sse2_create;
95 }
96 else if (!strcmp(argv[1], "sse2"))
97 {
98 if(!util_cpu_caps.has_sse2 || !rtasm_cpu_has_sse())
99 {
100 printf("Error: CPU doesn't support SSE2 (test with qemu)\n");
101 return 2;
102 }
103 util_cpu_caps.has_sse3 = 0;
104 util_cpu_caps.has_sse4_1 = 0;
105 create_fn = translate_sse2_create;
106 }
107 else if (!strcmp(argv[1], "sse3"))
108 {
109 if(!util_cpu_caps.has_sse3 || !rtasm_cpu_has_sse())
110 {
111 printf("Error: CPU doesn't support SSE3 (test with qemu)\n");
112 return 2;
113 }
114 util_cpu_caps.has_sse4_1 = 0;
115 create_fn = translate_sse2_create;
116 }
117 else if (!strcmp(argv[1], "sse4.1"))
118 {
119 if(!util_cpu_caps.has_sse4_1 || !rtasm_cpu_has_sse())
120 {
121 printf("Error: CPU doesn't support SSE4.1 (test with qemu)\n");
122 return 2;
123 }
124 create_fn = translate_sse2_create;
125 }
126
127 if (!create_fn)
128 {
129 printf("Usage: ./translate_test [generic|x86|nosse|sse|sse2|sse3|sse4.1]\n");
130 return 2;
131 }
132
133 for (i = 1; i < Elements(buffer); ++i)
134 buffer[i] = align_malloc(buffer_size, 4096);
135
136 byte_buffer = align_malloc(buffer_size, 4096);
137 float_buffer = align_malloc(buffer_size, 4096);
138 double_buffer = align_malloc(buffer_size, 4096);
139
140 key.nr_elements = 1;
141 key.element[0].input_buffer = 0;
142 key.element[0].input_offset = 0;
143 key.element[0].output_offset = 0;
144 key.element[0].type = TRANSLATE_ELEMENT_NORMAL;
145 key.element[0].instance_divisor = 0;
146
147 srand(4359025);
148
149 /* avoid negative values that work badly when converted to unsigned format*/
150 for (i = 0; i < buffer_size; ++i)
151 byte_buffer[i] = rand() & 0x7f7f7f7f;
152
153 for (i = 0; i < buffer_size / sizeof(float); ++i)
154 float_buffer[i] = (float)rand_double();
155
156 for (i = 0; i < buffer_size / sizeof(double); ++i)
157 double_buffer[i] = rand_double();
158
159 for (output_format = 1; output_format < PIPE_FORMAT_COUNT; ++output_format)
160 {
161 const struct util_format_description* output_format_desc = util_format_description(output_format);
162 unsigned output_format_size;
163 unsigned output_normalized = 0;
164
165 if (!output_format_desc
166 || !output_format_desc->fetch_rgba_float
167 || !output_format_desc->pack_rgba_float
168 || output_format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB
169 || output_format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN
170 || !translate_is_output_format_supported(output_format))
171 continue;
172
173 for(i = 0; i < output_format_desc->nr_channels; ++i)
174 {
175 if(output_format_desc->channel[i].type != UTIL_FORMAT_TYPE_FLOAT)
176 output_normalized |= (1 << output_format_desc->channel[i].normalized);
177 }
178
179 output_format_size = util_format_get_stride(output_format, 1);
180
181 for (input_format = 1; input_format < PIPE_FORMAT_COUNT; ++input_format)
182 {
183 const struct util_format_description* input_format_desc = util_format_description(input_format);
184 unsigned input_format_size;
185 struct translate* translate[2];
186 unsigned fail = 0;
187 unsigned used_generic = 0;
188 unsigned input_normalized = 0;
189 boolean input_is_float = FALSE;
190
191 if (!input_format_desc
192 || !input_format_desc->fetch_rgba_float
193 || !input_format_desc->pack_rgba_float
194 || input_format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB
195 || input_format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN
196 || !translate_is_output_format_supported(input_format))
197 continue;
198
199 input_format_size = util_format_get_stride(input_format, 1);
200
201 for(i = 0; i < input_format_desc->nr_channels; ++i)
202 {
203 if(input_format_desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)
204 {
205 input_is_float = 1;
206 input_normalized |= 1 << 1;
207 }
208 else
209 input_normalized |= (1 << input_format_desc->channel[i].normalized);
210 }
211
212 if(((input_normalized | output_normalized) == 3)
213 || ((input_normalized & 1) && (output_normalized & 1)
214 && input_format_size * output_format_desc->nr_channels > output_format_size * input_format_desc->nr_channels))
215 continue;
216
217 key.element[0].input_format = input_format;
218 key.element[0].output_format = output_format;
219 key.output_stride = output_format_size;
220 translate[0] = create_fn(&key);
221 if (!translate[0])
222 continue;
223
224 key.element[0].input_format = output_format;
225 key.element[0].output_format = input_format;
226 key.output_stride = input_format_size;
227 translate[1] = create_fn(&key);
228 if(!translate[1])
229 {
230 used_generic = 1;
231 translate[1] = translate_generic_create(&key);
232 if(!translate[1])
233 continue;
234 }
235
236 for(i = 1; i < 5; ++i)
237 memset(buffer[i], 0xcd - (0x22 * i), 4096);
238
239 if(input_is_float && input_format_desc->channel[0].size == 32)
240 buffer[0] = (unsigned char*)float_buffer;
241 else if(input_is_float && input_format_desc->channel[0].size == 64)
242 buffer[0] = (unsigned char*)double_buffer;
243 else if(input_is_float)
244 abort();
245 else
246 buffer[0] = byte_buffer;
247
248 translate[0]->set_buffer(translate[0], 0, buffer[0], input_format_size, ~0);
249 translate[0]->run(translate[0], 0, count, 0, buffer[1]);
250 translate[1]->set_buffer(translate[1], 0, buffer[1], output_format_size, ~0);
251 translate[1]->run(translate[1], 0, count, 0, buffer[2]);
252 translate[0]->set_buffer(translate[0], 0, buffer[2], input_format_size, ~0);
253 translate[0]->run(translate[0], 0, count, 0, buffer[3]);
254 translate[1]->set_buffer(translate[1], 0, buffer[3], output_format_size, ~0);
255 translate[1]->run(translate[1], 0, count, 0, buffer[4]);
256
257 for (i = 0; i < count; ++i)
258 {
259 float a[4];
260 float b[4];
261 input_format_desc->fetch_rgba_float(a, buffer[2] + i * input_format_size, 0, 0);
262 input_format_desc->fetch_rgba_float(b, buffer[4] + i * input_format_size, 0, 0);
263
264 for (j = 0; j < count; ++j)
265 {
266 float d = a[j] - b[j];
267 if (d > error || d < -error)
268 {
269 fail = 1;
270 break;
271 }
272 }
273 }
274
275 printf("%s%s: %s -> %s -> %s -> %s -> %s\n",
276 fail ? "FAIL" : "PASS",
277 used_generic ? "[GENERIC]" : "",
278 input_format_desc->name, output_format_desc->name, input_format_desc->name, output_format_desc->name, input_format_desc->name);
279
280 if (1)
281 {
282 for (i = 0; i < Elements(buffer); ++i)
283 {
284 unsigned format_size = (i & 1) ? output_format_size : input_format_size;
285 printf("%c ", (i == 2 || i == 4) ? '*' : ' ');
286 for (j = 0; j < count; ++j)
287 {
288 for (k = 0; k < format_size; ++k)
289 {
290 printf("%02x", buffer[i][j * format_size + k]);
291 }
292 printf(" ");
293 }
294 printf("\n");
295 }
296 }
297
298 if (!fail)
299 ++passed;
300 ++total;
301
302 if(translate[1])
303 translate[1]->release(translate[1]);
304 translate[0]->release(translate[0]);
305 }
306 }
307
308 printf("%u/%u tests passed for translate_%s\n", passed, total, argv[1]);
309 return passed != total;
310 }