Path: blob/21.2-virgl/src/util/format/u_format_other.c
7197 views
/**************************************************************************1*2* Copyright 2010 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,17* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19* USE OR OTHER DEALINGS IN THE SOFTWARE.20*21* The above copyright notice and this permission notice (including the22* next paragraph) shall be included in all copies or substantial portions23* of the Software.24*25**************************************************************************/262728#include "util/format/u_format_other.h"29#include "util/u_math.h"30#include "util/format_rgb9e5.h"31#include "util/format_r11g11b10f.h"323334void35util_format_r9g9b9e5_float_unpack_rgba_float(void *restrict dst_row,36const uint8_t *restrict src_row,37unsigned width)38{39unsigned x;40float *dst = dst_row;41const uint8_t *src = src_row;42for(x = 0; x < width; x += 1) {43uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);44rgb9e5_to_float3(value, dst);45dst[3] = 1; /* a */46src += 4;47dst += 4;48}49}5051void52util_format_r9g9b9e5_float_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,53const float *restrict src_row, unsigned src_stride,54unsigned width, unsigned height)55{56unsigned x, y;57for(y = 0; y < height; y += 1) {58const float *src = src_row;59uint8_t *dst = dst_row;60for(x = 0; x < width; x += 1) {61uint32_t value = util_cpu_to_le32(float3_to_rgb9e5(src));62*(uint32_t *)dst = value;63src += 4;64dst += 4;65}66dst_row += dst_stride;67src_row += src_stride/sizeof(*src_row);68}69}7071void72util_format_r9g9b9e5_float_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src,73UNUSED unsigned i, UNUSED unsigned j)74{75float *dst = in_dst;76uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);77rgb9e5_to_float3(value, dst);78dst[3] = 1; /* a */79}808182void83util_format_r9g9b9e5_float_unpack_rgba_8unorm(uint8_t *restrict dst_row,84const uint8_t *restrict src_row,85unsigned width)86{87unsigned x;88float p[3];89uint8_t *dst = dst_row;90const uint8_t *src = src_row;91for(x = 0; x < width; x += 1) {92uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);93rgb9e5_to_float3(value, p);94dst[0] = float_to_ubyte(p[0]); /* r */95dst[1] = float_to_ubyte(p[1]); /* g */96dst[2] = float_to_ubyte(p[2]); /* b */97dst[3] = 255; /* a */98src += 4;99dst += 4;100}101}102103104void105util_format_r9g9b9e5_float_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,106const uint8_t *restrict src_row, unsigned src_stride,107unsigned width, unsigned height)108{109unsigned x, y;110float p[3];111for(y = 0; y < height; y += 1) {112const uint8_t *src = src_row;113uint8_t *dst = dst_row;114for(x = 0; x < width; x += 1) {115uint32_t value;116p[0] = ubyte_to_float(src[0]);117p[1] = ubyte_to_float(src[1]);118p[2] = ubyte_to_float(src[2]);119value = util_cpu_to_le32(float3_to_rgb9e5(p));120*(uint32_t *)dst = value;121src += 4;122dst += 4;123}124dst_row += dst_stride;125src_row += src_stride/sizeof(*src_row);126}127}128129130void131util_format_r11g11b10_float_unpack_rgba_float(void *restrict dst_row,132const uint8_t *restrict src_row,133unsigned width)134{135unsigned x;136float *dst = dst_row;137const uint8_t *src = src_row;138for(x = 0; x < width; x += 1) {139uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);140r11g11b10f_to_float3(value, dst);141dst[3] = 1; /* a */142src += 4;143dst += 4;144}145}146147void148util_format_r11g11b10_float_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,149const float *restrict src_row, unsigned src_stride,150unsigned width, unsigned height)151{152unsigned x, y;153for(y = 0; y < height; y += 1) {154const float *src = src_row;155uint8_t *dst = dst_row;156for(x = 0; x < width; x += 1) {157uint32_t value = util_cpu_to_le32(float3_to_r11g11b10f(src));158*(uint32_t *)dst = value;159src += 4;160dst += 4;161}162dst_row += dst_stride;163src_row += src_stride/sizeof(*src_row);164}165}166167void168util_format_r11g11b10_float_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src,169UNUSED unsigned i, UNUSED unsigned j)170{171float *dst = in_dst;172uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);173r11g11b10f_to_float3(value, dst);174dst[3] = 1; /* a */175}176177178void179util_format_r11g11b10_float_unpack_rgba_8unorm(uint8_t *restrict dst_row,180const uint8_t *restrict src_row,181unsigned width)182{183unsigned x;184float p[3];185uint8_t *dst = dst_row;186const uint8_t *src = src_row;187for(x = 0; x < width; x += 1) {188uint32_t value = util_cpu_to_le32(*(const uint32_t *)src);189r11g11b10f_to_float3(value, p);190dst[0] = float_to_ubyte(p[0]); /* r */191dst[1] = float_to_ubyte(p[1]); /* g */192dst[2] = float_to_ubyte(p[2]); /* b */193dst[3] = 255; /* a */194src += 4;195dst += 4;196}197}198199200void201util_format_r11g11b10_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{205unsigned x, y;206float p[3];207for(y = 0; y < height; y += 1) {208const uint8_t *src = src_row;209uint8_t *dst = dst_row;210for(x = 0; x < width; x += 1) {211uint32_t value;212p[0] = ubyte_to_float(src[0]);213p[1] = ubyte_to_float(src[1]);214p[2] = ubyte_to_float(src[2]);215value = util_cpu_to_le32(float3_to_r11g11b10f(p));216*(uint32_t *)dst = value;217src += 4;218dst += 4;219}220dst_row += dst_stride;221src_row += src_stride/sizeof(*src_row);222}223}224225/*226* PIPE_FORMAT_R8G8Bx_SNORM227*228* A.k.a. D3DFMT_CxV8U8229*/230231static uint8_t232r8g8bx_derive(int16_t r, int16_t g)233{234/* Derive blue from red and green components.235* Apparently, we must always use integers to perform calculations,236* otherwise the results won't match D3D's CxV8U8 definition.237*/238return (uint8_t)sqrtf(0x7f * 0x7f - r * r - g * g) * 0xff / 0x7f;239}240241void242util_format_r8g8bx_snorm_unpack_rgba_float(void *restrict dst_row,243const uint8_t *restrict src_row, unsigned width)244{245unsigned x;246float *dst = dst_row;247const uint16_t *src = (const uint16_t *)src_row;248for(x = 0; x < width; x += 1) {249uint16_t value = util_cpu_to_le16(*src++);250int16_t r, g;251252r = ((int16_t)(value << 8)) >> 8;253g = ((int16_t)(value << 0)) >> 8;254255dst[0] = (float)(r * (1.0f/0x7f)); /* r */256dst[1] = (float)(g * (1.0f/0x7f)); /* g */257dst[2] = r8g8bx_derive(r, g) * (1.0f/0xff); /* b */258dst[3] = 1.0f; /* a */259dst += 4;260}261}262263264void265util_format_r8g8bx_snorm_unpack_rgba_8unorm(uint8_t *restrict dst,266const uint8_t *restrict src_row,267unsigned width)268{269unsigned x;270const uint16_t *src = (const uint16_t *)src_row;271for(x = 0; x < width; x += 1) {272uint16_t value = util_cpu_to_le16(*src++);273int16_t r, g;274275r = ((int16_t)(value << 8)) >> 8;276g = ((int16_t)(value << 0)) >> 8;277278dst[0] = (uint8_t)(((uint16_t)MAX2(r, 0)) * 0xff / 0x7f); /* r */279dst[1] = (uint8_t)(((uint16_t)MAX2(g, 0)) * 0xff / 0x7f); /* g */280dst[2] = r8g8bx_derive(r, g); /* b */281dst[3] = 255; /* a */282dst += 4;283}284}285286287void288util_format_r8g8bx_snorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,289const float *restrict src_row, unsigned src_stride,290unsigned width, unsigned height)291{292unsigned x, y;293for(y = 0; y < height; y += 1) {294const float *src = src_row;295uint16_t *dst = (uint16_t *)dst_row;296for(x = 0; x < width; x += 1) {297uint16_t value = 0;298299value |= (uint16_t)(((int8_t)(CLAMP(src[0], -1, 1) * 0x7f)) & 0xff) ;300value |= (uint16_t)((((int8_t)(CLAMP(src[1], -1, 1) * 0x7f)) & 0xff) << 8) ;301302*dst++ = util_le16_to_cpu(value);303304src += 4;305}306dst_row += dst_stride;307src_row += src_stride/sizeof(*src_row);308}309}310311312void313util_format_r8g8bx_snorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,314const uint8_t *restrict src_row, unsigned src_stride,315unsigned width, unsigned height)316{317unsigned x, y;318319for(y = 0; y < height; y += 1) {320const uint8_t *src = src_row;321uint16_t *dst = (uint16_t *)dst_row;322for(x = 0; x < width; x += 1) {323uint16_t value = 0;324325value |= src[0] >> 1;326value |= (src[1] >> 1) << 8;327328*dst++ = util_le16_to_cpu(value);329330src += 4;331}332dst_row += dst_stride;333src_row += src_stride/sizeof(*src_row);334}335}336337338void339util_format_r8g8bx_snorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src,340UNUSED unsigned i, UNUSED unsigned j)341{342float *dst = in_dst;343uint16_t value = util_cpu_to_le16(*(const uint16_t *)src);344int16_t r, g;345346r = ((int16_t)(value << 8)) >> 8;347g = ((int16_t)(value << 0)) >> 8;348349dst[0] = r * (1.0f/0x7f); /* r */350dst[1] = g * (1.0f/0x7f); /* g */351dst[2] = r8g8bx_derive(r, g) * (1.0f/0xff); /* b */352dst[3] = 1.0f; /* a */353}354355356