Path: blob/21.2-virgl/src/gallium/tests/unit/translate_test.c
4565 views
/**************************************************************************1*2* Copyright © 2010 Luca Barbieri3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*23**************************************************************************/2425#include <stdio.h>26#include "translate/translate.h"27#include "util/u_memory.h"28#include "util/format/u_format.h"29#include "util/half_float.h"30#include "util/u_cpu_detect.h"31#include "rtasm/rtasm_cpu.h"3233/* don't use this for serious use */34static double rand_double()35{36const double rm = (double)RAND_MAX + 1;37double div = 1;38double v = 0;39unsigned i;40for(i = 0; i < 4; ++i)41{42div *= rm;43v += (double)rand() / div;44}45return v;46}4748int main(int argc, char** argv)49{50struct translate *(*create_fn)(const struct translate_key *key) = 0;5152extern struct util_cpu_caps_t util_cpu_caps;53struct translate_key key;54unsigned output_format;55unsigned input_format;56unsigned buffer_size = 4096;57unsigned char* buffer[5];58unsigned char* byte_buffer;59float* float_buffer;60double* double_buffer;61uint16_t *half_buffer;62unsigned * elts;63unsigned count = 4;64unsigned i, j, k;65unsigned passed = 0;66unsigned total = 0;67const float error = 0.03125;6869create_fn = 0;7071util_cpu_detect();7273if (argc <= 1 ||74!strcmp(argv[1], "default") )75create_fn = translate_create;76else if (!strcmp(argv[1], "generic"))77create_fn = translate_generic_create;78else if (!strcmp(argv[1], "x86"))79create_fn = translate_sse2_create;80else if (!strcmp(argv[1], "nosse"))81{82util_cpu_caps.has_sse = 0;83util_cpu_caps.has_sse2 = 0;84util_cpu_caps.has_sse3 = 0;85util_cpu_caps.has_sse4_1 = 0;86create_fn = translate_sse2_create;87}88else if (!strcmp(argv[1], "sse"))89{90if(!util_get_cpu_caps()->has_sse || !rtasm_cpu_has_sse())91{92printf("Error: CPU doesn't support SSE (test with qemu)\n");93return 2;94}95util_cpu_caps.has_sse2 = 0;96util_cpu_caps.has_sse3 = 0;97util_cpu_caps.has_sse4_1 = 0;98create_fn = translate_sse2_create;99}100else if (!strcmp(argv[1], "sse2"))101{102if(!util_get_cpu_caps()->has_sse2 || !rtasm_cpu_has_sse())103{104printf("Error: CPU doesn't support SSE2 (test with qemu)\n");105return 2;106}107util_cpu_caps.has_sse3 = 0;108util_cpu_caps.has_sse4_1 = 0;109create_fn = translate_sse2_create;110}111else if (!strcmp(argv[1], "sse3"))112{113if(!util_get_cpu_caps()->has_sse3 || !rtasm_cpu_has_sse())114{115printf("Error: CPU doesn't support SSE3 (test with qemu)\n");116return 2;117}118util_cpu_caps.has_sse4_1 = 0;119create_fn = translate_sse2_create;120}121else if (!strcmp(argv[1], "sse4.1"))122{123if(!util_get_cpu_caps()->has_sse4_1 || !rtasm_cpu_has_sse())124{125printf("Error: CPU doesn't support SSE4.1 (test with qemu)\n");126return 2;127}128create_fn = translate_sse2_create;129}130131if (!create_fn)132{133printf("Usage: ./translate_test [default|generic|x86|nosse|sse|sse2|sse3|sse4.1]\n");134return 2;135}136137for (i = 1; i < ARRAY_SIZE(buffer); ++i)138buffer[i] = align_malloc(buffer_size, 4096);139140byte_buffer = align_malloc(buffer_size, 4096);141float_buffer = align_malloc(buffer_size, 4096);142double_buffer = align_malloc(buffer_size, 4096);143half_buffer = align_malloc(buffer_size, 4096);144145elts = align_malloc(count * sizeof *elts, 4096);146147key.nr_elements = 1;148key.element[0].input_buffer = 0;149key.element[0].input_offset = 0;150key.element[0].output_offset = 0;151key.element[0].type = TRANSLATE_ELEMENT_NORMAL;152key.element[0].instance_divisor = 0;153154srand(4359025);155156/* avoid negative values that work badly when converted to unsigned format*/157for (i = 0; i < buffer_size; ++i)158byte_buffer[i] = rand() & 0x7f7f7f7f;159160for (i = 0; i < buffer_size / sizeof(float); ++i)161float_buffer[i] = (float)rand_double();162163for (i = 0; i < buffer_size / sizeof(double); ++i)164double_buffer[i] = rand_double();165166for (i = 0; i < buffer_size / sizeof(double); ++i)167half_buffer[i] = _mesa_float_to_half((float) rand_double());168169for (i = 0; i < count; ++i)170elts[i] = i;171172for (output_format = 1; output_format < PIPE_FORMAT_COUNT; ++output_format)173{174const struct util_format_description* output_format_desc = util_format_description(output_format);175const struct util_format_pack_description* output_format_pack = util_format_pack_description(output_format);176util_format_fetch_rgba_func_ptr fetch_rgba =177util_format_fetch_rgba_func(output_format);178unsigned output_format_size;179unsigned output_normalized = 0;180181if (!output_format_desc182|| !fetch_rgba183|| !output_format_pack->pack_rgba_float184|| output_format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB185|| output_format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN186|| !translate_is_output_format_supported(output_format))187continue;188189for(i = 0; i < output_format_desc->nr_channels; ++i)190{191if(output_format_desc->channel[i].type != UTIL_FORMAT_TYPE_FLOAT)192output_normalized |= (1 << output_format_desc->channel[i].normalized);193}194195output_format_size = util_format_get_stride(output_format, 1);196197for (input_format = 1; input_format < PIPE_FORMAT_COUNT; ++input_format)198{199const struct util_format_description* input_format_desc = util_format_description(input_format);200const struct util_format_pack_description* input_format_pack = util_format_pack_description(input_format);201util_format_fetch_rgba_func_ptr fetch_rgba =202util_format_fetch_rgba_func(input_format);203unsigned input_format_size;204struct translate* translate[2];205unsigned fail = 0;206unsigned used_generic = 0;207unsigned input_normalized = 0;208boolean input_is_float = FALSE;209210if (!input_format_desc211|| !fetch_rgba212|| !input_format_pack->pack_rgba_float213|| input_format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB214|| input_format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN215|| !translate_is_output_format_supported(input_format))216continue;217218input_format_size = util_format_get_stride(input_format, 1);219220for(i = 0; i < input_format_desc->nr_channels; ++i)221{222if(input_format_desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)223{224input_is_float = 1;225input_normalized |= 1 << 1;226}227else228input_normalized |= (1 << input_format_desc->channel[i].normalized);229}230231if(((input_normalized | output_normalized) == 3)232|| ((input_normalized & 1) && (output_normalized & 1)233&& input_format_size * output_format_desc->nr_channels > output_format_size * input_format_desc->nr_channels))234continue;235236key.element[0].input_format = input_format;237key.element[0].output_format = output_format;238key.output_stride = output_format_size;239translate[0] = create_fn(&key);240if (!translate[0])241continue;242243key.element[0].input_format = output_format;244key.element[0].output_format = input_format;245key.output_stride = input_format_size;246translate[1] = create_fn(&key);247if(!translate[1])248{249used_generic = 1;250translate[1] = translate_generic_create(&key);251if(!translate[1])252continue;253}254255for(i = 1; i < 5; ++i)256memset(buffer[i], 0xcd - (0x22 * i), 4096);257258if(input_is_float && input_format_desc->channel[0].size == 32)259buffer[0] = (unsigned char*)float_buffer;260else if(input_is_float && input_format_desc->channel[0].size == 64)261buffer[0] = (unsigned char*)double_buffer;262else if(input_is_float && input_format_desc->channel[0].size == 16)263buffer[0] = (unsigned char*)half_buffer;264else if(input_is_float)265abort();266else267buffer[0] = byte_buffer;268269translate[0]->set_buffer(translate[0], 0, buffer[0], input_format_size, count - 1);270translate[0]->run_elts(translate[0], elts, count, 0, 0, buffer[1]);271translate[1]->set_buffer(translate[1], 0, buffer[1], output_format_size, count - 1);272translate[1]->run_elts(translate[1], elts, count, 0, 0, buffer[2]);273translate[0]->set_buffer(translate[0], 0, buffer[2], input_format_size, count - 1);274translate[0]->run_elts(translate[0], elts, count, 0, 0, buffer[3]);275translate[1]->set_buffer(translate[1], 0, buffer[3], output_format_size, count - 1);276translate[1]->run_elts(translate[1], elts, count, 0, 0, buffer[4]);277278for (i = 0; i < count; ++i)279{280float a[4];281float b[4];282fetch_rgba(a, buffer[2] + i * input_format_size, 0, 0);283fetch_rgba(b, buffer[4] + i * input_format_size, 0, 0);284285for (j = 0; j < count; ++j)286{287float d = a[j] - b[j];288if (d > error || d < -error)289{290fail = 1;291break;292}293}294}295296printf("%s%s: %s -> %s -> %s -> %s -> %s\n",297fail ? "FAIL" : "PASS",298used_generic ? "[GENERIC]" : "",299input_format_desc->name, output_format_desc->name, input_format_desc->name, output_format_desc->name, input_format_desc->name);300301if (1)302{303for (i = 0; i < ARRAY_SIZE(buffer); ++i)304{305unsigned format_size = (i & 1) ? output_format_size : input_format_size;306printf("%c ", (i == 2 || i == 4) ? '*' : ' ');307for (j = 0; j < count; ++j)308{309for (k = 0; k < format_size; ++k)310{311printf("%02x", buffer[i][j * format_size + k]);312}313printf(" ");314}315printf("\n");316}317}318319if (!fail)320++passed;321++total;322323if(translate[1])324translate[1]->release(translate[1]);325translate[0]->release(translate[0]);326}327}328329printf("%u/%u tests passed for translate_%s\n", passed, total, argv[1]);330return passed != total;331}332333334