st_atom_pixeltransfer.c revision ee1720b99dfb5964962f2346406a4e3e88374a68
1/************************************************************************** 2 * 3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 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 TUNGSTEN GRAPHICS 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 * Generate fragment programs to implement pixel transfer ops, such as 30 * scale/bias, colormatrix, colortable, convolution... 31 * 32 * Authors: 33 * Brian Paul 34 */ 35 36#include "main/imports.h" 37#include "main/image.h" 38#include "main/macros.h" 39#include "shader/program.h" 40#include "shader/prog_instruction.h" 41#include "shader/prog_parameter.h" 42#include "shader/prog_print.h" 43 44#include "st_context.h" 45#include "st_format.h" 46#include "st_program.h" 47#include "st_texture.h" 48#include "st_inlines.h" 49 50#include "pipe/p_screen.h" 51#include "pipe/p_context.h" 52#include "util/u_pack_color.h" 53 54 55struct state_key 56{ 57 GLuint scaleAndBias:1; 58 GLuint colorMatrix:1; 59 GLuint colorMatrixPostScaleBias:1; 60 GLuint pixelMaps:1; 61 62#if 0 63 GLfloat Maps[3][256][4]; 64 int NumMaps; 65 GLint NumStages; 66 pipeline_stage Stages[STAGE_MAX]; 67 GLboolean StagesUsed[STAGE_MAX]; 68 GLfloat Scale1[4], Bias1[4]; 69 GLfloat Scale2[4], Bias2[4]; 70#endif 71}; 72 73 74static GLboolean 75is_identity(const GLfloat m[16]) 76{ 77 GLuint i; 78 for (i = 0; i < 16; i++) { 79 const int row = i % 4, col = i / 4; 80 const float val = (GLfloat)(row == col); 81 if (m[i] != val) 82 return GL_FALSE; 83 } 84 return GL_TRUE; 85} 86 87 88static void 89make_state_key(GLcontext *ctx, struct state_key *key) 90{ 91 static const GLfloat zero[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; 92 static const GLfloat one[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; 93 94 memset(key, 0, sizeof(*key)); 95 96 if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 || 97 ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 || 98 ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 || 99 ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) { 100 key->scaleAndBias = 1; 101 } 102 103 if (!is_identity(ctx->ColorMatrixStack.Top->m)) { 104 key->colorMatrix = 1; 105 } 106 107 if (!TEST_EQ_4V(ctx->Pixel.PostColorMatrixScale, one) || 108 !TEST_EQ_4V(ctx->Pixel.PostColorMatrixBias, zero)) { 109 key->colorMatrixPostScaleBias = 1; 110 } 111 112 key->pixelMaps = ctx->Pixel.MapColorFlag; 113} 114 115 116static struct pipe_texture * 117create_color_map_texture(GLcontext *ctx) 118{ 119 struct pipe_context *pipe = ctx->st->pipe; 120 struct pipe_texture *pt; 121 enum pipe_format format; 122 const uint texSize = 256; /* simple, and usually perfect */ 123 124 /* find an RGBA texture format */ 125 format = st_choose_format(pipe->screen, GL_RGBA, 126 PIPE_TEXTURE_2D, PIPE_TEXTURE_USAGE_SAMPLER); 127 128 /* create texture for color map/table */ 129 pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, format, 0, 130 texSize, texSize, 1, PIPE_TEXTURE_USAGE_SAMPLER); 131 return pt; 132} 133 134 135/** 136 * Update the pixelmap texture with the contents of the R/G/B/A pixel maps. 137 */ 138static void 139load_color_map_texture(GLcontext *ctx, struct pipe_texture *pt) 140{ 141 struct pipe_context *pipe = ctx->st->pipe; 142 struct pipe_screen *screen = pipe->screen; 143 struct pipe_transfer *transfer; 144 const GLuint rSize = ctx->PixelMaps.RtoR.Size; 145 const GLuint gSize = ctx->PixelMaps.GtoG.Size; 146 const GLuint bSize = ctx->PixelMaps.BtoB.Size; 147 const GLuint aSize = ctx->PixelMaps.AtoA.Size; 148 const uint texSize = pt->width0; 149 uint *dest; 150 uint i, j; 151 152 transfer = st_cond_flush_get_tex_transfer(st_context(ctx), 153 pt, 0, 0, 0, PIPE_TRANSFER_WRITE, 154 0, 0, texSize, texSize); 155 dest = (uint *) screen->transfer_map(screen, transfer); 156 157 /* Pack four 1D maps into a 2D texture: 158 * R map is placed horizontally, indexed by S, in channel 0 159 * G map is placed vertically, indexed by T, in channel 1 160 * B map is placed horizontally, indexed by S, in channel 2 161 * A map is placed vertically, indexed by T, in channel 3 162 */ 163 for (i = 0; i < texSize; i++) { 164 for (j = 0; j < texSize; j++) { 165 union util_color uc; 166 int k = (i * texSize + j); 167 ubyte r = ctx->PixelMaps.RtoR.Map8[j * rSize / texSize]; 168 ubyte g = ctx->PixelMaps.GtoG.Map8[i * gSize / texSize]; 169 ubyte b = ctx->PixelMaps.BtoB.Map8[j * bSize / texSize]; 170 ubyte a = ctx->PixelMaps.AtoA.Map8[i * aSize / texSize]; 171 util_pack_color_ub(r, g, b, a, pt->format, &uc); 172 *(dest + k) = uc.ui; 173 } 174 } 175 176 screen->transfer_unmap(screen, transfer); 177 screen->tex_transfer_destroy(transfer); 178} 179 180 181 182#define MAX_INST 100 183 184/** 185 * Returns a fragment program which implements the current pixel transfer ops. 186 */ 187static struct gl_fragment_program * 188get_pixel_transfer_program(GLcontext *ctx, const struct state_key *key) 189{ 190 struct st_context *st = ctx->st; 191 struct prog_instruction inst[MAX_INST]; 192 struct gl_program_parameter_list *params; 193 struct gl_fragment_program *fp; 194 GLuint ic = 0; 195 const GLuint colorTemp = 0; 196 197 fp = (struct gl_fragment_program *) 198 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0); 199 if (!fp) 200 return NULL; 201 202 params = _mesa_new_parameter_list(); 203 204 /* 205 * Get initial pixel color from the texture. 206 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D; 207 */ 208 _mesa_init_instructions(inst + ic, 1); 209 inst[ic].Opcode = OPCODE_TEX; 210 inst[ic].DstReg.File = PROGRAM_TEMPORARY; 211 inst[ic].DstReg.Index = colorTemp; 212 inst[ic].SrcReg[0].File = PROGRAM_INPUT; 213 inst[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0; 214 inst[ic].TexSrcUnit = 0; 215 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX; 216 ic++; 217 fp->Base.InputsRead = (1 << FRAG_ATTRIB_TEX0); 218 fp->Base.OutputsWritten = (1 << FRAG_RESULT_COLOR); 219 fp->Base.SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */ 220 221 if (key->scaleAndBias) { 222 static const gl_state_index scale_state[STATE_LENGTH] = 223 { STATE_INTERNAL, STATE_PT_SCALE, 0, 0, 0 }; 224 static const gl_state_index bias_state[STATE_LENGTH] = 225 { STATE_INTERNAL, STATE_PT_BIAS, 0, 0, 0 }; 226 GLfloat scale[4], bias[4]; 227 GLint scale_p, bias_p; 228 229 scale[0] = ctx->Pixel.RedScale; 230 scale[1] = ctx->Pixel.GreenScale; 231 scale[2] = ctx->Pixel.BlueScale; 232 scale[3] = ctx->Pixel.AlphaScale; 233 bias[0] = ctx->Pixel.RedBias; 234 bias[1] = ctx->Pixel.GreenBias; 235 bias[2] = ctx->Pixel.BlueBias; 236 bias[3] = ctx->Pixel.AlphaBias; 237 238 scale_p = _mesa_add_state_reference(params, scale_state); 239 bias_p = _mesa_add_state_reference(params, bias_state); 240 241 /* MAD colorTemp, colorTemp, scale, bias; */ 242 _mesa_init_instructions(inst + ic, 1); 243 inst[ic].Opcode = OPCODE_MAD; 244 inst[ic].DstReg.File = PROGRAM_TEMPORARY; 245 inst[ic].DstReg.Index = colorTemp; 246 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 247 inst[ic].SrcReg[0].Index = colorTemp; 248 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR; 249 inst[ic].SrcReg[1].Index = scale_p; 250 inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR; 251 inst[ic].SrcReg[2].Index = bias_p; 252 ic++; 253 } 254 255 if (key->pixelMaps) { 256 const GLuint temp = 1; 257 258 /* create the colormap/texture now if not already done */ 259 if (!st->pixel_xfer.pixelmap_texture) { 260 st->pixel_xfer.pixelmap_texture = create_color_map_texture(ctx); 261 } 262 263 /* with a little effort, we can do four pixel map look-ups with 264 * two TEX instructions: 265 */ 266 267 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */ 268 _mesa_init_instructions(inst + ic, 1); 269 inst[ic].Opcode = OPCODE_TEX; 270 inst[ic].DstReg.File = PROGRAM_TEMPORARY; 271 inst[ic].DstReg.Index = temp; 272 inst[ic].DstReg.WriteMask = WRITEMASK_XY; /* write R,G */ 273 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 274 inst[ic].SrcReg[0].Index = colorTemp; 275 inst[ic].TexSrcUnit = 1; 276 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX; 277 ic++; 278 279 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */ 280 _mesa_init_instructions(inst + ic, 1); 281 inst[ic].Opcode = OPCODE_TEX; 282 inst[ic].DstReg.File = PROGRAM_TEMPORARY; 283 inst[ic].DstReg.Index = temp; 284 inst[ic].DstReg.WriteMask = WRITEMASK_ZW; /* write B,A */ 285 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 286 inst[ic].SrcReg[0].Index = colorTemp; 287 inst[ic].SrcReg[0].Swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, 288 SWIZZLE_Z, SWIZZLE_W); 289 inst[ic].TexSrcUnit = 1; 290 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX; 291 ic++; 292 293 /* MOV colorTemp, temp; */ 294 _mesa_init_instructions(inst + ic, 1); 295 inst[ic].Opcode = OPCODE_MOV; 296 inst[ic].DstReg.File = PROGRAM_TEMPORARY; 297 inst[ic].DstReg.Index = colorTemp; 298 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 299 inst[ic].SrcReg[0].Index = temp; 300 ic++; 301 302 fp->Base.SamplersUsed |= (1 << 1); /* sampler 1 is used */ 303 } 304 305 if (key->colorMatrix) { 306 static const gl_state_index row0_state[STATE_LENGTH] = 307 { STATE_COLOR_MATRIX, 0, 0, 0, 0 }; 308 static const gl_state_index row1_state[STATE_LENGTH] = 309 { STATE_COLOR_MATRIX, 0, 1, 1, 0 }; 310 static const gl_state_index row2_state[STATE_LENGTH] = 311 { STATE_COLOR_MATRIX, 0, 2, 2, 0 }; 312 static const gl_state_index row3_state[STATE_LENGTH] = 313 { STATE_COLOR_MATRIX, 0, 3, 3, 0 }; 314 315 GLint row0_p = _mesa_add_state_reference(params, row0_state); 316 GLint row1_p = _mesa_add_state_reference(params, row1_state); 317 GLint row2_p = _mesa_add_state_reference(params, row2_state); 318 GLint row3_p = _mesa_add_state_reference(params, row3_state); 319 const GLuint temp = 1; 320 321 /* DP4 temp.x, colorTemp, matrow0; */ 322 _mesa_init_instructions(inst + ic, 1); 323 inst[ic].Opcode = OPCODE_DP4; 324 inst[ic].DstReg.File = PROGRAM_TEMPORARY; 325 inst[ic].DstReg.Index = temp; 326 inst[ic].DstReg.WriteMask = WRITEMASK_X; 327 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 328 inst[ic].SrcReg[0].Index = colorTemp; 329 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR; 330 inst[ic].SrcReg[1].Index = row0_p; 331 ic++; 332 333 /* DP4 temp.y, colorTemp, matrow1; */ 334 _mesa_init_instructions(inst + ic, 1); 335 inst[ic].Opcode = OPCODE_DP4; 336 inst[ic].DstReg.File = PROGRAM_TEMPORARY; 337 inst[ic].DstReg.Index = temp; 338 inst[ic].DstReg.WriteMask = WRITEMASK_Y; 339 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 340 inst[ic].SrcReg[0].Index = colorTemp; 341 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR; 342 inst[ic].SrcReg[1].Index = row1_p; 343 ic++; 344 345 /* DP4 temp.z, colorTemp, matrow2; */ 346 _mesa_init_instructions(inst + ic, 1); 347 inst[ic].Opcode = OPCODE_DP4; 348 inst[ic].DstReg.File = PROGRAM_TEMPORARY; 349 inst[ic].DstReg.Index = temp; 350 inst[ic].DstReg.WriteMask = WRITEMASK_Z; 351 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 352 inst[ic].SrcReg[0].Index = colorTemp; 353 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR; 354 inst[ic].SrcReg[1].Index = row2_p; 355 ic++; 356 357 /* DP4 temp.w, colorTemp, matrow3; */ 358 _mesa_init_instructions(inst + ic, 1); 359 inst[ic].Opcode = OPCODE_DP4; 360 inst[ic].DstReg.File = PROGRAM_TEMPORARY; 361 inst[ic].DstReg.Index = temp; 362 inst[ic].DstReg.WriteMask = WRITEMASK_W; 363 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 364 inst[ic].SrcReg[0].Index = colorTemp; 365 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR; 366 inst[ic].SrcReg[1].Index = row3_p; 367 ic++; 368 369 /* MOV colorTemp, temp; */ 370 _mesa_init_instructions(inst + ic, 1); 371 inst[ic].Opcode = OPCODE_MOV; 372 inst[ic].DstReg.File = PROGRAM_TEMPORARY; 373 inst[ic].DstReg.Index = colorTemp; 374 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 375 inst[ic].SrcReg[0].Index = temp; 376 ic++; 377 } 378 379 if (key->colorMatrixPostScaleBias) { 380 static const gl_state_index scale_state[STATE_LENGTH] = 381 { STATE_INTERNAL, STATE_PT_SCALE, 0, 0, 0 }; 382 static const gl_state_index bias_state[STATE_LENGTH] = 383 { STATE_INTERNAL, STATE_PT_BIAS, 0, 0, 0 }; 384 GLint scale_param, bias_param; 385 386 scale_param = _mesa_add_state_reference(params, scale_state); 387 bias_param = _mesa_add_state_reference(params, bias_state); 388 389 _mesa_init_instructions(inst + ic, 1); 390 inst[ic].Opcode = OPCODE_MAD; 391 inst[ic].DstReg.File = PROGRAM_TEMPORARY; 392 inst[ic].DstReg.Index = colorTemp; 393 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 394 inst[ic].SrcReg[0].Index = colorTemp; 395 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR; 396 inst[ic].SrcReg[1].Index = scale_param; 397 inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR; 398 inst[ic].SrcReg[2].Index = bias_param; 399 ic++; 400 } 401 402 /* Modify last instruction's dst reg to write to result.color */ 403 { 404 struct prog_instruction *last = &inst[ic - 1]; 405 last->DstReg.File = PROGRAM_OUTPUT; 406 last->DstReg.Index = FRAG_RESULT_COLOR; 407 } 408 409 /* END; */ 410 _mesa_init_instructions(inst + ic, 1); 411 inst[ic].Opcode = OPCODE_END; 412 ic++; 413 414 assert(ic <= MAX_INST); 415 416 417 fp->Base.Instructions = _mesa_alloc_instructions(ic); 418 if (!fp->Base.Instructions) { 419 _mesa_error(ctx, GL_OUT_OF_MEMORY, 420 "generating pixel transfer program"); 421 return NULL; 422 } 423 424 _mesa_copy_instructions(fp->Base.Instructions, inst, ic); 425 fp->Base.NumInstructions = ic; 426 fp->Base.Parameters = params; 427 428#if 0 429 printf("========= pixel transfer prog\n"); 430 _mesa_print_program(&fp->Base); 431 _mesa_print_parameter_list(fp->Base.Parameters); 432#endif 433 434 return fp; 435} 436 437 438 439/** 440 * Update st->pixel_xfer.program in response to new pixel-transfer state. 441 */ 442static void 443update_pixel_transfer(struct st_context *st) 444{ 445 GLcontext *ctx = st->ctx; 446 struct state_key key; 447 struct gl_fragment_program *fp; 448 449 make_state_key(st->ctx, &key); 450 451 fp = (struct gl_fragment_program *) 452 _mesa_search_program_cache(st->pixel_xfer.cache, &key, sizeof(key)); 453 if (!fp) { 454 fp = get_pixel_transfer_program(st->ctx, &key); 455 _mesa_program_cache_insert(st->ctx, st->pixel_xfer.cache, 456 &key, sizeof(key), &fp->Base); 457 } 458 459 if (ctx->Pixel.MapColorFlag) { 460 load_color_map_texture(ctx, st->pixel_xfer.pixelmap_texture); 461 } 462 st->pixel_xfer.pixelmap_enabled = ctx->Pixel.MapColorFlag; 463 464 st->pixel_xfer.program = (struct st_fragment_program *) fp; 465} 466 467 468 469const struct st_tracked_state st_update_pixel_transfer = { 470 "st_update_pixel_transfer", /* name */ 471 { /* dirty */ 472 _NEW_PIXEL | _NEW_COLOR_MATRIX, /* mesa */ 473 0, /* st */ 474 }, 475 update_pixel_transfer /* update */ 476}; 477