Path: blob/21.2-virgl/src/util/format/u_format_bptc.c
7197 views
/**************************************************************************1*2* Copyright (C) 1999-2007 Brian Paul All Rights Reserved.3* Copyright (c) 2008 VMware, Inc.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included13* in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS16* OR 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 OR19* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,20* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR21* OTHER DEALINGS IN THE SOFTWARE.22*23**************************************************************************/2425#include "util/format/u_format.h"26#include "util/format/u_format_bptc.h"27#include "u_format_pack.h"28#include "util/format_srgb.h"29#include "util/u_math.h"3031#define BPTC_BLOCK_DECODE32#include "../../mesa/main/texcompress_bptc_tmp.h"3334void35util_format_bptc_rgba_unorm_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,36const uint8_t *restrict src_row, unsigned src_stride,37unsigned width, unsigned height)38{39decompress_rgba_unorm(width, height,40src_row, src_stride,41dst_row, dst_stride);42}4344void45util_format_bptc_rgba_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,46const uint8_t *restrict src_row, unsigned src_stride,47unsigned width, unsigned height)48{49compress_rgba_unorm(width, height,50src_row, src_stride,51dst_row, dst_stride);52}5354void55util_format_bptc_rgba_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,56const uint8_t *restrict src_row, unsigned src_stride,57unsigned width, unsigned height)58{59uint8_t *temp_block;60temp_block = malloc(width * height * 4 * sizeof(uint8_t));61decompress_rgba_unorm(width, height,62src_row, src_stride,63temp_block, width * 4 * sizeof(uint8_t));64/* Direct call to row unpack instead of util_format_rgba_unpack_rect()65* to avoid table lookup that would pull in all unpack symbols.66*/67for (int y = 0; y < height; y++) {68util_format_r8g8b8a8_unorm_unpack_rgba_float((char *)dst_row + dst_stride * y,69temp_block + 4 * width * y,70width);71}72free((void *) temp_block);73}7475void76util_format_bptc_rgba_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,77const float *restrict src_row, unsigned src_stride,78unsigned width, unsigned height)79{80uint8_t *temp_block;81temp_block = malloc(width * height * 4 * sizeof(uint8_t));82/* Direct call to row unpack instead of util_format_rgba_unpack_rect()83* to avoid table lookup that would pull in all unpack symbols.84*/85for (int y = 0; y < height; y++) {86util_format_r32g32b32a32_float_unpack_rgba_8unorm(87temp_block + 4 * width * y,88(uint8_t *)src_row + src_stride * y,89width);90}91compress_rgba_unorm(width, height,92temp_block, width * 4 * sizeof(uint8_t),93dst_row, dst_stride);94free((void *) temp_block);95}9697void98util_format_bptc_rgba_unorm_fetch_rgba(void *restrict dst, const uint8_t *restrict src,99unsigned width, unsigned height)100{101uint8_t temp_block[4];102103fetch_rgba_unorm_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,104temp_block, (width % 4) + (height % 4) * 4);105106util_format_read_4(PIPE_FORMAT_R8G8B8A8_UNORM,107dst, 4 * sizeof(float),108temp_block, 4 * sizeof(uint8_t),1090, 0, 1, 1);110}111112void113util_format_bptc_srgba_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,114const uint8_t *restrict src_row, unsigned src_stride,115unsigned width, unsigned height)116{117decompress_rgba_unorm(width, height,118src_row, src_stride,119dst_row, dst_stride);120}121122void123util_format_bptc_srgba_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,124const uint8_t *restrict src_row, unsigned src_stride,125unsigned width, unsigned height)126{127compress_rgba_unorm(width, height,128src_row, src_stride,129dst_row, dst_stride);130}131132void133util_format_bptc_srgba_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,134const uint8_t *restrict src_row, unsigned src_stride,135unsigned width, unsigned height)136{137uint8_t *temp_block;138temp_block = malloc(width * height * 4 * sizeof(uint8_t));139decompress_rgba_unorm(width, height,140src_row, src_stride,141temp_block, width * 4 * sizeof(uint8_t));142143/* Direct call to row unpack instead of util_format_rgba_unpack_rect()144* to avoid table lookup that would pull in all unpack symbols.145*/146for (int y = 0; y < height; y++) {147util_format_r8g8b8a8_srgb_unpack_rgba_float((char *)dst_row + dst_stride * y,148temp_block + width * 4 * y,149width);150}151152free((void *) temp_block);153}154155void156util_format_bptc_srgba_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,157const float *restrict src_row, unsigned src_stride,158unsigned width, unsigned height)159{160compress_rgb_float(width, height,161src_row, src_stride,162dst_row, dst_stride,163true);164}165166void167util_format_bptc_srgba_fetch_rgba(void *restrict dst, const uint8_t *restrict src,168unsigned width, unsigned height)169{170uint8_t temp_block[4];171172fetch_rgba_unorm_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,173temp_block, (width % 4) + (height % 4) * 4);174util_format_r8g8b8a8_srgb_fetch_rgba(dst, temp_block, 0, 0);175}176177void178util_format_bptc_rgb_float_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,179const uint8_t *restrict src_row, unsigned src_stride,180unsigned width, unsigned height)181{182float *temp_block;183temp_block = malloc(width * height * 4 * sizeof(float));184decompress_rgb_float(width, height,185src_row, src_stride,186temp_block, width * 4 * sizeof(float),187true);188/* Direct call to row unpack instead of util_format_rgba_unpack_rect()189* to avoid table lookup that would pull in all unpack symbols.190*/191for (int y = 0; y < height; y++) {192util_format_r32g32b32a32_float_unpack_rgba_8unorm(193dst_row + dst_stride * y,194(const uint8_t *)temp_block + width * 4 * sizeof(float) * y,195width);196}197free((void *) temp_block);198}199200void201util_format_bptc_rgb_float_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,202const uint8_t *restrict src_row, unsigned src_stride,203unsigned width, unsigned height)204{205compress_rgba_unorm(width, height,206src_row, src_stride,207dst_row, dst_stride);208}209210void211util_format_bptc_rgb_float_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,212const uint8_t *restrict src_row, unsigned src_stride,213unsigned width, unsigned height)214{215decompress_rgb_float(width, height,216src_row, src_stride,217dst_row, dst_stride,218true);219}220221void222util_format_bptc_rgb_float_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,223const float *restrict src_row, unsigned src_stride,224unsigned width, unsigned height)225{226compress_rgb_float(width, height,227src_row, src_stride,228dst_row, dst_stride,229true);230}231232void233util_format_bptc_rgb_float_fetch_rgba(void *restrict dst, const uint8_t *restrict src,234unsigned width, unsigned height)235{236fetch_rgb_float_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,237dst, (width % 4) + (height % 4) * 4, true);238}239240void241util_format_bptc_rgb_ufloat_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,242const uint8_t *restrict src_row, unsigned src_stride,243unsigned width, unsigned height)244{245float *temp_block;246temp_block = malloc(width * height * 4 * sizeof(float));247decompress_rgb_float(width, height,248src_row, src_stride,249temp_block, width * 4 * sizeof(float),250false);251/* Direct call to row unpack instead of util_format_rgba_unpack_8unorm()252* to avoid table lookup that would pull in all unpack symbols.253*/254for (int y = 0; y < height; y++) {255util_format_r32g32b32a32_float_unpack_rgba_8unorm(dst_row + dst_stride * y,256(void *)(temp_block + 4 * width * y),257width);258}259free((void *) temp_block);260}261262void263util_format_bptc_rgb_ufloat_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,264const uint8_t *restrict src_row, unsigned src_stride,265unsigned width, unsigned height)266{267compress_rgba_unorm(width, height,268src_row, src_stride,269dst_row, dst_stride);270}271272void273util_format_bptc_rgb_ufloat_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,274const uint8_t *restrict src_row, unsigned src_stride,275unsigned width, unsigned height)276{277decompress_rgb_float(width, height,278src_row, src_stride,279dst_row, dst_stride,280false);281}282283void284util_format_bptc_rgb_ufloat_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,285const float *restrict src_row, unsigned src_stride,286unsigned width, unsigned height)287{288compress_rgb_float(width, height,289src_row, src_stride,290dst_row, dst_stride,291false);292}293294void295util_format_bptc_rgb_ufloat_fetch_rgba(void *restrict dst, const uint8_t *restrict src,296unsigned width, unsigned height)297{298fetch_rgb_float_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,299dst, (width % 4) + (height % 4) * 4, false);300}301302303