translate_test: improve
[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 int main(int argc, char** argv)
34 {
35 struct translate *(*create_fn)(const struct translate_key *key) = 0;
36
37 struct translate_key key;
38 unsigned output_format;
39 unsigned input_format;
40 unsigned buffer_size = 4096;
41 unsigned char* buffer[5];
42 unsigned char* byte_buffer;
43 float* float_buffer;
44 double* double_buffer;
45 unsigned count = 4;
46 unsigned i, j, k;
47 unsigned passed = 0;
48 unsigned total = 0;
49 const float error = 0.03125;
50
51 create_fn = 0;
52
53 util_cpu_detect();
54
55 if(argc <= 1)
56 {}
57 else if (!strcmp(argv[1], "generic"))
58 create_fn = translate_generic_create;
59 else if (!strcmp(argv[1], "x86"))
60 create_fn = translate_sse2_create;
61 else if (!strcmp(argv[1], "nosse"))
62 {
63 util_cpu_caps.has_sse = 0;
64 util_cpu_caps.has_sse2 = 0;
65 util_cpu_caps.has_sse3 = 0;
66 util_cpu_caps.has_sse4_1 = 0;
67 create_fn = translate_sse2_create;
68 }
69 else if (!strcmp(argv[1], "sse"))
70 {
71 if(!util_cpu_caps.has_sse || !rtasm_cpu_has_sse())
72 {
73 printf("Error: CPU doesn't support SSE (test with qemu)\n");
74 return 2;
75 }
76 util_cpu_caps.has_sse2 = 0;
77 util_cpu_caps.has_sse3 = 0;
78 util_cpu_caps.has_sse4_1 = 0;
79 create_fn = translate_sse2_create;
80 }
81 else if (!strcmp(argv[1], "sse2"))
82 {
83 if(!util_cpu_caps.has_sse2 || !rtasm_cpu_has_sse())
84 {
85 printf("Error: CPU doesn't support SSE2 (test with qemu)\n");
86 return 2;
87 }
88 util_cpu_caps.has_sse3 = 0;
89 util_cpu_caps.has_sse4_1 = 0;
90 create_fn = translate_sse2_create;
91 }
92 else if (!strcmp(argv[1], "sse3"))
93 {
94 if(!util_cpu_caps.has_sse3 || !rtasm_cpu_has_sse())
95 {
96 printf("Error: CPU doesn't support SSE3 (test with qemu)\n");
97 return 2;
98 }
99 util_cpu_caps.has_sse4_1 = 0;
100 create_fn = translate_sse2_create;
101 }
102 else if (!strcmp(argv[1], "sse4.1"))
103 {
104 if(!util_cpu_caps.has_sse4_1 || !rtasm_cpu_has_sse())
105 {
106 printf("Error: CPU doesn't support SSE4.1 (test with qemu)\n");
107 return 2;
108 }
109 create_fn = translate_sse2_create;
110 }
111
112 if (!create_fn)
113 {
114 printf("Usage: ./translate_test [generic|x86|nosse|sse|sse2|sse3|sse4.1]\n");
115 return 2;
116 }
117
118 for (i = 1; i < Elements(buffer); ++i)
119 buffer[i] = align_malloc(buffer_size, 4096);
120
121 byte_buffer = align_malloc(buffer_size, 4096);
122 float_buffer = align_malloc(buffer_size, 4096);
123 double_buffer = align_malloc(buffer_size, 4096);
124
125 key.nr_elements = 1;
126 key.element[0].input_buffer = 0;
127 key.element[0].input_offset = 0;
128 key.element[0].output_offset = 0;
129 key.element[0].type = TRANSLATE_ELEMENT_NORMAL;
130 key.element[0].instance_divisor = 0;
131
132 srand48(4359025);
133
134 /* avoid negative values that work badly when converted to unsigned format*/
135 for (i = 0; i < buffer_size / sizeof(unsigned); ++i)
136 ((unsigned*)byte_buffer)[i] = mrand48() & 0x7f7f7f7f;
137
138 for (i = 0; i < buffer_size / sizeof(float); ++i)
139 float_buffer[i] = (float)drand48();
140
141 for (i = 0; i < buffer_size / sizeof(double); ++i)
142 double_buffer[i] = drand48();
143
144 for (output_format = 1; output_format < PIPE_FORMAT_COUNT; ++output_format)
145 {
146 const struct util_format_description* output_format_desc = util_format_description(output_format);
147 unsigned output_format_size;
148 unsigned output_normalized = 0;
149
150 if (!output_format_desc
151 || !output_format_desc->fetch_rgba_float
152 || !output_format_desc->pack_rgba_float
153 || output_format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB
154 || output_format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN
155 || !translate_is_output_format_supported(output_format))
156 continue;
157
158 for(i = 0; i < output_format_desc->nr_channels; ++i)
159 {
160 if(output_format_desc->channel[i].type != UTIL_FORMAT_TYPE_FLOAT)
161 output_normalized |= (1 << output_format_desc->channel[i].normalized);
162 }
163
164 output_format_size = util_format_get_stride(output_format, 1);
165
166 for (input_format = 1; input_format < PIPE_FORMAT_COUNT; ++input_format)
167 {
168 const struct util_format_description* input_format_desc = util_format_description(input_format);
169 unsigned input_format_size;
170 struct translate* translate[2];
171 unsigned fail = 0;
172 unsigned used_generic = 0;
173 unsigned input_normalized = 0;
174 boolean input_is_float = FALSE;
175
176 if (!input_format_desc
177 || !input_format_desc->fetch_rgba_float
178 || !input_format_desc->pack_rgba_float
179 || input_format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB
180 || input_format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN
181 || !translate_is_output_format_supported(input_format))
182 continue;
183
184 input_format_size = util_format_get_stride(input_format, 1);
185
186 for(i = 0; i < input_format_desc->nr_channels; ++i)
187 {
188 if(input_format_desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)
189 {
190 input_is_float = 1;
191 input_normalized |= 1 << 1;
192 }
193 else
194 input_normalized |= (1 << input_format_desc->channel[i].normalized);
195 }
196
197 if(((input_normalized | output_normalized) == 3)
198 || ((input_normalized & 1) && (output_normalized & 1)
199 && input_format_size * output_format_desc->nr_channels > output_format_size * input_format_desc->nr_channels))
200 continue;
201
202 key.element[0].input_format = input_format;
203 key.element[0].output_format = output_format;
204 key.output_stride = output_format_size;
205 translate[0] = create_fn(&key);
206 if (!translate[0])
207 continue;
208
209 key.element[0].input_format = output_format;
210 key.element[0].output_format = input_format;
211 key.output_stride = input_format_size;
212 translate[1] = create_fn(&key);
213 if(!translate[1])
214 {
215 used_generic = 1;
216 translate[1] = translate_generic_create(&key);
217 if(!translate[1])
218 continue;
219 }
220
221 for(i = 1; i < 5; ++i)
222 memset(buffer[i], 0xcd - (0x22 * i), 4096);
223
224 if(input_is_float && input_format_desc->channel[0].size == 32)
225 buffer[0] = (unsigned char*)float_buffer;
226 else if(input_is_float && input_format_desc->channel[0].size == 64)
227 buffer[0] = (unsigned char*)double_buffer;
228 else if(input_is_float)
229 abort();
230 else
231 buffer[0] = byte_buffer;
232
233 translate[0]->set_buffer(translate[0], 0, buffer[0], input_format_size, ~0);
234 translate[0]->run(translate[0], 0, count, 0, buffer[1]);
235 translate[1]->set_buffer(translate[1], 0, buffer[1], output_format_size, ~0);
236 translate[1]->run(translate[1], 0, count, 0, buffer[2]);
237 translate[0]->set_buffer(translate[0], 0, buffer[2], input_format_size, ~0);
238 translate[0]->run(translate[0], 0, count, 0, buffer[3]);
239 translate[1]->set_buffer(translate[1], 0, buffer[3], output_format_size, ~0);
240 translate[1]->run(translate[1], 0, count, 0, buffer[4]);
241
242 for (i = 0; i < count; ++i)
243 {
244 float a[4];
245 float b[4];
246 input_format_desc->fetch_rgba_float(a, buffer[2] + i * input_format_size, 0, 0);
247 input_format_desc->fetch_rgba_float(b, buffer[4] + i * input_format_size, 0, 0);
248
249 for (j = 0; j < count; ++j)
250 {
251 float d = a[j] - b[j];
252 if (d > error || d < -error)
253 {
254 fail = 1;
255 break;
256 }
257 }
258 }
259
260 printf("%s%s: %s -> %s -> %s -> %s -> %s\n",
261 fail ? "FAIL" : "PASS",
262 used_generic ? "[GENERIC]" : "",
263 input_format_desc->name, output_format_desc->name, input_format_desc->name, output_format_desc->name, input_format_desc->name);
264
265 if (1)
266 {
267 for (i = 0; i < Elements(buffer); ++i)
268 {
269 unsigned format_size = (i & 1) ? output_format_size : input_format_size;
270 printf("%c ", (i == 2 || i == 4) ? '*' : ' ');
271 for (j = 0; j < count; ++j)
272 {
273 for (k = 0; k < format_size; ++k)
274 {
275 printf("%02x", buffer[i][j * format_size + k]);
276 }
277 printf(" ");
278 }
279 printf("\n");
280 }
281 }
282
283 if (!fail)
284 ++passed;
285 ++total;
286
287 if(translate[1])
288 translate[1]->release(translate[1]);
289 translate[0]->release(translate[0]);
290 }
291 }
292
293 printf("%u/%u tests passed for translate_%s\n", passed, total, argv[1]);
294 return passed != total;
295 }