st_cb_bitmap.c revision 6f8286163c79a8187c2912a9b673a6f11f4f60c6
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 * Authors: 30 * Brian Paul 31 */ 32 33#include "main/imports.h" 34#include "main/image.h" 35#include "main/bufferobj.h" 36#include "main/macros.h" 37#include "main/texformat.h" 38#include "shader/program.h" 39#include "shader/prog_parameter.h" 40#include "shader/prog_print.h" 41 42#include "st_context.h" 43#include "st_atom.h" 44#include "st_atom_constbuf.h" 45#include "st_program.h" 46#include "st_cb_bitmap.h" 47#include "st_cb_program.h" 48#include "st_mesa_to_tgsi.h" 49#include "st_texture.h" 50#include "pipe/p_context.h" 51#include "pipe/p_defines.h" 52#include "pipe/p_inlines.h" 53#include "pipe/p_winsys.h" 54#include "util/p_tile.h" 55#include "util/u_draw_quad.h" 56#include "util/u_simple_shaders.h" 57#include "shader/prog_instruction.h" 58#include "cso_cache/cso_context.h" 59 60 61 62/** 63 * Make fragment program for glBitmap: 64 * Sample the texture and kill the fragment if the bit is 0. 65 * This program will be combined with the user's fragment program. 66 */ 67static struct st_fragment_program * 68make_bitmap_fragment_program(GLcontext *ctx) 69{ 70 struct st_fragment_program *stfp; 71 struct gl_program *p; 72 GLuint ic = 0; 73 74 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0); 75 if (!p) 76 return NULL; 77 78 p->NumInstructions = 5; 79 80 p->Instructions = _mesa_alloc_instructions(p->NumInstructions); 81 if (!p->Instructions) { 82 ctx->Driver.DeleteProgram(ctx, p); 83 return NULL; 84 } 85 _mesa_init_instructions(p->Instructions, p->NumInstructions); 86 87 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */ 88 p->Instructions[ic].Opcode = OPCODE_TEX; 89 p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY; 90 p->Instructions[ic].DstReg.Index = 0; 91 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT; 92 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0; 93 p->Instructions[ic].TexSrcUnit = 0; 94 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX; 95 ic++; 96 97 /* SWZ tmp0.x, tmp0.x, 1111; # tmp0.x = 1.0 */ 98 p->Instructions[ic].Opcode = OPCODE_SWZ; 99 p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY; 100 p->Instructions[ic].DstReg.Index = 0; 101 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_X; 102 p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 103 p->Instructions[ic].SrcReg[0].Index = 0; 104 p->Instructions[ic].SrcReg[0].Swizzle 105 = MAKE_SWIZZLE4(SWIZZLE_ONE, SWIZZLE_ONE, SWIZZLE_ONE, SWIZZLE_ONE ); 106 ic++; 107 108 /* SUB tmp0, tmp0.wwww, tmp0.xxxx; # tmp0.w -= 1 */ 109 p->Instructions[ic].Opcode = OPCODE_SUB; 110 p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY; 111 p->Instructions[ic].DstReg.Index = 0; 112 p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 113 p->Instructions[ic].SrcReg[0].Index = 0; 114 p->Instructions[ic].SrcReg[0].Swizzle = SWIZZLE_WWWW; 115 p->Instructions[ic].SrcReg[1].File = PROGRAM_TEMPORARY; 116 p->Instructions[ic].SrcReg[1].Index = 0; 117 p->Instructions[ic].SrcReg[1].Swizzle = SWIZZLE_XXXX; /* 1.0 */ 118 ic++; 119 120 /* KIL if tmp0 < 0 */ 121 p->Instructions[ic].Opcode = OPCODE_KIL; 122 p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY; 123 p->Instructions[ic].SrcReg[0].Index = 0; 124 ic++; 125 126 /* END; */ 127 p->Instructions[ic++].Opcode = OPCODE_END; 128 129 assert(ic == p->NumInstructions); 130 131 p->InputsRead = FRAG_BIT_TEX0; 132 p->OutputsWritten = 0x0; 133 134 stfp = (struct st_fragment_program *) p; 135 stfp->Base.UsesKill = GL_TRUE; 136 st_translate_fragment_program(ctx->st, stfp, NULL); 137 138 return stfp; 139} 140 141 142/** 143 * Combine basic bitmap fragment program with the user-defined program. 144 */ 145static struct st_fragment_program * 146combined_bitmap_fragment_program(GLcontext *ctx) 147{ 148 struct st_context *st = ctx->st; 149 struct st_fragment_program *stfp; 150 151 if (!st->bitmap.program) { 152 /* create the basic bitmap fragment program */ 153 st->bitmap.program = make_bitmap_fragment_program(ctx); 154 } 155 156 if (st->bitmap.user_prog_sn == st->fp->serialNo) { 157 /* re-use */ 158 stfp = st->bitmap.combined_prog; 159 } 160 else { 161 /* Concatenate the bitmap program with the current user-defined program. 162 */ 163 stfp = (struct st_fragment_program *) 164 _mesa_combine_programs(ctx, 165 &st->bitmap.program->Base.Base, 166 &st->fp->Base.Base); 167 168#if 0 169 { 170 struct gl_program *p = &stfp->Base.Base; 171 printf("Combined bitmap program:\n"); 172 _mesa_print_program(p); 173 printf("InputsRead: 0x%x\n", p->InputsRead); 174 printf("OutputsWritten: 0x%x\n", p->OutputsWritten); 175 _mesa_print_parameter_list(p->Parameters); 176 } 177#endif 178 179 /* translate to TGSI tokens */ 180 st_translate_fragment_program(st, stfp, NULL); 181 182 /* save new program, update serial numbers */ 183 st->bitmap.user_prog_sn = st->fp->serialNo; 184 st->bitmap.combined_prog = stfp; 185 } 186 187 /* Ideally we'd have updated the pipe constants during the normal 188 * st/atom mechanism. But we can't since this is specific to glBitmap. 189 */ 190 st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT); 191 192 return stfp; 193} 194 195 196/** 197 * Create a texture which represents a bitmap image. 198 */ 199static struct pipe_texture * 200make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height, 201 const struct gl_pixelstore_attrib *unpack, 202 const GLubyte *bitmap) 203{ 204 struct pipe_context *pipe = ctx->st->pipe; 205 struct pipe_screen *screen = pipe->screen; 206 struct pipe_surface *surface; 207 uint format = 0, cpp, comp; 208 ubyte *dest; 209 struct pipe_texture *pt; 210 int row, col; 211 212 /* find a texture format we know */ 213 if (screen->is_format_supported( screen, PIPE_FORMAT_U_I8, PIPE_TEXTURE )) { 214 format = PIPE_FORMAT_U_I8; 215 cpp = 1; 216 comp = 0; 217 } 218 else if (screen->is_format_supported( screen, PIPE_FORMAT_A8R8G8B8_UNORM, PIPE_TEXTURE )) { 219 format = PIPE_FORMAT_A8R8G8B8_UNORM; 220 cpp = 4; 221 comp = 3; /* alpha channel */ /*XXX little-endian dependency */ 222 } 223 else { 224 /* XXX support more formats */ 225 assert( 0 ); 226 } 227 228 /* PBO source... */ 229 bitmap = _mesa_map_bitmap_pbo(ctx, unpack, bitmap); 230 if (!bitmap) { 231 return NULL; 232 } 233 234 /** 235 * Create texture to hold bitmap pattern. 236 */ 237 pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, format, 0, width, height, 238 1, 0); 239 if (!pt) { 240 _mesa_unmap_bitmap_pbo(ctx, unpack); 241 return NULL; 242 } 243 244 surface = screen->get_tex_surface(screen, pt, 0, 0, 0); 245 246 /* map texture surface */ 247 dest = pipe_surface_map(surface); 248 249 /* Put image into texture surface. 250 * Note that the image is actually going to be upside down in 251 * the texture. We deal with that with texcoords. 252 */ 253 254 for (row = 0; row < height; row++) { 255 const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack, 256 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0); 257 ubyte *destRow = dest + row * surface->pitch * cpp; 258 259 if (unpack->LsbFirst) { 260 /* Lsb first */ 261 GLubyte mask = 1U << (unpack->SkipPixels & 0x7); 262 for (col = 0; col < width; col++) { 263 264 /* set texel to 255 if bit is set */ 265 destRow[comp] = (*src & mask) ? 255 : 0; 266 destRow += cpp; 267 268 if (mask == 128U) { 269 src++; 270 mask = 1U; 271 } 272 else { 273 mask = mask << 1; 274 } 275 } 276 277 /* get ready for next row */ 278 if (mask != 1) 279 src++; 280 } 281 else { 282 /* Msb first */ 283 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7); 284 for (col = 0; col < width; col++) { 285 286 /* set texel to 255 if bit is set */ 287 destRow[comp] =(*src & mask) ? 255 : 0; 288 destRow += cpp; 289 290 if (mask == 1U) { 291 src++; 292 mask = 128U; 293 } 294 else { 295 mask = mask >> 1; 296 } 297 } 298 299 /* get ready for next row */ 300 if (mask != 128) 301 src++; 302 } 303 304 } /* row */ 305 306 _mesa_unmap_bitmap_pbo(ctx, unpack); 307 308 /* Release surface */ 309 pipe_surface_unmap(surface); 310 pipe_surface_reference(&surface, NULL); 311 pipe->texture_update(pipe, pt, 0, 0x1); 312 313 pt->format = format; 314 315 return pt; 316} 317 318 319static void 320setup_bitmap_vertex_data(struct st_context *st, 321 int x, int y, int width, int height, 322 float z, const float color[4]) 323{ 324 struct pipe_context *pipe = st->pipe; 325 const struct gl_framebuffer *fb = st->ctx->DrawBuffer; 326 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP); 327 const GLfloat x0 = x; 328 const GLfloat x1 = x + width; 329 const GLfloat y0 = invert ? (fb->Height - y - height) : y; 330 const GLfloat y1 = invert ? (y0 + height) : y + height; 331 const GLfloat bias = st->bitmap_texcoord_bias; 332 const GLfloat xBias = bias / (x1-x0); 333 const GLfloat yBias = bias / (y1-y0); 334 const GLfloat sLeft = 0.0 + xBias, sRight = 1.0 + xBias; 335 const GLfloat tTop = 1.0 - yBias, tBot = 1.0 - tTop - yBias; 336 GLuint i; 337 void *buf; 338 339 if (!st->bitmap.vbuf) { 340 st->bitmap.vbuf = pipe->winsys->buffer_create(pipe->winsys, 32, 341 PIPE_BUFFER_USAGE_VERTEX, 342 sizeof(st->bitmap.vertices)); 343 } 344 345 /* positions, texcoords */ 346 st->bitmap.vertices[0][0][0] = x0; 347 st->bitmap.vertices[0][0][1] = y0; 348 st->bitmap.vertices[0][2][0] = sLeft; 349 st->bitmap.vertices[0][2][1] = tTop; 350 351 st->bitmap.vertices[1][0][0] = x1; 352 st->bitmap.vertices[1][0][1] = y0; 353 st->bitmap.vertices[1][2][0] = sRight; 354 st->bitmap.vertices[1][2][1] = tTop; 355 356 st->bitmap.vertices[2][0][0] = x1; 357 st->bitmap.vertices[2][0][1] = y1; 358 st->bitmap.vertices[2][2][0] = sRight; 359 st->bitmap.vertices[2][2][1] = tBot; 360 361 st->bitmap.vertices[3][0][0] = x0; 362 st->bitmap.vertices[3][0][1] = y1; 363 st->bitmap.vertices[3][2][0] = sLeft; 364 st->bitmap.vertices[3][2][1] = tBot; 365 366 /* same for all verts: */ 367 for (i = 0; i < 4; i++) { 368 st->bitmap.vertices[i][0][2] = z; 369 st->bitmap.vertices[i][0][3] = 1.0; 370 st->bitmap.vertices[i][1][0] = color[0]; 371 st->bitmap.vertices[i][1][1] = color[1]; 372 st->bitmap.vertices[i][1][2] = color[2]; 373 st->bitmap.vertices[i][1][3] = color[3]; 374 st->bitmap.vertices[i][2][2] = 0.0; /*R*/ 375 st->bitmap.vertices[i][2][3] = 1.0; /*Q*/ 376 } 377 378 /* put vertex data into vbuf */ 379 buf = pipe->winsys->buffer_map(pipe->winsys, st->bitmap.vbuf, 380 PIPE_BUFFER_USAGE_CPU_WRITE); 381 memcpy(buf, st->bitmap.vertices, sizeof(st->bitmap.vertices)); 382 pipe->winsys->buffer_unmap(pipe->winsys, st->bitmap.vbuf); 383} 384 385 386 387/** 388 * Render a glBitmap by drawing a textured quad 389 */ 390static void 391draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z, 392 GLsizei width, GLsizei height, 393 struct pipe_texture *pt, 394 struct st_fragment_program *stfp) 395{ 396 struct st_context *st = ctx->st; 397 struct pipe_context *pipe = ctx->st->pipe; 398 struct cso_context *cso = ctx->st->cso_context; 399 GLuint maxSize; 400 401 /* limit checks */ 402 /* XXX if DrawPixels image is larger than max texture size, break 403 * it up into chunks. 404 */ 405 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1); 406 assert(width <= maxSize); 407 assert(height <= maxSize); 408 409 cso_save_rasterizer(cso); 410 cso_save_samplers(cso); 411 412 /* rasterizer state: just scissor */ 413 { 414 struct pipe_rasterizer_state rasterizer; 415 memset(&rasterizer, 0, sizeof(rasterizer)); 416 if (ctx->Scissor.Enabled) 417 rasterizer.scissor = 1; 418 rasterizer.bypass_clipping = 1; 419 420 cso_set_rasterizer(cso, &rasterizer); 421 } 422 423 /* fragment shader state: TEX lookup program */ 424 pipe->bind_fs_state(pipe, stfp->driver_shader); 425 426 /* vertex shader state: position + texcoord pass-through */ 427 pipe->bind_vs_state(pipe, st->bitmap.vs); 428 429 /* sampler / texture state */ 430 { 431 struct pipe_sampler_state sampler; 432 memset(&sampler, 0, sizeof(sampler)); 433 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP; 434 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP; 435 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP; 436 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 437 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 438 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 439 sampler.normalized_coords = 1; 440 441 cso_single_sampler(cso, 0, &sampler); 442 cso_single_sampler_done(cso); 443 444 pipe->set_sampler_textures(pipe, 1, &pt); 445 } 446 447 /* draw textured quad */ 448 setup_bitmap_vertex_data(st, x, y, width, height, 449 ctx->Current.RasterPos[2], 450 ctx->Current.RasterColor); 451 452 util_draw_vertex_buffer(pipe, st->bitmap.vbuf, 453 PIPE_PRIM_TRIANGLE_FAN, 454 4, /* verts */ 455 3); /* attribs/vert */ 456 457 458 /* restore state */ 459 cso_restore_rasterizer(cso); 460 cso_restore_samplers(cso); 461 /* shaders don't go through cso yet */ 462 pipe->bind_fs_state(pipe, st->fp->driver_shader); 463 pipe->bind_vs_state(pipe, st->vp->driver_shader); 464 pipe->set_sampler_textures(pipe, ctx->st->state.num_textures, 465 ctx->st->state.sampler_texture); 466} 467 468 469 470static void 471st_Bitmap(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height, 472 const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap ) 473{ 474 struct st_fragment_program *stfp; 475 struct st_context *st = ctx->st; 476 struct pipe_texture *pt; 477 478 st_validate_state(st); 479 480 stfp = combined_bitmap_fragment_program(ctx); 481 482 if (!st->bitmap.vs) { 483 /* create pass-through vertex shader now */ 484 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION, 485 TGSI_SEMANTIC_COLOR, 486 TGSI_SEMANTIC_GENERIC }; 487 const uint semantic_indexes[] = { 0, 0, 0 }; 488 st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3, 489 semantic_names, 490 semantic_indexes, 491 &st->bitmap.vert_shader); 492 } 493 494 st_validate_state(st); 495 496 pt = make_bitmap_texture(ctx, width, height, unpack, bitmap); 497 if (pt) { 498 assert(pt->target == PIPE_TEXTURE_2D); 499 draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2], 500 width, height, 501 pt, stfp); 502 pipe_texture_reference(&pt, NULL); 503 } 504} 505 506 507 508void st_init_bitmap_functions(struct dd_function_table *functions) 509{ 510 functions->Bitmap = st_Bitmap; 511} 512 513 514void 515st_destroy_bitmap(struct st_context *st) 516{ 517 struct pipe_context *pipe = st->pipe; 518 519 if (st->bitmap.combined_prog) { 520 st_delete_program(st->ctx, &st->bitmap.combined_prog->Base.Base); 521 } 522 523 if (st->bitmap.program) { 524 st_delete_program(st->ctx, &st->bitmap.program->Base.Base); 525 } 526 527 if (st->bitmap.vs) { 528 pipe->delete_vs_state(pipe, st->bitmap.vs); 529 st->bitmap.vs = NULL; 530 } 531 if (st->bitmap.vbuf) { 532 pipe->winsys->buffer_destroy(pipe->winsys, st->bitmap.vbuf); 533 st->bitmap.vbuf = NULL; 534 } 535} 536 537