u_gen_mipmap.c revision 9c8db8685432fedd068157795422764ce96b89a0
1/************************************************************************** 2 * 3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * Copyright 2008 VMware, Inc. All rights reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 29/** 30 * @file 31 * Mipmap generation utility 32 * 33 * @author Brian Paul 34 */ 35 36 37#include "pipe/p_context.h" 38#include "pipe/p_debug.h" 39#include "pipe/p_defines.h" 40#include "pipe/p_inlines.h" 41#include "pipe/p_winsys.h" 42#include "pipe/p_shader_tokens.h" 43 44#include "util/u_memory.h" 45#include "util/u_draw_quad.h" 46#include "util/u_gen_mipmap.h" 47#include "util/u_simple_shaders.h" 48 49#include "tgsi/tgsi_build.h" 50#include "tgsi/tgsi_dump.h" 51#include "tgsi/tgsi_parse.h" 52 53#include "cso_cache/cso_context.h" 54 55 56struct gen_mipmap_state 57{ 58 struct pipe_context *pipe; 59 struct cso_context *cso; 60 61 struct pipe_blend_state blend; 62 struct pipe_depth_stencil_alpha_state depthstencil; 63 struct pipe_rasterizer_state rasterizer; 64 struct pipe_sampler_state sampler; 65 struct pipe_viewport_state viewport; 66 67 struct pipe_shader_state vert_shader; 68 struct pipe_shader_state frag_shader; 69 void *vs; 70 void *fs; 71 72 struct pipe_buffer *vbuf; /**< quad vertices */ 73 unsigned vbuf_slot; 74 75 float vertices[4][2][4]; /**< vertex/texcoords for quad */ 76}; 77 78 79 80enum dtype 81{ 82 UBYTE, 83 UBYTE_3_3_2, 84 USHORT, 85 USHORT_4_4_4_4, 86 USHORT_5_6_5, 87 USHORT_1_5_5_5_REV, 88 UINT, 89 FLOAT, 90 HALF_FLOAT 91}; 92 93 94typedef ushort half_float; 95 96 97static half_float 98float_to_half(float f) 99{ 100 /* XXX fix this */ 101 return 0; 102} 103 104static float 105half_to_float(half_float h) 106{ 107 /* XXX fix this */ 108 return 0.0f; 109} 110 111 112 113 114/** 115 * \name Support macros for do_row and do_row_3d 116 * 117 * The macro madness is here for two reasons. First, it compacts the code 118 * slightly. Second, it makes it much easier to adjust the specifics of the 119 * filter to tune the rounding characteristics. 120 */ 121/*@{*/ 122#define DECLARE_ROW_POINTERS(t, e) \ 123 const t(*rowA)[e] = (const t(*)[e]) srcRowA; \ 124 const t(*rowB)[e] = (const t(*)[e]) srcRowB; \ 125 const t(*rowC)[e] = (const t(*)[e]) srcRowC; \ 126 const t(*rowD)[e] = (const t(*)[e]) srcRowD; \ 127 t(*dst)[e] = (t(*)[e]) dstRow 128 129#define DECLARE_ROW_POINTERS0(t) \ 130 const t *rowA = (const t *) srcRowA; \ 131 const t *rowB = (const t *) srcRowB; \ 132 const t *rowC = (const t *) srcRowC; \ 133 const t *rowD = (const t *) srcRowD; \ 134 t *dst = (t *) dstRow 135 136#define FILTER_SUM_3D(Aj, Ak, Bj, Bk, Cj, Ck, Dj, Dk) \ 137 ((unsigned) Aj + (unsigned) Ak \ 138 + (unsigned) Bj + (unsigned) Bk \ 139 + (unsigned) Cj + (unsigned) Ck \ 140 + (unsigned) Dj + (unsigned) Dk \ 141 + 4) >> 3 142 143#define FILTER_3D(e) \ 144 do { \ 145 dst[i][e] = FILTER_SUM_3D(rowA[j][e], rowA[k][e], \ 146 rowB[j][e], rowB[k][e], \ 147 rowC[j][e], rowC[k][e], \ 148 rowD[j][e], rowD[k][e]); \ 149 } while(0) 150 151#define FILTER_F_3D(e) \ 152 do { \ 153 dst[i][e] = (rowA[j][e] + rowA[k][e] \ 154 + rowB[j][e] + rowB[k][e] \ 155 + rowC[j][e] + rowC[k][e] \ 156 + rowD[j][e] + rowD[k][e]) * 0.125F; \ 157 } while(0) 158 159#define FILTER_HF_3D(e) \ 160 do { \ 161 const float aj = half_to_float(rowA[j][e]); \ 162 const float ak = half_to_float(rowA[k][e]); \ 163 const float bj = half_to_float(rowB[j][e]); \ 164 const float bk = half_to_float(rowB[k][e]); \ 165 const float cj = half_to_float(rowC[j][e]); \ 166 const float ck = half_to_float(rowC[k][e]); \ 167 const float dj = half_to_float(rowD[j][e]); \ 168 const float dk = half_to_float(rowD[k][e]); \ 169 dst[i][e] = float_to_half((aj + ak + bj + bk + cj + ck + dj + dk) \ 170 * 0.125F); \ 171 } while(0) 172/*@}*/ 173 174 175/** 176 * Average together two rows of a source image to produce a single new 177 * row in the dest image. It's legal for the two source rows to point 178 * to the same data. The source width must be equal to either the 179 * dest width or two times the dest width. 180 * \param datatype GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, GL_FLOAT, etc. 181 * \param comps number of components per pixel (1..4) 182 */ 183static void 184do_row(enum dtype datatype, uint comps, int srcWidth, 185 const void *srcRowA, const void *srcRowB, 186 int dstWidth, void *dstRow) 187{ 188 const uint k0 = (srcWidth == dstWidth) ? 0 : 1; 189 const uint colStride = (srcWidth == dstWidth) ? 1 : 2; 190 191 assert(comps >= 1); 192 assert(comps <= 4); 193 194 /* This assertion is no longer valid with non-power-of-2 textures 195 assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth); 196 */ 197 198 if (datatype == UBYTE && comps == 4) { 199 uint i, j, k; 200 const ubyte(*rowA)[4] = (const ubyte(*)[4]) srcRowA; 201 const ubyte(*rowB)[4] = (const ubyte(*)[4]) srcRowB; 202 ubyte(*dst)[4] = (ubyte(*)[4]) dstRow; 203 for (i = j = 0, k = k0; i < (uint) dstWidth; 204 i++, j += colStride, k += colStride) { 205 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4; 206 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4; 207 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4; 208 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4; 209 } 210 } 211 else if (datatype == UBYTE && comps == 3) { 212 uint i, j, k; 213 const ubyte(*rowA)[3] = (const ubyte(*)[3]) srcRowA; 214 const ubyte(*rowB)[3] = (const ubyte(*)[3]) srcRowB; 215 ubyte(*dst)[3] = (ubyte(*)[3]) dstRow; 216 for (i = j = 0, k = k0; i < (uint) dstWidth; 217 i++, j += colStride, k += colStride) { 218 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4; 219 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4; 220 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4; 221 } 222 } 223 else if (datatype == UBYTE && comps == 2) { 224 uint i, j, k; 225 const ubyte(*rowA)[2] = (const ubyte(*)[2]) srcRowA; 226 const ubyte(*rowB)[2] = (const ubyte(*)[2]) srcRowB; 227 ubyte(*dst)[2] = (ubyte(*)[2]) dstRow; 228 for (i = j = 0, k = k0; i < (uint) dstWidth; 229 i++, j += colStride, k += colStride) { 230 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) >> 2; 231 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) >> 2; 232 } 233 } 234 else if (datatype == UBYTE && comps == 1) { 235 uint i, j, k; 236 const ubyte *rowA = (const ubyte *) srcRowA; 237 const ubyte *rowB = (const ubyte *) srcRowB; 238 ubyte *dst = (ubyte *) dstRow; 239 for (i = j = 0, k = k0; i < (uint) dstWidth; 240 i++, j += colStride, k += colStride) { 241 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) >> 2; 242 } 243 } 244 245 else if (datatype == USHORT && comps == 4) { 246 uint i, j, k; 247 const ushort(*rowA)[4] = (const ushort(*)[4]) srcRowA; 248 const ushort(*rowB)[4] = (const ushort(*)[4]) srcRowB; 249 ushort(*dst)[4] = (ushort(*)[4]) dstRow; 250 for (i = j = 0, k = k0; i < (uint) dstWidth; 251 i++, j += colStride, k += colStride) { 252 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4; 253 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4; 254 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4; 255 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4; 256 } 257 } 258 else if (datatype == USHORT && comps == 3) { 259 uint i, j, k; 260 const ushort(*rowA)[3] = (const ushort(*)[3]) srcRowA; 261 const ushort(*rowB)[3] = (const ushort(*)[3]) srcRowB; 262 ushort(*dst)[3] = (ushort(*)[3]) dstRow; 263 for (i = j = 0, k = k0; i < (uint) dstWidth; 264 i++, j += colStride, k += colStride) { 265 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4; 266 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4; 267 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4; 268 } 269 } 270 else if (datatype == USHORT && comps == 2) { 271 uint i, j, k; 272 const ushort(*rowA)[2] = (const ushort(*)[2]) srcRowA; 273 const ushort(*rowB)[2] = (const ushort(*)[2]) srcRowB; 274 ushort(*dst)[2] = (ushort(*)[2]) dstRow; 275 for (i = j = 0, k = k0; i < (uint) dstWidth; 276 i++, j += colStride, k += colStride) { 277 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4; 278 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4; 279 } 280 } 281 else if (datatype == USHORT && comps == 1) { 282 uint i, j, k; 283 const ushort *rowA = (const ushort *) srcRowA; 284 const ushort *rowB = (const ushort *) srcRowB; 285 ushort *dst = (ushort *) dstRow; 286 for (i = j = 0, k = k0; i < (uint) dstWidth; 287 i++, j += colStride, k += colStride) { 288 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) / 4; 289 } 290 } 291 292 else if (datatype == FLOAT && comps == 4) { 293 uint i, j, k; 294 const float(*rowA)[4] = (const float(*)[4]) srcRowA; 295 const float(*rowB)[4] = (const float(*)[4]) srcRowB; 296 float(*dst)[4] = (float(*)[4]) dstRow; 297 for (i = j = 0, k = k0; i < (uint) dstWidth; 298 i++, j += colStride, k += colStride) { 299 dst[i][0] = (rowA[j][0] + rowA[k][0] + 300 rowB[j][0] + rowB[k][0]) * 0.25F; 301 dst[i][1] = (rowA[j][1] + rowA[k][1] + 302 rowB[j][1] + rowB[k][1]) * 0.25F; 303 dst[i][2] = (rowA[j][2] + rowA[k][2] + 304 rowB[j][2] + rowB[k][2]) * 0.25F; 305 dst[i][3] = (rowA[j][3] + rowA[k][3] + 306 rowB[j][3] + rowB[k][3]) * 0.25F; 307 } 308 } 309 else if (datatype == FLOAT && comps == 3) { 310 uint i, j, k; 311 const float(*rowA)[3] = (const float(*)[3]) srcRowA; 312 const float(*rowB)[3] = (const float(*)[3]) srcRowB; 313 float(*dst)[3] = (float(*)[3]) dstRow; 314 for (i = j = 0, k = k0; i < (uint) dstWidth; 315 i++, j += colStride, k += colStride) { 316 dst[i][0] = (rowA[j][0] + rowA[k][0] + 317 rowB[j][0] + rowB[k][0]) * 0.25F; 318 dst[i][1] = (rowA[j][1] + rowA[k][1] + 319 rowB[j][1] + rowB[k][1]) * 0.25F; 320 dst[i][2] = (rowA[j][2] + rowA[k][2] + 321 rowB[j][2] + rowB[k][2]) * 0.25F; 322 } 323 } 324 else if (datatype == FLOAT && comps == 2) { 325 uint i, j, k; 326 const float(*rowA)[2] = (const float(*)[2]) srcRowA; 327 const float(*rowB)[2] = (const float(*)[2]) srcRowB; 328 float(*dst)[2] = (float(*)[2]) dstRow; 329 for (i = j = 0, k = k0; i < (uint) dstWidth; 330 i++, j += colStride, k += colStride) { 331 dst[i][0] = (rowA[j][0] + rowA[k][0] + 332 rowB[j][0] + rowB[k][0]) * 0.25F; 333 dst[i][1] = (rowA[j][1] + rowA[k][1] + 334 rowB[j][1] + rowB[k][1]) * 0.25F; 335 } 336 } 337 else if (datatype == FLOAT && comps == 1) { 338 uint i, j, k; 339 const float *rowA = (const float *) srcRowA; 340 const float *rowB = (const float *) srcRowB; 341 float *dst = (float *) dstRow; 342 for (i = j = 0, k = k0; i < (uint) dstWidth; 343 i++, j += colStride, k += colStride) { 344 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) * 0.25F; 345 } 346 } 347 348#if 0 349 else if (datatype == HALF_FLOAT && comps == 4) { 350 uint i, j, k, comp; 351 const half_float(*rowA)[4] = (const half_float(*)[4]) srcRowA; 352 const half_float(*rowB)[4] = (const half_float(*)[4]) srcRowB; 353 half_float(*dst)[4] = (half_float(*)[4]) dstRow; 354 for (i = j = 0, k = k0; i < (uint) dstWidth; 355 i++, j += colStride, k += colStride) { 356 for (comp = 0; comp < 4; comp++) { 357 float aj, ak, bj, bk; 358 aj = half_to_float(rowA[j][comp]); 359 ak = half_to_float(rowA[k][comp]); 360 bj = half_to_float(rowB[j][comp]); 361 bk = half_to_float(rowB[k][comp]); 362 dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F); 363 } 364 } 365 } 366 else if (datatype == HALF_FLOAT && comps == 3) { 367 uint i, j, k, comp; 368 const half_float(*rowA)[3] = (const half_float(*)[3]) srcRowA; 369 const half_float(*rowB)[3] = (const half_float(*)[3]) srcRowB; 370 half_float(*dst)[3] = (half_float(*)[3]) dstRow; 371 for (i = j = 0, k = k0; i < (uint) dstWidth; 372 i++, j += colStride, k += colStride) { 373 for (comp = 0; comp < 3; comp++) { 374 float aj, ak, bj, bk; 375 aj = half_to_float(rowA[j][comp]); 376 ak = half_to_float(rowA[k][comp]); 377 bj = half_to_float(rowB[j][comp]); 378 bk = half_to_float(rowB[k][comp]); 379 dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F); 380 } 381 } 382 } 383 else if (datatype == HALF_FLOAT && comps == 2) { 384 uint i, j, k, comp; 385 const half_float(*rowA)[2] = (const half_float(*)[2]) srcRowA; 386 const half_float(*rowB)[2] = (const half_float(*)[2]) srcRowB; 387 half_float(*dst)[2] = (half_float(*)[2]) dstRow; 388 for (i = j = 0, k = k0; i < (uint) dstWidth; 389 i++, j += colStride, k += colStride) { 390 for (comp = 0; comp < 2; comp++) { 391 float aj, ak, bj, bk; 392 aj = half_to_float(rowA[j][comp]); 393 ak = half_to_float(rowA[k][comp]); 394 bj = half_to_float(rowB[j][comp]); 395 bk = half_to_float(rowB[k][comp]); 396 dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F); 397 } 398 } 399 } 400 else if (datatype == HALF_FLOAT && comps == 1) { 401 uint i, j, k; 402 const half_float *rowA = (const half_float *) srcRowA; 403 const half_float *rowB = (const half_float *) srcRowB; 404 half_float *dst = (half_float *) dstRow; 405 for (i = j = 0, k = k0; i < (uint) dstWidth; 406 i++, j += colStride, k += colStride) { 407 float aj, ak, bj, bk; 408 aj = half_to_float(rowA[j]); 409 ak = half_to_float(rowA[k]); 410 bj = half_to_float(rowB[j]); 411 bk = half_to_float(rowB[k]); 412 dst[i] = float_to_half((aj + ak + bj + bk) * 0.25F); 413 } 414 } 415#endif 416 417 else if (datatype == UINT && comps == 1) { 418 uint i, j, k; 419 const uint *rowA = (const uint *) srcRowA; 420 const uint *rowB = (const uint *) srcRowB; 421 uint *dst = (uint *) dstRow; 422 for (i = j = 0, k = k0; i < (uint) dstWidth; 423 i++, j += colStride, k += colStride) { 424 dst[i] = rowA[j] / 4 + rowA[k] / 4 + rowB[j] / 4 + rowB[k] / 4; 425 } 426 } 427 428 else if (datatype == USHORT_5_6_5 && comps == 3) { 429 uint i, j, k; 430 const ushort *rowA = (const ushort *) srcRowA; 431 const ushort *rowB = (const ushort *) srcRowB; 432 ushort *dst = (ushort *) dstRow; 433 for (i = j = 0, k = k0; i < (uint) dstWidth; 434 i++, j += colStride, k += colStride) { 435 const int rowAr0 = rowA[j] & 0x1f; 436 const int rowAr1 = rowA[k] & 0x1f; 437 const int rowBr0 = rowB[j] & 0x1f; 438 const int rowBr1 = rowB[k] & 0x1f; 439 const int rowAg0 = (rowA[j] >> 5) & 0x3f; 440 const int rowAg1 = (rowA[k] >> 5) & 0x3f; 441 const int rowBg0 = (rowB[j] >> 5) & 0x3f; 442 const int rowBg1 = (rowB[k] >> 5) & 0x3f; 443 const int rowAb0 = (rowA[j] >> 11) & 0x1f; 444 const int rowAb1 = (rowA[k] >> 11) & 0x1f; 445 const int rowBb0 = (rowB[j] >> 11) & 0x1f; 446 const int rowBb1 = (rowB[k] >> 11) & 0x1f; 447 const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2; 448 const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2; 449 const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2; 450 dst[i] = (blue << 11) | (green << 5) | red; 451 } 452 } 453 else if (datatype == USHORT_4_4_4_4 && comps == 4) { 454 uint i, j, k; 455 const ushort *rowA = (const ushort *) srcRowA; 456 const ushort *rowB = (const ushort *) srcRowB; 457 ushort *dst = (ushort *) dstRow; 458 for (i = j = 0, k = k0; i < (uint) dstWidth; 459 i++, j += colStride, k += colStride) { 460 const int rowAr0 = rowA[j] & 0xf; 461 const int rowAr1 = rowA[k] & 0xf; 462 const int rowBr0 = rowB[j] & 0xf; 463 const int rowBr1 = rowB[k] & 0xf; 464 const int rowAg0 = (rowA[j] >> 4) & 0xf; 465 const int rowAg1 = (rowA[k] >> 4) & 0xf; 466 const int rowBg0 = (rowB[j] >> 4) & 0xf; 467 const int rowBg1 = (rowB[k] >> 4) & 0xf; 468 const int rowAb0 = (rowA[j] >> 8) & 0xf; 469 const int rowAb1 = (rowA[k] >> 8) & 0xf; 470 const int rowBb0 = (rowB[j] >> 8) & 0xf; 471 const int rowBb1 = (rowB[k] >> 8) & 0xf; 472 const int rowAa0 = (rowA[j] >> 12) & 0xf; 473 const int rowAa1 = (rowA[k] >> 12) & 0xf; 474 const int rowBa0 = (rowB[j] >> 12) & 0xf; 475 const int rowBa1 = (rowB[k] >> 12) & 0xf; 476 const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2; 477 const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2; 478 const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2; 479 const int alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2; 480 dst[i] = (alpha << 12) | (blue << 8) | (green << 4) | red; 481 } 482 } 483 else if (datatype == USHORT_1_5_5_5_REV && comps == 4) { 484 uint i, j, k; 485 const ushort *rowA = (const ushort *) srcRowA; 486 const ushort *rowB = (const ushort *) srcRowB; 487 ushort *dst = (ushort *) dstRow; 488 for (i = j = 0, k = k0; i < (uint) dstWidth; 489 i++, j += colStride, k += colStride) { 490 const int rowAr0 = rowA[j] & 0x1f; 491 const int rowAr1 = rowA[k] & 0x1f; 492 const int rowBr0 = rowB[j] & 0x1f; 493 const int rowBr1 = rowB[k] & 0x1f; 494 const int rowAg0 = (rowA[j] >> 5) & 0x1f; 495 const int rowAg1 = (rowA[k] >> 5) & 0x1f; 496 const int rowBg0 = (rowB[j] >> 5) & 0x1f; 497 const int rowBg1 = (rowB[k] >> 5) & 0x1f; 498 const int rowAb0 = (rowA[j] >> 10) & 0x1f; 499 const int rowAb1 = (rowA[k] >> 10) & 0x1f; 500 const int rowBb0 = (rowB[j] >> 10) & 0x1f; 501 const int rowBb1 = (rowB[k] >> 10) & 0x1f; 502 const int rowAa0 = (rowA[j] >> 15) & 0x1; 503 const int rowAa1 = (rowA[k] >> 15) & 0x1; 504 const int rowBa0 = (rowB[j] >> 15) & 0x1; 505 const int rowBa1 = (rowB[k] >> 15) & 0x1; 506 const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2; 507 const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2; 508 const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2; 509 const int alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2; 510 dst[i] = (alpha << 15) | (blue << 10) | (green << 5) | red; 511 } 512 } 513 else if (datatype == UBYTE_3_3_2 && comps == 3) { 514 uint i, j, k; 515 const ubyte *rowA = (const ubyte *) srcRowA; 516 const ubyte *rowB = (const ubyte *) srcRowB; 517 ubyte *dst = (ubyte *) dstRow; 518 for (i = j = 0, k = k0; i < (uint) dstWidth; 519 i++, j += colStride, k += colStride) { 520 const int rowAr0 = rowA[j] & 0x3; 521 const int rowAr1 = rowA[k] & 0x3; 522 const int rowBr0 = rowB[j] & 0x3; 523 const int rowBr1 = rowB[k] & 0x3; 524 const int rowAg0 = (rowA[j] >> 2) & 0x7; 525 const int rowAg1 = (rowA[k] >> 2) & 0x7; 526 const int rowBg0 = (rowB[j] >> 2) & 0x7; 527 const int rowBg1 = (rowB[k] >> 2) & 0x7; 528 const int rowAb0 = (rowA[j] >> 5) & 0x7; 529 const int rowAb1 = (rowA[k] >> 5) & 0x7; 530 const int rowBb0 = (rowB[j] >> 5) & 0x7; 531 const int rowBb1 = (rowB[k] >> 5) & 0x7; 532 const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2; 533 const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2; 534 const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2; 535 dst[i] = (blue << 5) | (green << 2) | red; 536 } 537 } 538 else { 539 debug_printf("bad format in do_row()"); 540 } 541} 542 543 544/** 545 * Average together four rows of a source image to produce a single new 546 * row in the dest image. It's legal for the two source rows to point 547 * to the same data. The source width must be equal to either the 548 * dest width or two times the dest width. 549 * 550 * \param datatype GL pixel type \c GL_UNSIGNED_BYTE, \c GL_UNSIGNED_SHORT, 551 * \c GL_FLOAT, etc. 552 * \param comps number of components per pixel (1..4) 553 * \param srcWidth Width of a row in the source data 554 * \param srcRowA Pointer to one of the rows of source data 555 * \param srcRowB Pointer to one of the rows of source data 556 * \param srcRowC Pointer to one of the rows of source data 557 * \param srcRowD Pointer to one of the rows of source data 558 * \param dstWidth Width of a row in the destination data 559 * \param srcRowA Pointer to the row of destination data 560 */ 561static void 562do_row_3D(enum dtype datatype, uint comps, int srcWidth, 563 const void *srcRowA, const void *srcRowB, 564 const void *srcRowC, const void *srcRowD, 565 int dstWidth, void *dstRow) 566{ 567 const uint k0 = (srcWidth == dstWidth) ? 0 : 1; 568 const uint colStride = (srcWidth == dstWidth) ? 1 : 2; 569 uint i, j, k; 570 571 assert(comps >= 1); 572 assert(comps <= 4); 573 574 if ((datatype == UBYTE) && (comps == 4)) { 575 DECLARE_ROW_POINTERS(ubyte, 4); 576 577 for (i = j = 0, k = k0; i < (uint) dstWidth; 578 i++, j += colStride, k += colStride) { 579 FILTER_3D(0); 580 FILTER_3D(1); 581 FILTER_3D(2); 582 FILTER_3D(3); 583 } 584 } 585 else if ((datatype == UBYTE) && (comps == 3)) { 586 DECLARE_ROW_POINTERS(ubyte, 3); 587 588 for (i = j = 0, k = k0; i < (uint) dstWidth; 589 i++, j += colStride, k += colStride) { 590 FILTER_3D(0); 591 FILTER_3D(1); 592 FILTER_3D(2); 593 } 594 } 595 else if ((datatype == UBYTE) && (comps == 2)) { 596 DECLARE_ROW_POINTERS(ubyte, 2); 597 598 for (i = j = 0, k = k0; i < (uint) dstWidth; 599 i++, j += colStride, k += colStride) { 600 FILTER_3D(0); 601 FILTER_3D(1); 602 } 603 } 604 else if ((datatype == UBYTE) && (comps == 1)) { 605 DECLARE_ROW_POINTERS(ubyte, 1); 606 607 for (i = j = 0, k = k0; i < (uint) dstWidth; 608 i++, j += colStride, k += colStride) { 609 FILTER_3D(0); 610 } 611 } 612 else if ((datatype == USHORT) && (comps == 4)) { 613 DECLARE_ROW_POINTERS(ushort, 4); 614 615 for (i = j = 0, k = k0; i < (uint) dstWidth; 616 i++, j += colStride, k += colStride) { 617 FILTER_3D(0); 618 FILTER_3D(1); 619 FILTER_3D(2); 620 FILTER_3D(3); 621 } 622 } 623 else if ((datatype == USHORT) && (comps == 3)) { 624 DECLARE_ROW_POINTERS(ushort, 3); 625 626 for (i = j = 0, k = k0; i < (uint) dstWidth; 627 i++, j += colStride, k += colStride) { 628 FILTER_3D(0); 629 FILTER_3D(1); 630 FILTER_3D(2); 631 } 632 } 633 else if ((datatype == USHORT) && (comps == 2)) { 634 DECLARE_ROW_POINTERS(ushort, 2); 635 636 for (i = j = 0, k = k0; i < (uint) dstWidth; 637 i++, j += colStride, k += colStride) { 638 FILTER_3D(0); 639 FILTER_3D(1); 640 } 641 } 642 else if ((datatype == USHORT) && (comps == 1)) { 643 DECLARE_ROW_POINTERS(ushort, 1); 644 645 for (i = j = 0, k = k0; i < (uint) dstWidth; 646 i++, j += colStride, k += colStride) { 647 FILTER_3D(0); 648 } 649 } 650 else if ((datatype == FLOAT) && (comps == 4)) { 651 DECLARE_ROW_POINTERS(float, 4); 652 653 for (i = j = 0, k = k0; i < (uint) dstWidth; 654 i++, j += colStride, k += colStride) { 655 FILTER_F_3D(0); 656 FILTER_F_3D(1); 657 FILTER_F_3D(2); 658 FILTER_F_3D(3); 659 } 660 } 661 else if ((datatype == FLOAT) && (comps == 3)) { 662 DECLARE_ROW_POINTERS(float, 3); 663 664 for (i = j = 0, k = k0; i < (uint) dstWidth; 665 i++, j += colStride, k += colStride) { 666 FILTER_F_3D(0); 667 FILTER_F_3D(1); 668 FILTER_F_3D(2); 669 } 670 } 671 else if ((datatype == FLOAT) && (comps == 2)) { 672 DECLARE_ROW_POINTERS(float, 2); 673 674 for (i = j = 0, k = k0; i < (uint) dstWidth; 675 i++, j += colStride, k += colStride) { 676 FILTER_F_3D(0); 677 FILTER_F_3D(1); 678 } 679 } 680 else if ((datatype == FLOAT) && (comps == 1)) { 681 DECLARE_ROW_POINTERS(float, 1); 682 683 for (i = j = 0, k = k0; i < (uint) dstWidth; 684 i++, j += colStride, k += colStride) { 685 FILTER_F_3D(0); 686 } 687 } 688 else if ((datatype == HALF_FLOAT) && (comps == 4)) { 689 DECLARE_ROW_POINTERS(half_float, 4); 690 691 for (i = j = 0, k = k0; i < (uint) dstWidth; 692 i++, j += colStride, k += colStride) { 693 FILTER_HF_3D(0); 694 FILTER_HF_3D(1); 695 FILTER_HF_3D(2); 696 FILTER_HF_3D(3); 697 } 698 } 699 else if ((datatype == HALF_FLOAT) && (comps == 3)) { 700 DECLARE_ROW_POINTERS(half_float, 4); 701 702 for (i = j = 0, k = k0; i < (uint) dstWidth; 703 i++, j += colStride, k += colStride) { 704 FILTER_HF_3D(0); 705 FILTER_HF_3D(1); 706 FILTER_HF_3D(2); 707 } 708 } 709 else if ((datatype == HALF_FLOAT) && (comps == 2)) { 710 DECLARE_ROW_POINTERS(half_float, 4); 711 712 for (i = j = 0, k = k0; i < (uint) dstWidth; 713 i++, j += colStride, k += colStride) { 714 FILTER_HF_3D(0); 715 FILTER_HF_3D(1); 716 } 717 } 718 else if ((datatype == HALF_FLOAT) && (comps == 1)) { 719 DECLARE_ROW_POINTERS(half_float, 4); 720 721 for (i = j = 0, k = k0; i < (uint) dstWidth; 722 i++, j += colStride, k += colStride) { 723 FILTER_HF_3D(0); 724 } 725 } 726 else if ((datatype == UINT) && (comps == 1)) { 727 const uint *rowA = (const uint *) srcRowA; 728 const uint *rowB = (const uint *) srcRowB; 729 const uint *rowC = (const uint *) srcRowC; 730 const uint *rowD = (const uint *) srcRowD; 731 float *dst = (float *) dstRow; 732 733 for (i = j = 0, k = k0; i < (uint) dstWidth; 734 i++, j += colStride, k += colStride) { 735 const uint64_t tmp = (((uint64_t) rowA[j] + (uint64_t) rowA[k]) 736 + ((uint64_t) rowB[j] + (uint64_t) rowB[k]) 737 + ((uint64_t) rowC[j] + (uint64_t) rowC[k]) 738 + ((uint64_t) rowD[j] + (uint64_t) rowD[k])); 739 dst[i] = (float)((double) tmp * 0.125); 740 } 741 } 742 else if ((datatype == USHORT_5_6_5) && (comps == 3)) { 743 DECLARE_ROW_POINTERS0(ushort); 744 745 for (i = j = 0, k = k0; i < (uint) dstWidth; 746 i++, j += colStride, k += colStride) { 747 const int rowAr0 = rowA[j] & 0x1f; 748 const int rowAr1 = rowA[k] & 0x1f; 749 const int rowBr0 = rowB[j] & 0x1f; 750 const int rowBr1 = rowB[k] & 0x1f; 751 const int rowCr0 = rowC[j] & 0x1f; 752 const int rowCr1 = rowC[k] & 0x1f; 753 const int rowDr0 = rowD[j] & 0x1f; 754 const int rowDr1 = rowD[k] & 0x1f; 755 const int rowAg0 = (rowA[j] >> 5) & 0x3f; 756 const int rowAg1 = (rowA[k] >> 5) & 0x3f; 757 const int rowBg0 = (rowB[j] >> 5) & 0x3f; 758 const int rowBg1 = (rowB[k] >> 5) & 0x3f; 759 const int rowCg0 = (rowC[j] >> 5) & 0x3f; 760 const int rowCg1 = (rowC[k] >> 5) & 0x3f; 761 const int rowDg0 = (rowD[j] >> 5) & 0x3f; 762 const int rowDg1 = (rowD[k] >> 5) & 0x3f; 763 const int rowAb0 = (rowA[j] >> 11) & 0x1f; 764 const int rowAb1 = (rowA[k] >> 11) & 0x1f; 765 const int rowBb0 = (rowB[j] >> 11) & 0x1f; 766 const int rowBb1 = (rowB[k] >> 11) & 0x1f; 767 const int rowCb0 = (rowC[j] >> 11) & 0x1f; 768 const int rowCb1 = (rowC[k] >> 11) & 0x1f; 769 const int rowDb0 = (rowD[j] >> 11) & 0x1f; 770 const int rowDb1 = (rowD[k] >> 11) & 0x1f; 771 const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1, 772 rowCr0, rowCr1, rowDr0, rowDr1); 773 const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1, 774 rowCg0, rowCg1, rowDg0, rowDg1); 775 const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1, 776 rowCb0, rowCb1, rowDb0, rowDb1); 777 dst[i] = (b << 11) | (g << 5) | r; 778 } 779 } 780 else if ((datatype == USHORT_4_4_4_4) && (comps == 4)) { 781 DECLARE_ROW_POINTERS0(ushort); 782 783 for (i = j = 0, k = k0; i < (uint) dstWidth; 784 i++, j += colStride, k += colStride) { 785 const int rowAr0 = rowA[j] & 0xf; 786 const int rowAr1 = rowA[k] & 0xf; 787 const int rowBr0 = rowB[j] & 0xf; 788 const int rowBr1 = rowB[k] & 0xf; 789 const int rowCr0 = rowC[j] & 0xf; 790 const int rowCr1 = rowC[k] & 0xf; 791 const int rowDr0 = rowD[j] & 0xf; 792 const int rowDr1 = rowD[k] & 0xf; 793 const int rowAg0 = (rowA[j] >> 4) & 0xf; 794 const int rowAg1 = (rowA[k] >> 4) & 0xf; 795 const int rowBg0 = (rowB[j] >> 4) & 0xf; 796 const int rowBg1 = (rowB[k] >> 4) & 0xf; 797 const int rowCg0 = (rowC[j] >> 4) & 0xf; 798 const int rowCg1 = (rowC[k] >> 4) & 0xf; 799 const int rowDg0 = (rowD[j] >> 4) & 0xf; 800 const int rowDg1 = (rowD[k] >> 4) & 0xf; 801 const int rowAb0 = (rowA[j] >> 8) & 0xf; 802 const int rowAb1 = (rowA[k] >> 8) & 0xf; 803 const int rowBb0 = (rowB[j] >> 8) & 0xf; 804 const int rowBb1 = (rowB[k] >> 8) & 0xf; 805 const int rowCb0 = (rowC[j] >> 8) & 0xf; 806 const int rowCb1 = (rowC[k] >> 8) & 0xf; 807 const int rowDb0 = (rowD[j] >> 8) & 0xf; 808 const int rowDb1 = (rowD[k] >> 8) & 0xf; 809 const int rowAa0 = (rowA[j] >> 12) & 0xf; 810 const int rowAa1 = (rowA[k] >> 12) & 0xf; 811 const int rowBa0 = (rowB[j] >> 12) & 0xf; 812 const int rowBa1 = (rowB[k] >> 12) & 0xf; 813 const int rowCa0 = (rowC[j] >> 12) & 0xf; 814 const int rowCa1 = (rowC[k] >> 12) & 0xf; 815 const int rowDa0 = (rowD[j] >> 12) & 0xf; 816 const int rowDa1 = (rowD[k] >> 12) & 0xf; 817 const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1, 818 rowCr0, rowCr1, rowDr0, rowDr1); 819 const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1, 820 rowCg0, rowCg1, rowDg0, rowDg1); 821 const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1, 822 rowCb0, rowCb1, rowDb0, rowDb1); 823 const int a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1, 824 rowCa0, rowCa1, rowDa0, rowDa1); 825 826 dst[i] = (a << 12) | (b << 8) | (g << 4) | r; 827 } 828 } 829 else if ((datatype == USHORT_1_5_5_5_REV) && (comps == 4)) { 830 DECLARE_ROW_POINTERS0(ushort); 831 832 for (i = j = 0, k = k0; i < (uint) dstWidth; 833 i++, j += colStride, k += colStride) { 834 const int rowAr0 = rowA[j] & 0x1f; 835 const int rowAr1 = rowA[k] & 0x1f; 836 const int rowBr0 = rowB[j] & 0x1f; 837 const int rowBr1 = rowB[k] & 0x1f; 838 const int rowCr0 = rowC[j] & 0x1f; 839 const int rowCr1 = rowC[k] & 0x1f; 840 const int rowDr0 = rowD[j] & 0x1f; 841 const int rowDr1 = rowD[k] & 0x1f; 842 const int rowAg0 = (rowA[j] >> 5) & 0x1f; 843 const int rowAg1 = (rowA[k] >> 5) & 0x1f; 844 const int rowBg0 = (rowB[j] >> 5) & 0x1f; 845 const int rowBg1 = (rowB[k] >> 5) & 0x1f; 846 const int rowCg0 = (rowC[j] >> 5) & 0x1f; 847 const int rowCg1 = (rowC[k] >> 5) & 0x1f; 848 const int rowDg0 = (rowD[j] >> 5) & 0x1f; 849 const int rowDg1 = (rowD[k] >> 5) & 0x1f; 850 const int rowAb0 = (rowA[j] >> 10) & 0x1f; 851 const int rowAb1 = (rowA[k] >> 10) & 0x1f; 852 const int rowBb0 = (rowB[j] >> 10) & 0x1f; 853 const int rowBb1 = (rowB[k] >> 10) & 0x1f; 854 const int rowCb0 = (rowC[j] >> 10) & 0x1f; 855 const int rowCb1 = (rowC[k] >> 10) & 0x1f; 856 const int rowDb0 = (rowD[j] >> 10) & 0x1f; 857 const int rowDb1 = (rowD[k] >> 10) & 0x1f; 858 const int rowAa0 = (rowA[j] >> 15) & 0x1; 859 const int rowAa1 = (rowA[k] >> 15) & 0x1; 860 const int rowBa0 = (rowB[j] >> 15) & 0x1; 861 const int rowBa1 = (rowB[k] >> 15) & 0x1; 862 const int rowCa0 = (rowC[j] >> 15) & 0x1; 863 const int rowCa1 = (rowC[k] >> 15) & 0x1; 864 const int rowDa0 = (rowD[j] >> 15) & 0x1; 865 const int rowDa1 = (rowD[k] >> 15) & 0x1; 866 const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1, 867 rowCr0, rowCr1, rowDr0, rowDr1); 868 const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1, 869 rowCg0, rowCg1, rowDg0, rowDg1); 870 const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1, 871 rowCb0, rowCb1, rowDb0, rowDb1); 872 const int a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1, 873 rowCa0, rowCa1, rowDa0, rowDa1); 874 875 dst[i] = (a << 15) | (b << 10) | (g << 5) | r; 876 } 877 } 878 else if ((datatype == UBYTE_3_3_2) && (comps == 3)) { 879 DECLARE_ROW_POINTERS0(ushort); 880 881 for (i = j = 0, k = k0; i < (uint) dstWidth; 882 i++, j += colStride, k += colStride) { 883 const int rowAr0 = rowA[j] & 0x3; 884 const int rowAr1 = rowA[k] & 0x3; 885 const int rowBr0 = rowB[j] & 0x3; 886 const int rowBr1 = rowB[k] & 0x3; 887 const int rowCr0 = rowC[j] & 0x3; 888 const int rowCr1 = rowC[k] & 0x3; 889 const int rowDr0 = rowD[j] & 0x3; 890 const int rowDr1 = rowD[k] & 0x3; 891 const int rowAg0 = (rowA[j] >> 2) & 0x7; 892 const int rowAg1 = (rowA[k] >> 2) & 0x7; 893 const int rowBg0 = (rowB[j] >> 2) & 0x7; 894 const int rowBg1 = (rowB[k] >> 2) & 0x7; 895 const int rowCg0 = (rowC[j] >> 2) & 0x7; 896 const int rowCg1 = (rowC[k] >> 2) & 0x7; 897 const int rowDg0 = (rowD[j] >> 2) & 0x7; 898 const int rowDg1 = (rowD[k] >> 2) & 0x7; 899 const int rowAb0 = (rowA[j] >> 5) & 0x7; 900 const int rowAb1 = (rowA[k] >> 5) & 0x7; 901 const int rowBb0 = (rowB[j] >> 5) & 0x7; 902 const int rowBb1 = (rowB[k] >> 5) & 0x7; 903 const int rowCb0 = (rowC[j] >> 5) & 0x7; 904 const int rowCb1 = (rowC[k] >> 5) & 0x7; 905 const int rowDb0 = (rowD[j] >> 5) & 0x7; 906 const int rowDb1 = (rowD[k] >> 5) & 0x7; 907 const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1, 908 rowCr0, rowCr1, rowDr0, rowDr1); 909 const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1, 910 rowCg0, rowCg1, rowDg0, rowDg1); 911 const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1, 912 rowCb0, rowCb1, rowDb0, rowDb1); 913 dst[i] = (b << 5) | (g << 2) | r; 914 } 915 } 916 else { 917 debug_printf("bad format in do_row_3D()"); 918 } 919} 920 921 922 923static void 924format_to_type_comps(enum pipe_format pformat, 925 enum dtype *datatype, uint *comps) 926{ 927 switch (pformat) { 928 case PIPE_FORMAT_A8R8G8B8_UNORM: 929 case PIPE_FORMAT_X8R8G8B8_UNORM: 930 case PIPE_FORMAT_B8G8R8A8_UNORM: 931 case PIPE_FORMAT_B8G8R8X8_UNORM: 932 *datatype = UBYTE; 933 *comps = 4; 934 return; 935 case PIPE_FORMAT_A1R5G5B5_UNORM: 936 *datatype = USHORT_1_5_5_5_REV; 937 *comps = 4; 938 return; 939 case PIPE_FORMAT_A4R4G4B4_UNORM: 940 *datatype = USHORT_4_4_4_4; 941 *comps = 4; 942 return; 943 case PIPE_FORMAT_R5G6B5_UNORM: 944 *datatype = USHORT_5_6_5; 945 *comps = 3; 946 return; 947 case PIPE_FORMAT_L8_UNORM: 948 case PIPE_FORMAT_A8_UNORM: 949 case PIPE_FORMAT_I8_UNORM: 950 *datatype = UBYTE; 951 *comps = 1; 952 return; 953 case PIPE_FORMAT_A8L8_UNORM: 954 *datatype = UBYTE; 955 *comps = 2; 956 return; 957 default: 958 assert(0); 959 *datatype = UBYTE; 960 *comps = 0; 961 break; 962 } 963} 964 965 966static void 967reduce_1d(enum pipe_format pformat, 968 int srcWidth, const ubyte *srcPtr, 969 int dstWidth, ubyte *dstPtr) 970{ 971 enum dtype datatype; 972 uint comps; 973 974 format_to_type_comps(pformat, &datatype, &comps); 975 976 /* we just duplicate the input row, kind of hack, saves code */ 977 do_row(datatype, comps, 978 srcWidth, srcPtr, srcPtr, 979 dstWidth, dstPtr); 980} 981 982 983/** 984 * Strides are in bytes. If zero, it'll be computed as width * bpp. 985 */ 986static void 987reduce_2d(enum pipe_format pformat, 988 int srcWidth, int srcHeight, 989 int srcRowStride, const ubyte *srcPtr, 990 int dstWidth, int dstHeight, 991 int dstRowStride, ubyte *dstPtr) 992{ 993 enum dtype datatype; 994 uint comps; 995 const int bpt = pf_get_size(pformat); 996 const ubyte *srcA, *srcB; 997 ubyte *dst; 998 int row; 999 1000 format_to_type_comps(pformat, &datatype, &comps); 1001 1002 if (!srcRowStride) 1003 srcRowStride = bpt * srcWidth; 1004 1005 if (!dstRowStride) 1006 dstRowStride = bpt * dstWidth; 1007 1008 /* Compute src and dst pointers */ 1009 srcA = srcPtr; 1010 if (srcHeight > 1) 1011 srcB = srcA + srcRowStride; 1012 else 1013 srcB = srcA; 1014 dst = dstPtr; 1015 1016 for (row = 0; row < dstHeight; row++) { 1017 do_row(datatype, comps, 1018 srcWidth, srcA, srcB, 1019 dstWidth, dst); 1020 srcA += 2 * srcRowStride; 1021 srcB += 2 * srcRowStride; 1022 dst += dstRowStride; 1023 } 1024} 1025 1026 1027static void 1028reduce_3d(enum pipe_format pformat, 1029 int srcWidth, int srcHeight, int srcDepth, 1030 int srcRowStride, const ubyte *srcPtr, 1031 int dstWidth, int dstHeight, int dstDepth, 1032 int dstRowStride, ubyte *dstPtr) 1033{ 1034 const int bpt = pf_get_size(pformat); 1035 const int border = 0; 1036 int img, row; 1037 int bytesPerSrcImage, bytesPerDstImage; 1038 int bytesPerSrcRow, bytesPerDstRow; 1039 int srcImageOffset, srcRowOffset; 1040 enum dtype datatype; 1041 uint comps; 1042 1043 format_to_type_comps(pformat, &datatype, &comps); 1044 1045 bytesPerSrcImage = srcWidth * srcHeight * bpt; 1046 bytesPerDstImage = dstWidth * dstHeight * bpt; 1047 1048 bytesPerSrcRow = srcWidth * bpt; 1049 bytesPerDstRow = dstWidth * bpt; 1050 1051 /* Offset between adjacent src images to be averaged together */ 1052 srcImageOffset = (srcDepth == dstDepth) ? 0 : bytesPerSrcImage; 1053 1054 /* Offset between adjacent src rows to be averaged together */ 1055 srcRowOffset = (srcHeight == dstHeight) ? 0 : srcWidth * bpt; 1056 1057 /* 1058 * Need to average together up to 8 src pixels for each dest pixel. 1059 * Break that down into 3 operations: 1060 * 1. take two rows from source image and average them together. 1061 * 2. take two rows from next source image and average them together. 1062 * 3. take the two averaged rows and average them for the final dst row. 1063 */ 1064 1065 /* 1066 _mesa_printf("mip3d %d x %d x %d -> %d x %d x %d\n", 1067 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth); 1068 */ 1069 1070 for (img = 0; img < dstDepth; img++) { 1071 /* first source image pointer, skipping border */ 1072 const ubyte *imgSrcA = srcPtr 1073 + (bytesPerSrcImage + bytesPerSrcRow + border) * bpt * border 1074 + img * (bytesPerSrcImage + srcImageOffset); 1075 /* second source image pointer, skipping border */ 1076 const ubyte *imgSrcB = imgSrcA + srcImageOffset; 1077 /* address of the dest image, skipping border */ 1078 ubyte *imgDst = dstPtr 1079 + (bytesPerDstImage + bytesPerDstRow + border) * bpt * border 1080 + img * bytesPerDstImage; 1081 1082 /* setup the four source row pointers and the dest row pointer */ 1083 const ubyte *srcImgARowA = imgSrcA; 1084 const ubyte *srcImgARowB = imgSrcA + srcRowOffset; 1085 const ubyte *srcImgBRowA = imgSrcB; 1086 const ubyte *srcImgBRowB = imgSrcB + srcRowOffset; 1087 ubyte *dstImgRow = imgDst; 1088 1089 for (row = 0; row < dstHeight; row++) { 1090 do_row_3D(datatype, comps, srcWidth, 1091 srcImgARowA, srcImgARowB, 1092 srcImgBRowA, srcImgBRowB, 1093 dstWidth, dstImgRow); 1094 1095 /* advance to next rows */ 1096 srcImgARowA += bytesPerSrcRow + srcRowOffset; 1097 srcImgARowB += bytesPerSrcRow + srcRowOffset; 1098 srcImgBRowA += bytesPerSrcRow + srcRowOffset; 1099 srcImgBRowB += bytesPerSrcRow + srcRowOffset; 1100 dstImgRow += bytesPerDstRow; 1101 } 1102 } 1103} 1104 1105 1106 1107 1108static void 1109make_1d_mipmap(struct gen_mipmap_state *ctx, 1110 struct pipe_texture *pt, 1111 uint face, uint baseLevel, uint lastLevel) 1112{ 1113 struct pipe_context *pipe = ctx->pipe; 1114 struct pipe_screen *screen = pipe->screen; 1115 const uint zslice = 0; 1116 uint dstLevel; 1117 1118 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) { 1119 const uint srcLevel = dstLevel - 1; 1120 struct pipe_surface *srcSurf, *dstSurf; 1121 void *srcMap, *dstMap; 1122 1123 srcSurf = screen->get_tex_surface(screen, pt, face, srcLevel, zslice, 1124 PIPE_BUFFER_USAGE_CPU_READ); 1125 1126 dstSurf = screen->get_tex_surface(screen, pt, face, dstLevel, zslice, 1127 PIPE_BUFFER_USAGE_CPU_WRITE); 1128 1129 srcMap = ((ubyte *) pipe_buffer_map(screen, srcSurf->buffer, 1130 PIPE_BUFFER_USAGE_CPU_READ) 1131 + srcSurf->offset); 1132 dstMap = ((ubyte *) pipe_buffer_map(screen, dstSurf->buffer, 1133 PIPE_BUFFER_USAGE_CPU_WRITE) 1134 + dstSurf->offset); 1135 1136 reduce_1d(pt->format, 1137 srcSurf->width, srcMap, 1138 dstSurf->width, dstMap); 1139 1140 pipe_buffer_unmap(screen, srcSurf->buffer); 1141 pipe_buffer_unmap(screen, dstSurf->buffer); 1142 1143 pipe_surface_reference(&srcSurf, NULL); 1144 pipe_surface_reference(&dstSurf, NULL); 1145 } 1146} 1147 1148 1149static void 1150make_2d_mipmap(struct gen_mipmap_state *ctx, 1151 struct pipe_texture *pt, 1152 uint face, uint baseLevel, uint lastLevel) 1153{ 1154 struct pipe_context *pipe = ctx->pipe; 1155 struct pipe_screen *screen = pipe->screen; 1156 const uint zslice = 0; 1157 uint dstLevel; 1158 1159 assert(pt->block.width == 1); 1160 assert(pt->block.height == 1); 1161 1162 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) { 1163 const uint srcLevel = dstLevel - 1; 1164 struct pipe_surface *srcSurf, *dstSurf; 1165 ubyte *srcMap, *dstMap; 1166 1167 srcSurf = screen->get_tex_surface(screen, pt, face, srcLevel, zslice, 1168 PIPE_BUFFER_USAGE_CPU_READ); 1169 dstSurf = screen->get_tex_surface(screen, pt, face, dstLevel, zslice, 1170 PIPE_BUFFER_USAGE_CPU_WRITE); 1171 1172 srcMap = ((ubyte *) pipe_buffer_map(screen, srcSurf->buffer, 1173 PIPE_BUFFER_USAGE_CPU_READ) 1174 + srcSurf->offset); 1175 dstMap = ((ubyte *) pipe_buffer_map(screen, dstSurf->buffer, 1176 PIPE_BUFFER_USAGE_CPU_WRITE) 1177 + dstSurf->offset); 1178 1179 reduce_2d(pt->format, 1180 srcSurf->width, srcSurf->height, 1181 srcSurf->stride, srcMap, 1182 dstSurf->width, dstSurf->height, 1183 dstSurf->stride, dstMap); 1184 1185 pipe_buffer_unmap(screen, srcSurf->buffer); 1186 pipe_buffer_unmap(screen, dstSurf->buffer); 1187 1188 pipe_surface_reference(&srcSurf, NULL); 1189 pipe_surface_reference(&dstSurf, NULL); 1190 } 1191} 1192 1193 1194static void 1195make_3d_mipmap(struct gen_mipmap_state *ctx, 1196 struct pipe_texture *pt, 1197 uint face, uint baseLevel, uint lastLevel) 1198{ 1199 struct pipe_context *pipe = ctx->pipe; 1200 struct pipe_screen *screen = pipe->screen; 1201 uint dstLevel, zslice; 1202 1203 assert(pt->block.width == 1); 1204 assert(pt->block.height == 1); 1205 1206 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) { 1207 const uint srcLevel = dstLevel - 1; 1208 struct pipe_surface *srcSurf, *dstSurf; 1209 ubyte *srcMap, *dstMap; 1210 1211 srcSurf = screen->get_tex_surface(screen, pt, face, srcLevel, zslice, 1212 PIPE_BUFFER_USAGE_CPU_READ); 1213 dstSurf = screen->get_tex_surface(screen, pt, face, dstLevel, zslice, 1214 PIPE_BUFFER_USAGE_CPU_WRITE); 1215 1216 srcMap = ((ubyte *) pipe_buffer_map(screen, srcSurf->buffer, 1217 PIPE_BUFFER_USAGE_CPU_READ) 1218 + srcSurf->offset); 1219 dstMap = ((ubyte *) pipe_buffer_map(screen, dstSurf->buffer, 1220 PIPE_BUFFER_USAGE_CPU_WRITE) 1221 + dstSurf->offset); 1222 1223#if 0 1224 reduce_3d(pt->format, 1225 srcSurf->width, srcSurf->height, 1226 srcSurf->stride, srcMap, 1227 dstSurf->width, dstSurf->height, 1228 dstSurf->stride, dstMap); 1229#else 1230 (void) reduce_3d; 1231#endif 1232 1233 pipe_buffer_unmap(screen, srcSurf->buffer); 1234 pipe_buffer_unmap(screen, dstSurf->buffer); 1235 1236 pipe_surface_reference(&srcSurf, NULL); 1237 pipe_surface_reference(&dstSurf, NULL); 1238 } 1239} 1240 1241 1242static void 1243fallback_gen_mipmap(struct gen_mipmap_state *ctx, 1244 struct pipe_texture *pt, 1245 uint face, uint baseLevel, uint lastLevel) 1246{ 1247 switch (pt->target) { 1248 case PIPE_TEXTURE_1D: 1249 make_1d_mipmap(ctx, pt, face, baseLevel, lastLevel); 1250 break; 1251 case PIPE_TEXTURE_2D: 1252 case PIPE_TEXTURE_CUBE: 1253 make_2d_mipmap(ctx, pt, face, baseLevel, lastLevel); 1254 break; 1255 case PIPE_TEXTURE_3D: 1256 make_3d_mipmap(ctx, pt, face, baseLevel, lastLevel); 1257 break; 1258 default: 1259 assert(0); 1260 } 1261} 1262 1263 1264/** 1265 * Create a mipmap generation context. 1266 * The idea is to create one of these and re-use it each time we need to 1267 * generate a mipmap. 1268 */ 1269struct gen_mipmap_state * 1270util_create_gen_mipmap(struct pipe_context *pipe, 1271 struct cso_context *cso) 1272{ 1273 struct gen_mipmap_state *ctx; 1274 uint i; 1275 1276 ctx = CALLOC_STRUCT(gen_mipmap_state); 1277 if (!ctx) 1278 return NULL; 1279 1280 ctx->pipe = pipe; 1281 ctx->cso = cso; 1282 1283 /* disabled blending/masking */ 1284 memset(&ctx->blend, 0, sizeof(ctx->blend)); 1285 ctx->blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE; 1286 ctx->blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE; 1287 ctx->blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO; 1288 ctx->blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO; 1289 ctx->blend.colormask = PIPE_MASK_RGBA; 1290 1291 /* no-op depth/stencil/alpha */ 1292 memset(&ctx->depthstencil, 0, sizeof(ctx->depthstencil)); 1293 1294 /* rasterizer */ 1295 memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer)); 1296 ctx->rasterizer.front_winding = PIPE_WINDING_CW; 1297 ctx->rasterizer.cull_mode = PIPE_WINDING_NONE; 1298 ctx->rasterizer.bypass_clipping = 1; 1299 /*ctx->rasterizer.bypass_vs = 1;*/ 1300 ctx->rasterizer.gl_rasterization_rules = 1; 1301 1302 /* sampler state */ 1303 memset(&ctx->sampler, 0, sizeof(ctx->sampler)); 1304 ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 1305 ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 1306 ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 1307 ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST; 1308 ctx->sampler.normalized_coords = 1; 1309 1310 /* viewport state (identity, verts are in wincoords) */ 1311 ctx->viewport.scale[0] = 1.0; 1312 ctx->viewport.scale[1] = 1.0; 1313 ctx->viewport.scale[2] = 1.0; 1314 ctx->viewport.scale[3] = 1.0; 1315 ctx->viewport.translate[0] = 0.0; 1316 ctx->viewport.translate[1] = 0.0; 1317 ctx->viewport.translate[2] = 0.0; 1318 ctx->viewport.translate[3] = 0.0; 1319 1320 /* vertex shader */ 1321 { 1322 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION, 1323 TGSI_SEMANTIC_GENERIC }; 1324 const uint semantic_indexes[] = { 0, 0 }; 1325 ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names, 1326 semantic_indexes, 1327 &ctx->vert_shader); 1328 } 1329 1330 /* fragment shader */ 1331 ctx->fs = util_make_fragment_tex_shader(pipe, &ctx->frag_shader); 1332 1333 /* vertex data that doesn't change */ 1334 for (i = 0; i < 4; i++) { 1335 ctx->vertices[i][0][2] = 0.0f; /* z */ 1336 ctx->vertices[i][0][3] = 1.0f; /* w */ 1337 ctx->vertices[i][1][2] = 0.0f; /* r */ 1338 ctx->vertices[i][1][3] = 1.0f; /* q */ 1339 } 1340 1341 /* Note: the actual vertex buffer is allocated as needed below */ 1342 1343 return ctx; 1344} 1345 1346 1347/** 1348 * Get next "slot" of vertex space in the vertex buffer. 1349 * We're allocating one large vertex buffer and using it piece by piece. 1350 */ 1351static unsigned 1352get_next_slot(struct gen_mipmap_state *ctx) 1353{ 1354 const unsigned max_slots = 4096 / sizeof ctx->vertices; 1355 1356 if (ctx->vbuf_slot >= max_slots) 1357 util_gen_mipmap_flush( ctx ); 1358 1359 if (!ctx->vbuf) { 1360 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen, 1361 32, 1362 PIPE_BUFFER_USAGE_VERTEX, 1363 max_slots * sizeof ctx->vertices); 1364 } 1365 1366 return ctx->vbuf_slot++ * sizeof ctx->vertices; 1367} 1368 1369 1370static unsigned 1371set_vertex_data(struct gen_mipmap_state *ctx, float width, float height) 1372{ 1373 void *buf; 1374 unsigned offset; 1375 1376 ctx->vertices[0][0][0] = 0.0f; /*x*/ 1377 ctx->vertices[0][0][1] = 0.0f; /*y*/ 1378 ctx->vertices[0][1][0] = 0.0f; /*s*/ 1379 ctx->vertices[0][1][1] = 0.0f; /*t*/ 1380 1381 ctx->vertices[1][0][0] = width; 1382 ctx->vertices[1][0][1] = 0.0f; 1383 ctx->vertices[1][1][0] = 1.0f; 1384 ctx->vertices[1][1][1] = 0.0f; 1385 1386 ctx->vertices[2][0][0] = width; 1387 ctx->vertices[2][0][1] = height; 1388 ctx->vertices[2][1][0] = 1.0f; 1389 ctx->vertices[2][1][1] = 1.0f; 1390 1391 ctx->vertices[3][0][0] = 0.0f; 1392 ctx->vertices[3][0][1] = height; 1393 ctx->vertices[3][1][0] = 0.0f; 1394 ctx->vertices[3][1][1] = 1.0f; 1395 1396 offset = get_next_slot( ctx ); 1397 1398 buf = pipe_buffer_map(ctx->pipe->screen, ctx->vbuf, 1399 PIPE_BUFFER_USAGE_CPU_WRITE); 1400 1401 memcpy((char *)buf + offset, ctx->vertices, sizeof(ctx->vertices)); 1402 1403 pipe_buffer_unmap(ctx->pipe->screen, ctx->vbuf); 1404 1405 return offset; 1406} 1407 1408 1409 1410/** 1411 * Destroy a mipmap generation context 1412 */ 1413void 1414util_destroy_gen_mipmap(struct gen_mipmap_state *ctx) 1415{ 1416 struct pipe_context *pipe = ctx->pipe; 1417 1418 pipe->delete_vs_state(pipe, ctx->vs); 1419 pipe->delete_fs_state(pipe, ctx->fs); 1420 1421 FREE((void*) ctx->vert_shader.tokens); 1422 FREE((void*) ctx->frag_shader.tokens); 1423 1424 pipe_buffer_reference(pipe->screen, &ctx->vbuf, NULL); 1425 1426 FREE(ctx); 1427} 1428 1429 1430 1431/* Release vertex buffer at end of frame to avoid synchronous 1432 * rendering. 1433 */ 1434void util_gen_mipmap_flush( struct gen_mipmap_state *ctx ) 1435{ 1436 pipe_buffer_reference(ctx->pipe->screen, &ctx->vbuf, NULL); 1437 ctx->vbuf_slot = 0; 1438} 1439 1440 1441/** 1442 * Generate mipmap images. It's assumed all needed texture memory is 1443 * already allocated. 1444 * 1445 * \param pt the texture to generate mipmap levels for 1446 * \param face which cube face to generate mipmaps for (0 for non-cube maps) 1447 * \param baseLevel the first mipmap level to use as a src 1448 * \param lastLevel the last mipmap level to generate 1449 * \param filter the minification filter used to generate mipmap levels with 1450 * \param filter one of PIPE_TEX_FILTER_LINEAR, PIPE_TEX_FILTER_NEAREST 1451 */ 1452void 1453util_gen_mipmap(struct gen_mipmap_state *ctx, 1454 struct pipe_texture *pt, 1455 uint face, uint baseLevel, uint lastLevel, uint filter) 1456{ 1457 struct pipe_context *pipe = ctx->pipe; 1458 struct pipe_screen *screen = pipe->screen; 1459 struct pipe_framebuffer_state fb; 1460 uint dstLevel; 1461 uint zslice = 0; 1462 uint offset; 1463 1464 /* check if we can render in the texture's format */ 1465 if (!screen->is_format_supported(screen, pt->format, PIPE_TEXTURE_2D, 1466 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) { 1467 fallback_gen_mipmap(ctx, pt, face, baseLevel, lastLevel); 1468 return; 1469 } 1470 1471 /* save state (restored below) */ 1472 cso_save_blend(ctx->cso); 1473 cso_save_depth_stencil_alpha(ctx->cso); 1474 cso_save_rasterizer(ctx->cso); 1475 cso_save_samplers(ctx->cso); 1476 cso_save_sampler_textures(ctx->cso); 1477 cso_save_framebuffer(ctx->cso); 1478 cso_save_fragment_shader(ctx->cso); 1479 cso_save_vertex_shader(ctx->cso); 1480 cso_save_viewport(ctx->cso); 1481 1482 /* bind our state */ 1483 cso_set_blend(ctx->cso, &ctx->blend); 1484 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil); 1485 cso_set_rasterizer(ctx->cso, &ctx->rasterizer); 1486 cso_set_viewport(ctx->cso, &ctx->viewport); 1487 1488 cso_set_fragment_shader_handle(ctx->cso, ctx->fs); 1489 cso_set_vertex_shader_handle(ctx->cso, ctx->vs); 1490 1491 /* init framebuffer state */ 1492 memset(&fb, 0, sizeof(fb)); 1493 fb.num_cbufs = 1; 1494 1495 /* set min/mag to same filter for faster sw speed */ 1496 ctx->sampler.mag_img_filter = filter; 1497 ctx->sampler.min_img_filter = filter; 1498 1499 /* 1500 * XXX for small mipmap levels, it may be faster to use the software 1501 * fallback path... 1502 */ 1503 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) { 1504 const uint srcLevel = dstLevel - 1; 1505 1506 struct pipe_surface *surf = 1507 screen->get_tex_surface(screen, pt, face, dstLevel, zslice, 1508 PIPE_BUFFER_USAGE_GPU_WRITE); 1509 1510 /* 1511 * Setup framebuffer / dest surface 1512 */ 1513 fb.cbufs[0] = surf; 1514 fb.width = pt->width[dstLevel]; 1515 fb.height = pt->height[dstLevel]; 1516 cso_set_framebuffer(ctx->cso, &fb); 1517 1518 /* 1519 * Setup sampler state 1520 * Note: we should only have to set the min/max LOD clamps to ensure 1521 * we grab texels from the right mipmap level. But some hardware 1522 * has trouble with min clamping so we also set the lod_bias to 1523 * try to work around that. 1524 */ 1525 ctx->sampler.min_lod = ctx->sampler.max_lod = (float) srcLevel; 1526 ctx->sampler.lod_bias = (float) srcLevel; 1527 cso_single_sampler(ctx->cso, 0, &ctx->sampler); 1528 cso_single_sampler_done(ctx->cso); 1529 1530 cso_set_sampler_textures(ctx->cso, 1, &pt); 1531 1532 /* quad coords in window coords (bypassing clipping, viewport mapping) */ 1533 offset = set_vertex_data(ctx, 1534 (float) pt->width[dstLevel], 1535 (float) pt->height[dstLevel]); 1536 1537 util_draw_vertex_buffer(ctx->pipe, 1538 ctx->vbuf, 1539 offset, 1540 PIPE_PRIM_TRIANGLE_FAN, 1541 4, /* verts */ 1542 2); /* attribs/vert */ 1543 1544 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL); 1545 1546 /* need to signal that the texture has changed _after_ rendering to it */ 1547 pipe_surface_reference( &surf, NULL ); 1548 } 1549 1550 /* restore state we changed */ 1551 cso_restore_blend(ctx->cso); 1552 cso_restore_depth_stencil_alpha(ctx->cso); 1553 cso_restore_rasterizer(ctx->cso); 1554 cso_restore_samplers(ctx->cso); 1555 cso_restore_sampler_textures(ctx->cso); 1556 cso_restore_framebuffer(ctx->cso); 1557 cso_restore_fragment_shader(ctx->cso); 1558 cso_restore_vertex_shader(ctx->cso); 1559 cso_restore_viewport(ctx->cso); 1560} 1561