u_format.h revision ff6cf60cb80c3bc31d9a4307c32185f65054bc58
1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/************************************************************************** 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright 2009-2010 Vmware, Inc. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All Rights Reserved. 503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) * 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining a 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * copy of this software and associated documentation files (the 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * "Software"), to deal in the Software without restriction, including 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * without limitation the rights to use, copy, modify, merge, publish, 1003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) * distribute, sub license, and/or sell copies of the Software, and to 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * permit persons to whom the Software is furnished to do so, subject to 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the following conditions: 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) * The above copyright notice and this permission notice (including the 155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * next paragraph) shall be included in all copies or substantial portions 165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * of the Software. 17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch * 18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) **************************************************************************/ 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef U_FORMAT_H 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define U_FORMAT_H 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pipe/p_format.h" 3403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "util/u_debug.h" 3503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef __cplusplus 371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciextern "C" { 381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif 39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/** 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Describe how to pack/unpack pixels into/from the prescribed format. 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * XXX: This could be renamed to something like util_format_pack, or broke down 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * in flags inside util_format_block that said exactly what we want. 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum util_format_layout { 4803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) /** 4903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) * Formats with util_format_block::width == util_format_block::height == 1 5003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) * that can be described as an ordinary data structure. 5103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) */ 5203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) UTIL_FORMAT_LAYOUT_PLAIN = 0, 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Formats with sub-sampled channels. 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This is for formats like YV12 where there is less than one sample per 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * pixel. 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_LAYOUT_SUBSAMPLED = 3, 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * S3 Texture Compression formats. 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_LAYOUT_S3TC = 4, 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Red-Green Texture Compression formats. 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_LAYOUT_RGTC = 5, 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Everything else that doesn't fit in any of the above layouts. 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch UTIL_FORMAT_LAYOUT_OTHER = 6 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct util_format_block 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){ 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** Block width in pixels */ 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned width; 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** Block height in pixels */ 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned height; 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** Block size in bits */ 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned bits; 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum util_format_type { 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_TYPE_VOID = 0, 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_TYPE_UNSIGNED = 1, 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_TYPE_SIGNED = 2, 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_TYPE_FIXED = 3, 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_TYPE_FLOAT = 4 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum util_format_swizzle { 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_SWIZZLE_X = 0, 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_SWIZZLE_Y = 1, 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_SWIZZLE_Z = 2, 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_SWIZZLE_W = 3, 106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch UTIL_FORMAT_SWIZZLE_0 = 4, 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_SWIZZLE_1 = 5, 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_SWIZZLE_NONE = 6, 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_SWIZZLE_MAX = 7 /**< Number of enums counter (must be last) */ 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum util_format_colorspace { 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_COLORSPACE_RGB = 0, 11503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) UTIL_FORMAT_COLORSPACE_SRGB = 1, 11603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) UTIL_FORMAT_COLORSPACE_YUV = 2, 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTIL_FORMAT_COLORSPACE_ZS = 3 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)struct util_format_channel_description 122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles){ 1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned type:6; /**< UTIL_FORMAT_TYPE_x */ 1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned normalized:1; 1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned size:9; /**< bits per channel */ 1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}; 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct util_format_description 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){ 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum pipe_format format; 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char *name; 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Short name, striped of the prefix, lower case. 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char *short_name; 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Pixel block dimensions. 142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch */ 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) struct util_format_block block; 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum util_format_layout layout; 146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The number of channels. 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned nr_channels:3; 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 153 * Whether all channels have the same number of (whole) bytes. 154 */ 155 unsigned is_array:1; 156 157 /** 158 * Whether the pixel format can be described as a bitfield structure. 159 * 160 * In particular: 161 * - pixel depth must be 8, 16, or 32 bits; 162 * - all channels must be unsigned, signed, or void 163 */ 164 unsigned is_bitmask:1; 165 166 /** 167 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID). 168 */ 169 unsigned is_mixed:1; 170 171 /** 172 * Input channel description. 173 * 174 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats. 175 */ 176 struct util_format_channel_description channel[4]; 177 178 /** 179 * Output channel swizzle. 180 * 181 * The order is either: 182 * - RGBA 183 * - YUV(A) 184 * - ZS 185 * depending on the colorspace. 186 */ 187 unsigned char swizzle[4]; 188 189 /** 190 * Colorspace transformation. 191 */ 192 enum util_format_colorspace colorspace; 193 194 /** 195 * Unpack pixel blocks to R8G8B8A8_UNORM. 196 * Note: strides are in bytes. 197 * 198 * Only defined for non-depth-stencil formats. 199 */ 200 void 201 (*unpack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride, 202 const uint8_t *src, unsigned src_stride, 203 unsigned width, unsigned height); 204 205 /** 206 * Pack pixel blocks from R8G8B8A8_UNORM. 207 * Note: strides are in bytes. 208 * 209 * Only defined for non-depth-stencil formats. 210 */ 211 void 212 (*pack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride, 213 const uint8_t *src, unsigned src_stride, 214 unsigned width, unsigned height); 215 216 /** 217 * Fetch a single pixel (i, j) from a block. 218 * 219 * XXX: Only defined for a very few select formats. 220 */ 221 void 222 (*fetch_rgba_8unorm)(uint8_t *dst, 223 const uint8_t *src, 224 unsigned i, unsigned j); 225 226 /** 227 * Unpack pixel blocks to R32G32B32A32_FLOAT. 228 * Note: strides are in bytes. 229 * 230 * Only defined for non-depth-stencil formats. 231 */ 232 void 233 (*unpack_rgba_float)(float *dst, unsigned dst_stride, 234 const uint8_t *src, unsigned src_stride, 235 unsigned width, unsigned height); 236 237 /** 238 * Pack pixel blocks from R32G32B32A32_FLOAT. 239 * Note: strides are in bytes. 240 * 241 * Only defined for non-depth-stencil formats. 242 */ 243 void 244 (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride, 245 const float *src, unsigned src_stride, 246 unsigned width, unsigned height); 247 248 /** 249 * Fetch a single pixel (i, j) from a block. 250 * 251 * Only defined for non-depth-stencil formats. 252 */ 253 void 254 (*fetch_rgba_float)(float *dst, 255 const uint8_t *src, 256 unsigned i, unsigned j); 257 258 /** 259 * Unpack pixels to Z32_UNORM. 260 * Note: strides are in bytes. 261 * 262 * Only defined for depth formats. 263 */ 264 void 265 (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride, 266 const uint8_t *src, unsigned src_stride, 267 unsigned width, unsigned height); 268 269 /** 270 * Pack pixels from Z32_FLOAT. 271 * Note: strides are in bytes. 272 * 273 * Only defined for depth formats. 274 */ 275 void 276 (*pack_z_32unorm)(uint8_t *dst, unsigned dst_stride, 277 const uint32_t *src, unsigned src_stride, 278 unsigned width, unsigned height); 279 280 /** 281 * Unpack pixels to Z32_FLOAT. 282 * Note: strides are in bytes. 283 * 284 * Only defined for depth formats. 285 */ 286 void 287 (*unpack_z_float)(float *dst, unsigned dst_stride, 288 const uint8_t *src, unsigned src_stride, 289 unsigned width, unsigned height); 290 291 /** 292 * Pack pixels from Z32_FLOAT. 293 * Note: strides are in bytes. 294 * 295 * Only defined for depth formats. 296 */ 297 void 298 (*pack_z_float)(uint8_t *dst, unsigned dst_stride, 299 const float *src, unsigned src_stride, 300 unsigned width, unsigned height); 301 302 /** 303 * Unpack pixels to S8_USCALED. 304 * Note: strides are in bytes. 305 * 306 * Only defined for stencil formats. 307 */ 308 void 309 (*unpack_s_8uscaled)(uint8_t *dst, unsigned dst_stride, 310 const uint8_t *src, unsigned src_stride, 311 unsigned width, unsigned height); 312 313 /** 314 * Pack pixels from S8_USCALED. 315 * Note: strides are in bytes. 316 * 317 * Only defined for stencil formats. 318 */ 319 void 320 (*pack_s_8uscaled)(uint8_t *dst, unsigned dst_stride, 321 const uint8_t *src, unsigned src_stride, 322 unsigned width, unsigned height); 323 324}; 325 326 327extern const struct util_format_description 328util_format_description_table[]; 329 330 331const struct util_format_description * 332util_format_description(enum pipe_format format); 333 334 335/* 336 * Format query functions. 337 */ 338 339static INLINE const char * 340util_format_name(enum pipe_format format) 341{ 342 const struct util_format_description *desc = util_format_description(format); 343 344 assert(desc); 345 if (!desc) { 346 return "PIPE_FORMAT_???"; 347 } 348 349 return desc->name; 350} 351 352static INLINE const char * 353util_format_short_name(enum pipe_format format) 354{ 355 const struct util_format_description *desc = util_format_description(format); 356 357 assert(desc); 358 if (!desc) { 359 return "???"; 360 } 361 362 return desc->short_name; 363} 364 365/** 366 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info. 367 */ 368static INLINE boolean 369util_format_is_plain(enum pipe_format format) 370{ 371 const struct util_format_description *desc = util_format_description(format); 372 373 if (!format) { 374 return FALSE; 375 } 376 377 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE; 378} 379 380static INLINE boolean 381util_format_is_compressed(enum pipe_format format) 382{ 383 const struct util_format_description *desc = util_format_description(format); 384 385 assert(desc); 386 if (!desc) { 387 return FALSE; 388 } 389 390 switch (desc->layout) { 391 case UTIL_FORMAT_LAYOUT_S3TC: 392 case UTIL_FORMAT_LAYOUT_RGTC: 393 /* XXX add other formats in the future */ 394 return TRUE; 395 default: 396 return FALSE; 397 } 398} 399 400static INLINE boolean 401util_format_is_s3tc(enum pipe_format format) 402{ 403 const struct util_format_description *desc = util_format_description(format); 404 405 assert(desc); 406 if (!desc) { 407 return FALSE; 408 } 409 410 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE; 411} 412 413static INLINE boolean 414util_format_is_srgb(enum pipe_format format) 415{ 416 const struct util_format_description *desc = util_format_description(format); 417 return desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB; 418} 419 420static INLINE boolean 421util_format_has_depth(const struct util_format_description *desc) 422{ 423 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS && 424 desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE; 425} 426 427static INLINE boolean 428util_format_has_stencil(const struct util_format_description *desc) 429{ 430 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS && 431 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE; 432} 433 434static INLINE boolean 435util_format_is_depth_or_stencil(enum pipe_format format) 436{ 437 const struct util_format_description *desc = util_format_description(format); 438 439 assert(desc); 440 if (!desc) { 441 return FALSE; 442 } 443 444 return util_format_has_depth(desc) || 445 util_format_has_stencil(desc); 446} 447 448static INLINE boolean 449util_format_is_depth_and_stencil(enum pipe_format format) 450{ 451 const struct util_format_description *desc = util_format_description(format); 452 453 assert(desc); 454 if (!desc) { 455 return FALSE; 456 } 457 458 return util_format_has_depth(desc) && 459 util_format_has_stencil(desc); 460} 461 462 463/** 464 * Give the RGBA colormask of the channels that can be represented in this 465 * format. 466 * 467 * That is, the channels whose values are preserved. 468 */ 469static INLINE unsigned 470util_format_colormask(const struct util_format_description *desc) 471{ 472 unsigned colormask; 473 unsigned chan; 474 475 switch (desc->colorspace) { 476 case UTIL_FORMAT_COLORSPACE_RGB: 477 case UTIL_FORMAT_COLORSPACE_SRGB: 478 case UTIL_FORMAT_COLORSPACE_YUV: 479 colormask = 0; 480 for (chan = 0; chan < 4; ++chan) { 481 if (desc->swizzle[chan] < 4) { 482 colormask |= (1 << chan); 483 } 484 } 485 return colormask; 486 case UTIL_FORMAT_COLORSPACE_ZS: 487 return 0; 488 default: 489 assert(0); 490 return 0; 491 } 492} 493 494 495boolean 496util_format_is_float(enum pipe_format format); 497 498 499boolean 500util_format_is_rgb_no_alpha(enum pipe_format format); 501 502 503boolean 504util_format_is_luminance(enum pipe_format format); 505 506 507boolean 508util_format_is_luminance_alpha(enum pipe_format format); 509 510 511boolean 512util_format_is_intensity(enum pipe_format format); 513 514 515/** 516 * Whether the src format can be blitted to destation format with a simple 517 * memcpy. 518 */ 519boolean 520util_is_format_compatible(const struct util_format_description *src_desc, 521 const struct util_format_description *dst_desc); 522 523/** 524 * Whether the format is supported by Gallium for the given bindings. 525 * This covers S3TC textures and floating-point render targets. 526 */ 527boolean 528util_format_is_supported(enum pipe_format format, unsigned bind); 529 530/** 531 * Whether this format is a rgab8 variant. 532 * 533 * That is, any format that matches the 534 * 535 * PIPE_FORMAT_?8?8?8?8_UNORM 536 */ 537static INLINE boolean 538util_format_is_rgba8_variant(const struct util_format_description *desc) 539{ 540 unsigned chan; 541 542 if(desc->block.width != 1 || 543 desc->block.height != 1 || 544 desc->block.bits != 32) 545 return FALSE; 546 547 for(chan = 0; chan < 4; ++chan) { 548 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED && 549 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID) 550 return FALSE; 551 if(desc->channel[chan].size != 8) 552 return FALSE; 553 } 554 555 return TRUE; 556} 557 558 559/** 560 * Return total bits needed for the pixel format per block. 561 */ 562static INLINE uint 563util_format_get_blocksizebits(enum pipe_format format) 564{ 565 const struct util_format_description *desc = util_format_description(format); 566 567 assert(desc); 568 if (!desc) { 569 return 0; 570 } 571 572 return desc->block.bits; 573} 574 575/** 576 * Return bytes per block (not pixel) for the given format. 577 */ 578static INLINE uint 579util_format_get_blocksize(enum pipe_format format) 580{ 581 uint bits = util_format_get_blocksizebits(format); 582 583 assert(bits % 8 == 0); 584 585 return bits / 8; 586} 587 588static INLINE uint 589util_format_get_blockwidth(enum pipe_format format) 590{ 591 const struct util_format_description *desc = util_format_description(format); 592 593 assert(desc); 594 if (!desc) { 595 return 1; 596 } 597 598 return desc->block.width; 599} 600 601static INLINE uint 602util_format_get_blockheight(enum pipe_format format) 603{ 604 const struct util_format_description *desc = util_format_description(format); 605 606 assert(desc); 607 if (!desc) { 608 return 1; 609 } 610 611 return desc->block.height; 612} 613 614static INLINE unsigned 615util_format_get_nblocksx(enum pipe_format format, 616 unsigned x) 617{ 618 unsigned blockwidth = util_format_get_blockwidth(format); 619 return (x + blockwidth - 1) / blockwidth; 620} 621 622static INLINE unsigned 623util_format_get_nblocksy(enum pipe_format format, 624 unsigned y) 625{ 626 unsigned blockheight = util_format_get_blockheight(format); 627 return (y + blockheight - 1) / blockheight; 628} 629 630static INLINE unsigned 631util_format_get_nblocks(enum pipe_format format, 632 unsigned width, 633 unsigned height) 634{ 635 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height); 636} 637 638static INLINE size_t 639util_format_get_stride(enum pipe_format format, 640 unsigned width) 641{ 642 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format); 643} 644 645static INLINE size_t 646util_format_get_2d_size(enum pipe_format format, 647 size_t stride, 648 unsigned height) 649{ 650 return util_format_get_nblocksy(format, height) * stride; 651} 652 653static INLINE uint 654util_format_get_component_bits(enum pipe_format format, 655 enum util_format_colorspace colorspace, 656 uint component) 657{ 658 const struct util_format_description *desc = util_format_description(format); 659 enum util_format_colorspace desc_colorspace; 660 661 assert(format); 662 if (!format) { 663 return 0; 664 } 665 666 assert(component < 4); 667 668 /* Treat RGB and SRGB as equivalent. */ 669 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) { 670 colorspace = UTIL_FORMAT_COLORSPACE_RGB; 671 } 672 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) { 673 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB; 674 } else { 675 desc_colorspace = desc->colorspace; 676 } 677 678 if (desc_colorspace != colorspace) { 679 return 0; 680 } 681 682 switch (desc->swizzle[component]) { 683 case UTIL_FORMAT_SWIZZLE_X: 684 return desc->channel[0].size; 685 case UTIL_FORMAT_SWIZZLE_Y: 686 return desc->channel[1].size; 687 case UTIL_FORMAT_SWIZZLE_Z: 688 return desc->channel[2].size; 689 case UTIL_FORMAT_SWIZZLE_W: 690 return desc->channel[3].size; 691 default: 692 return 0; 693 } 694} 695 696static INLINE boolean 697util_format_has_alpha(enum pipe_format format) 698{ 699 const struct util_format_description *desc = util_format_description(format); 700 701 assert(format); 702 if (!format) { 703 return FALSE; 704 } 705 706 switch (desc->colorspace) { 707 case UTIL_FORMAT_COLORSPACE_RGB: 708 case UTIL_FORMAT_COLORSPACE_SRGB: 709 return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1; 710 case UTIL_FORMAT_COLORSPACE_YUV: 711 return FALSE; 712 case UTIL_FORMAT_COLORSPACE_ZS: 713 return FALSE; 714 default: 715 assert(0); 716 return FALSE; 717 } 718} 719 720/** 721 * Given a linear RGB colorspace format, return the corresponding SRGB 722 * format, or PIPE_FORMAT_NONE if none. 723 */ 724static INLINE enum pipe_format 725util_format_srgb(enum pipe_format format) 726{ 727 switch (format) { 728 case PIPE_FORMAT_L8_UNORM: 729 return PIPE_FORMAT_L8_SRGB; 730 case PIPE_FORMAT_L8A8_UNORM: 731 return PIPE_FORMAT_L8A8_SRGB; 732 case PIPE_FORMAT_R8G8B8_UNORM: 733 return PIPE_FORMAT_R8G8B8_SRGB; 734 case PIPE_FORMAT_A8B8G8R8_UNORM: 735 return PIPE_FORMAT_A8B8G8R8_SRGB; 736 case PIPE_FORMAT_X8B8G8R8_UNORM: 737 return PIPE_FORMAT_X8B8G8R8_SRGB; 738 case PIPE_FORMAT_B8G8R8A8_UNORM: 739 return PIPE_FORMAT_B8G8R8A8_SRGB; 740 case PIPE_FORMAT_B8G8R8X8_UNORM: 741 return PIPE_FORMAT_B8G8R8X8_SRGB; 742 case PIPE_FORMAT_A8R8G8B8_UNORM: 743 return PIPE_FORMAT_A8R8G8B8_SRGB; 744 case PIPE_FORMAT_X8R8G8B8_UNORM: 745 return PIPE_FORMAT_X8R8G8B8_SRGB; 746 case PIPE_FORMAT_DXT1_RGB: 747 return PIPE_FORMAT_DXT1_SRGB; 748 case PIPE_FORMAT_DXT1_RGBA: 749 return PIPE_FORMAT_DXT1_SRGBA; 750 case PIPE_FORMAT_DXT3_RGBA: 751 return PIPE_FORMAT_DXT3_SRGBA; 752 case PIPE_FORMAT_DXT5_RGBA: 753 return PIPE_FORMAT_DXT5_SRGBA; 754 default: 755 return PIPE_FORMAT_NONE; 756 } 757} 758 759/** 760 * Given an sRGB format, return the corresponding linear colorspace format. 761 * For non sRGB formats, return the format unchanged. 762 */ 763static INLINE enum pipe_format 764util_format_linear(enum pipe_format format) 765{ 766 switch (format) { 767 case PIPE_FORMAT_L8_SRGB: 768 return PIPE_FORMAT_L8_UNORM; 769 case PIPE_FORMAT_L8A8_SRGB: 770 return PIPE_FORMAT_L8A8_UNORM; 771 case PIPE_FORMAT_R8G8B8_SRGB: 772 return PIPE_FORMAT_R8G8B8_UNORM; 773 case PIPE_FORMAT_A8B8G8R8_SRGB: 774 return PIPE_FORMAT_A8B8G8R8_UNORM; 775 case PIPE_FORMAT_X8B8G8R8_SRGB: 776 return PIPE_FORMAT_X8B8G8R8_UNORM; 777 case PIPE_FORMAT_B8G8R8A8_SRGB: 778 return PIPE_FORMAT_B8G8R8A8_UNORM; 779 case PIPE_FORMAT_B8G8R8X8_SRGB: 780 return PIPE_FORMAT_B8G8R8X8_UNORM; 781 case PIPE_FORMAT_A8R8G8B8_SRGB: 782 return PIPE_FORMAT_A8R8G8B8_UNORM; 783 case PIPE_FORMAT_X8R8G8B8_SRGB: 784 return PIPE_FORMAT_X8R8G8B8_UNORM; 785 case PIPE_FORMAT_DXT1_SRGB: 786 return PIPE_FORMAT_DXT1_RGB; 787 case PIPE_FORMAT_DXT1_SRGBA: 788 return PIPE_FORMAT_DXT1_RGBA; 789 case PIPE_FORMAT_DXT3_SRGBA: 790 return PIPE_FORMAT_DXT3_RGBA; 791 case PIPE_FORMAT_DXT5_SRGBA: 792 return PIPE_FORMAT_DXT5_RGBA; 793 default: 794 return format; 795 } 796} 797 798/** 799 * Return the number of components stored. 800 * Formats with block size != 1x1 will always have 1 component (the block). 801 */ 802static INLINE unsigned 803util_format_get_nr_components(enum pipe_format format) 804{ 805 const struct util_format_description *desc = util_format_description(format); 806 return desc->nr_channels; 807} 808 809/* 810 * Format access functions. 811 */ 812 813void 814util_format_read_4f(enum pipe_format format, 815 float *dst, unsigned dst_stride, 816 const void *src, unsigned src_stride, 817 unsigned x, unsigned y, unsigned w, unsigned h); 818 819void 820util_format_write_4f(enum pipe_format format, 821 const float *src, unsigned src_stride, 822 void *dst, unsigned dst_stride, 823 unsigned x, unsigned y, unsigned w, unsigned h); 824 825void 826util_format_read_4ub(enum pipe_format format, 827 uint8_t *dst, unsigned dst_stride, 828 const void *src, unsigned src_stride, 829 unsigned x, unsigned y, unsigned w, unsigned h); 830 831void 832util_format_write_4ub(enum pipe_format format, 833 const uint8_t *src, unsigned src_stride, 834 void *dst, unsigned dst_stride, 835 unsigned x, unsigned y, unsigned w, unsigned h); 836 837/* 838 * Generic format conversion; 839 */ 840 841boolean 842util_format_fits_8unorm(const struct util_format_description *format_desc); 843 844void 845util_format_translate(enum pipe_format dst_format, 846 void *dst, unsigned dst_stride, 847 unsigned dst_x, unsigned dst_y, 848 enum pipe_format src_format, 849 const void *src, unsigned src_stride, 850 unsigned src_x, unsigned src_y, 851 unsigned width, unsigned height); 852 853/* 854 * Swizzle operations. 855 */ 856 857/* Compose two sets of swizzles. 858 * If V is a 4D vector and the function parameters represent functions that 859 * swizzle vector components, this holds: 860 * swz2(swz1(V)) = dst(V) 861 */ 862void util_format_compose_swizzles(const unsigned char swz1[4], 863 const unsigned char swz2[4], 864 unsigned char dst[4]); 865 866void util_format_swizzle_4f(float *dst, const float *src, 867 const unsigned char swz[4]); 868 869void util_format_unswizzle_4f(float *dst, const float *src, 870 const unsigned char swz[4]); 871 872#ifdef __cplusplus 873} // extern "C" { 874#endif 875 876#endif /* ! U_FORMAT_H */ 877