u_format.h revision eb926ddf9eee1095c7fc12013f0b8375bbaeca6f
1/************************************************************************** 2 * 3 * Copyright 2009 Vmware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28 29#ifndef U_FORMAT_H 30#define U_FORMAT_H 31 32 33#include "pipe/p_format.h" 34 35 36enum util_format_layout { 37 UTIL_FORMAT_LAYOUT_PLAIN = 0, /*< RGB, depth-stencil */ 38 UTIL_FORMAT_LAYOUT_DXT = 1 39}; 40 41 42struct util_format_block 43{ 44 /** Block width in pixels */ 45 unsigned width; 46 47 /** Block height in pixels */ 48 unsigned height; 49 50 /** Block size in bits */ 51 unsigned bits; 52}; 53 54 55enum util_format_type { 56 UTIL_FORMAT_TYPE_VOID = 0, 57 UTIL_FORMAT_TYPE_UNSIGNED = 1, 58 UTIL_FORMAT_TYPE_SIGNED = 2, 59 UTIL_FORMAT_TYPE_FIXED = 3, 60 UTIL_FORMAT_TYPE_FLOAT = 4 61}; 62 63 64enum util_format_swizzle { 65 UTIL_FORMAT_SWIZZLE_X = 0, 66 UTIL_FORMAT_SWIZZLE_Y = 1, 67 UTIL_FORMAT_SWIZZLE_Z = 2, 68 UTIL_FORMAT_SWIZZLE_W = 3, 69 UTIL_FORMAT_SWIZZLE_0 = 4, 70 UTIL_FORMAT_SWIZZLE_1 = 5, 71 UTIL_FORMAT_SWIZZLE_NONE = 6 72}; 73 74 75enum util_format_colorspace { 76 UTIL_FORMAT_COLORSPACE_RGB = 0, 77 UTIL_FORMAT_COLORSPACE_SRGB = 1, 78 UTIL_FORMAT_COLORSPACE_YUV = 2, 79 UTIL_FORMAT_COLORSPACE_ZS = 3, 80}; 81 82 83struct util_format_channel_description 84{ 85 unsigned type:6; 86 unsigned normalized:1; 87 unsigned size:9; 88}; 89 90 91struct util_format_description 92{ 93 enum pipe_format format; 94 const char *name; 95 struct util_format_block block; 96 enum util_format_layout layout; 97 struct util_format_channel_description channel[4]; 98 unsigned char swizzle[4]; 99 enum util_format_colorspace colorspace; 100}; 101 102 103extern const struct util_format_description 104util_format_description_table[]; 105 106 107const struct util_format_description * 108util_format_description(enum pipe_format format); 109 110 111/* 112 * Format query functions. 113 */ 114 115static INLINE boolean 116util_format_is_compressed(enum pipe_format format) 117{ 118 const struct util_format_description *desc = util_format_description(format); 119 120 assert(format); 121 if (!format) { 122 return FALSE; 123 } 124 125 return desc->layout == UTIL_FORMAT_LAYOUT_DXT ? TRUE : FALSE; 126} 127 128static INLINE boolean 129util_format_is_depth_or_stencil(enum pipe_format format) 130{ 131 const struct util_format_description *desc = util_format_description(format); 132 133 assert(format); 134 if (!format) { 135 return FALSE; 136 } 137 138 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE; 139} 140 141static INLINE boolean 142util_format_is_depth_and_stencil(enum pipe_format format) 143{ 144 const struct util_format_description *desc = util_format_description(format); 145 146 assert(format); 147 if (!format) { 148 return FALSE; 149 } 150 151 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) { 152 return FALSE; 153 } 154 155 return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE && 156 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE; 157} 158 159/** 160 * Describe pixel format's block. 161 * 162 * @sa http://msdn2.microsoft.com/en-us/library/ms796147.aspx 163 */ 164static INLINE void 165util_format_get_block(enum pipe_format format, 166 struct pipe_format_block *block) 167{ 168 const struct util_format_description *desc = util_format_description(format); 169 170 assert(format); 171 if (!format) { 172 block->size = 0; 173 block->width = 1; 174 block->height = 1; 175 return; 176 } 177 178 block->size = desc->block.bits / 8; 179 block->width = desc->block.width; 180 block->height = desc->block.height; 181} 182 183/** 184 * Return total bits needed for the pixel format. 185 */ 186static INLINE uint 187util_format_get_bits(enum pipe_format format) 188{ 189 const struct util_format_description *desc = util_format_description(format); 190 191 assert(format); 192 if (!format) { 193 return 0; 194 } 195 196 return desc->block.bits / (desc->block.width * desc->block.height); 197} 198 199/** 200 * Return bytes per pixel for the given format. 201 */ 202static INLINE uint 203util_format_get_size(enum pipe_format format) 204{ 205 uint bits = util_format_get_bits(format); 206 207 assert(bits % 8 == 0); 208 209 return bits / 8; 210} 211 212static INLINE uint 213util_format_get_component_bits(enum pipe_format format, 214 enum util_format_colorspace colorspace, 215 uint component) 216{ 217 const struct util_format_description *desc = util_format_description(format); 218 enum util_format_colorspace desc_colorspace; 219 220 assert(format); 221 if (!format) { 222 return 0; 223 } 224 225 assert(component >= 4); 226 227 /* Treat RGB and SRGB as equivalent. */ 228 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) { 229 colorspace = UTIL_FORMAT_COLORSPACE_RGB; 230 } 231 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) { 232 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB; 233 } else { 234 desc_colorspace = desc->colorspace; 235 } 236 237 if (desc_colorspace != colorspace) { 238 return 0; 239 } 240 241 switch (desc->swizzle[component]) { 242 case UTIL_FORMAT_SWIZZLE_X: 243 return desc->channel[0].size; 244 case UTIL_FORMAT_SWIZZLE_Y: 245 return desc->channel[1].size; 246 case UTIL_FORMAT_SWIZZLE_Z: 247 return desc->channel[2].size; 248 case UTIL_FORMAT_SWIZZLE_W: 249 return desc->channel[3].size; 250 default: 251 return 0; 252 } 253} 254 255 256/* 257 * Format access functions. 258 */ 259 260void 261util_format_read_4f(enum pipe_format format, 262 float *dst, unsigned dst_stride, 263 const void *src, unsigned src_stride, 264 unsigned x, unsigned y, unsigned w, unsigned h); 265 266void 267util_format_write_4f(enum pipe_format format, 268 const float *src, unsigned src_stride, 269 void *dst, unsigned dst_stride, 270 unsigned x, unsigned y, unsigned w, unsigned h); 271 272void 273util_format_read_4ub(enum pipe_format format, 274 uint8_t *dst, unsigned dst_stride, 275 const void *src, unsigned src_stride, 276 unsigned x, unsigned y, unsigned w, unsigned h); 277 278void 279util_format_write_4ub(enum pipe_format format, 280 const uint8_t *src, unsigned src_stride, 281 void *dst, unsigned dst_stride, 282 unsigned x, unsigned y, unsigned w, unsigned h); 283 284#endif /* ! U_FORMAT_H */ 285