st_cb_drawpixels.c revision 738d1ae3b59b073605c7fb58ec6f0a85a185ffd3
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/format_pack.h" 37#include "main/macros.h" 38#include "main/mfeatures.h" 39#include "main/mtypes.h" 40#include "main/pack.h" 41#include "main/pbo.h" 42#include "main/readpix.h" 43#include "main/texformat.h" 44#include "main/teximage.h" 45#include "main/texstore.h" 46#include "main/glformats.h" 47#include "program/program.h" 48#include "program/prog_print.h" 49#include "program/prog_instruction.h" 50 51#include "st_atom.h" 52#include "st_atom_constbuf.h" 53#include "st_cb_drawpixels.h" 54#include "st_cb_readpixels.h" 55#include "st_cb_fbo.h" 56#include "st_context.h" 57#include "st_debug.h" 58#include "st_format.h" 59#include "st_program.h" 60#include "st_texture.h" 61 62#include "pipe/p_context.h" 63#include "pipe/p_defines.h" 64#include "tgsi/tgsi_ureg.h" 65#include "util/u_draw_quad.h" 66#include "util/u_format.h" 67#include "util/u_inlines.h" 68#include "util/u_math.h" 69#include "util/u_tile.h" 70#include "util/u_upload_mgr.h" 71#include "cso_cache/cso_context.h" 72 73 74#if FEATURE_drawpix 75 76/** 77 * Check if the given program is: 78 * 0: MOVE result.color, fragment.color; 79 * 1: END; 80 */ 81static GLboolean 82is_passthrough_program(const struct gl_fragment_program *prog) 83{ 84 if (prog->Base.NumInstructions == 2) { 85 const struct prog_instruction *inst = prog->Base.Instructions; 86 if (inst[0].Opcode == OPCODE_MOV && 87 inst[1].Opcode == OPCODE_END && 88 inst[0].DstReg.File == PROGRAM_OUTPUT && 89 inst[0].DstReg.Index == FRAG_RESULT_COLOR && 90 inst[0].DstReg.WriteMask == WRITEMASK_XYZW && 91 inst[0].SrcReg[0].File == PROGRAM_INPUT && 92 inst[0].SrcReg[0].Index == FRAG_ATTRIB_COL0 && 93 inst[0].SrcReg[0].Swizzle == SWIZZLE_XYZW) { 94 return GL_TRUE; 95 } 96 } 97 return GL_FALSE; 98} 99 100 101/** 102 * Returns a fragment program which implements the current pixel transfer ops. 103 */ 104static struct gl_fragment_program * 105get_glsl_pixel_transfer_program(struct st_context *st, 106 struct st_fragment_program *orig) 107{ 108 int pixelMaps = 0, scaleAndBias = 0; 109 struct gl_context *ctx = st->ctx; 110 struct st_fragment_program *fp = (struct st_fragment_program *) 111 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0); 112 113 if (!fp) 114 return NULL; 115 116 if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 || 117 ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 || 118 ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 || 119 ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) { 120 scaleAndBias = 1; 121 } 122 123 pixelMaps = ctx->Pixel.MapColorFlag; 124 125 if (pixelMaps) { 126 /* create the colormap/texture now if not already done */ 127 if (!st->pixel_xfer.pixelmap_texture) { 128 st->pixel_xfer.pixelmap_texture = st_create_color_map_texture(ctx); 129 st->pixel_xfer.pixelmap_sampler_view = 130 st_create_texture_sampler_view(st->pipe, 131 st->pixel_xfer.pixelmap_texture); 132 } 133 } 134 135 get_pixel_transfer_visitor(fp, orig->glsl_to_tgsi, 136 scaleAndBias, pixelMaps); 137 138 return &fp->Base; 139} 140 141 142/** 143 * Make fragment shader for glDraw/CopyPixels. This shader is made 144 * by combining the pixel transfer shader with the user-defined shader. 145 * \param fpIn the current/incoming fragment program 146 * \param fpOut returns the combined fragment program 147 */ 148void 149st_make_drawpix_fragment_program(struct st_context *st, 150 struct gl_fragment_program *fpIn, 151 struct gl_fragment_program **fpOut) 152{ 153 struct gl_program *newProg; 154 struct st_fragment_program *stfp = (struct st_fragment_program *) fpIn; 155 156 if (is_passthrough_program(fpIn)) { 157 newProg = (struct gl_program *) _mesa_clone_fragment_program(st->ctx, 158 &st->pixel_xfer.program->Base); 159 } 160 else if (stfp->glsl_to_tgsi != NULL) { 161 newProg = (struct gl_program *) get_glsl_pixel_transfer_program(st, stfp); 162 } 163 else { 164#if 0 165 /* debug */ 166 printf("Base program:\n"); 167 _mesa_print_program(&fpIn->Base); 168 printf("DrawPix program:\n"); 169 _mesa_print_program(&st->pixel_xfer.program->Base.Base); 170#endif 171 newProg = _mesa_combine_programs(st->ctx, 172 &st->pixel_xfer.program->Base.Base, 173 &fpIn->Base); 174 } 175 176#if 0 177 /* debug */ 178 printf("Combined DrawPixels program:\n"); 179 _mesa_print_program(newProg); 180 printf("InputsRead: 0x%x\n", newProg->InputsRead); 181 printf("OutputsWritten: 0x%x\n", newProg->OutputsWritten); 182 _mesa_print_parameter_list(newProg->Parameters); 183#endif 184 185 *fpOut = (struct gl_fragment_program *) newProg; 186} 187 188 189/** 190 * Create fragment program that does a TEX() instruction to get a Z and/or 191 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL. 192 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX). 193 * Pass fragment color through as-is. 194 * \return pointer to the gl_fragment program 195 */ 196struct gl_fragment_program * 197st_make_drawpix_z_stencil_program(struct st_context *st, 198 GLboolean write_depth, 199 GLboolean write_stencil) 200{ 201 struct gl_context *ctx = st->ctx; 202 struct gl_program *p; 203 struct gl_fragment_program *fp; 204 GLuint ic = 0; 205 const GLuint shaderIndex = write_depth * 2 + write_stencil; 206 207 assert(shaderIndex < Elements(st->drawpix.shaders)); 208 209 if (st->drawpix.shaders[shaderIndex]) { 210 /* already have the proper shader */ 211 return st->drawpix.shaders[shaderIndex]; 212 } 213 214 /* 215 * Create shader now 216 */ 217 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0); 218 if (!p) 219 return NULL; 220 221 p->NumInstructions = write_depth ? 3 : 1; 222 p->NumInstructions += write_stencil ? 1 : 0; 223 224 p->Instructions = _mesa_alloc_instructions(p->NumInstructions); 225 if (!p->Instructions) { 226 ctx->Driver.DeleteProgram(ctx, p); 227 return NULL; 228 } 229 _mesa_init_instructions(p->Instructions, p->NumInstructions); 230 231 if (write_depth) { 232 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */ 233 p->Instructions[ic].Opcode = OPCODE_TEX; 234 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT; 235 p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH; 236 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z; 237 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT; 238 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0; 239 p->Instructions[ic].TexSrcUnit = 0; 240 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX; 241 ic++; 242 /* MOV result.color, fragment.color; */ 243 p->Instructions[ic].Opcode = OPCODE_MOV; 244 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT; 245 p->Instructions[ic].DstReg.Index = FRAG_RESULT_COLOR; 246 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT; 247 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_COL0; 248 ic++; 249 } 250 251 if (write_stencil) { 252 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */ 253 p->Instructions[ic].Opcode = OPCODE_TEX; 254 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT; 255 p->Instructions[ic].DstReg.Index = FRAG_RESULT_STENCIL; 256 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Y; 257 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT; 258 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0; 259 p->Instructions[ic].TexSrcUnit = 1; 260 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX; 261 ic++; 262 } 263 264 /* END; */ 265 p->Instructions[ic++].Opcode = OPCODE_END; 266 267 assert(ic == p->NumInstructions); 268 269 p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0; 270 p->OutputsWritten = 0; 271 if (write_depth) { 272 p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_DEPTH); 273 p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_COLOR); 274 } 275 if (write_stencil) 276 p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_STENCIL); 277 278 p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */ 279 if (write_stencil) 280 p->SamplersUsed |= 1 << 1; 281 282 fp = (struct gl_fragment_program *) p; 283 284 /* save the new shader */ 285 st->drawpix.shaders[shaderIndex] = fp; 286 287 return fp; 288} 289 290 291/** 292 * Create a simple vertex shader that just passes through the 293 * vertex position and texcoord (and optionally, color). 294 */ 295static void * 296make_passthrough_vertex_shader(struct st_context *st, 297 GLboolean passColor) 298{ 299 if (!st->drawpix.vert_shaders[passColor]) { 300 struct ureg_program *ureg = ureg_create( TGSI_PROCESSOR_VERTEX ); 301 302 if (ureg == NULL) 303 return NULL; 304 305 /* MOV result.pos, vertex.pos; */ 306 ureg_MOV(ureg, 307 ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ), 308 ureg_DECL_vs_input( ureg, 0 )); 309 310 /* MOV result.texcoord0, vertex.attr[1]; */ 311 ureg_MOV(ureg, 312 ureg_DECL_output( ureg, TGSI_SEMANTIC_GENERIC, 0 ), 313 ureg_DECL_vs_input( ureg, 1 )); 314 315 if (passColor) { 316 /* MOV result.color0, vertex.attr[2]; */ 317 ureg_MOV(ureg, 318 ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ), 319 ureg_DECL_vs_input( ureg, 2 )); 320 } 321 322 ureg_END( ureg ); 323 324 st->drawpix.vert_shaders[passColor] = 325 ureg_create_shader_and_destroy( ureg, st->pipe ); 326 } 327 328 return st->drawpix.vert_shaders[passColor]; 329} 330 331 332/** 333 * Return a texture internalFormat for drawing/copying an image 334 * of the given format and type. 335 */ 336static GLenum 337internal_format(struct gl_context *ctx, GLenum format, GLenum type) 338{ 339 switch (format) { 340 case GL_DEPTH_COMPONENT: 341 switch (type) { 342 case GL_UNSIGNED_SHORT: 343 return GL_DEPTH_COMPONENT16; 344 345 case GL_UNSIGNED_INT: 346 return GL_DEPTH_COMPONENT32; 347 348 case GL_FLOAT: 349 if (ctx->Extensions.ARB_depth_buffer_float) 350 return GL_DEPTH_COMPONENT32F; 351 else 352 return GL_DEPTH_COMPONENT; 353 354 default: 355 return GL_DEPTH_COMPONENT; 356 } 357 358 case GL_DEPTH_STENCIL: 359 switch (type) { 360 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: 361 return GL_DEPTH32F_STENCIL8; 362 363 case GL_UNSIGNED_INT_24_8: 364 default: 365 return GL_DEPTH24_STENCIL8; 366 } 367 368 case GL_STENCIL_INDEX: 369 return GL_STENCIL_INDEX; 370 371 default: 372 if (_mesa_is_enum_format_integer(format)) { 373 switch (type) { 374 case GL_BYTE: 375 return GL_RGBA8I; 376 case GL_UNSIGNED_BYTE: 377 return GL_RGBA8UI; 378 case GL_SHORT: 379 return GL_RGBA16I; 380 case GL_UNSIGNED_SHORT: 381 return GL_RGBA16UI; 382 case GL_INT: 383 return GL_RGBA32I; 384 case GL_UNSIGNED_INT: 385 return GL_RGBA32UI; 386 default: 387 assert(0 && "Unexpected type in internal_format()"); 388 return GL_RGBA_INTEGER; 389 } 390 } 391 else { 392 switch (type) { 393 case GL_UNSIGNED_BYTE: 394 case GL_UNSIGNED_INT_8_8_8_8: 395 case GL_UNSIGNED_INT_8_8_8_8_REV: 396 default: 397 return GL_RGBA8; 398 399 case GL_UNSIGNED_BYTE_3_3_2: 400 case GL_UNSIGNED_BYTE_2_3_3_REV: 401 return GL_R3_G3_B2; 402 403 case GL_UNSIGNED_SHORT_4_4_4_4: 404 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 405 return GL_RGBA4; 406 407 case GL_UNSIGNED_SHORT_5_6_5: 408 case GL_UNSIGNED_SHORT_5_6_5_REV: 409 return GL_RGB565; 410 411 case GL_UNSIGNED_SHORT_5_5_5_1: 412 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 413 return GL_RGB5_A1; 414 415 case GL_UNSIGNED_INT_10_10_10_2: 416 case GL_UNSIGNED_INT_2_10_10_10_REV: 417 return GL_RGB10_A2; 418 419 case GL_UNSIGNED_SHORT: 420 case GL_UNSIGNED_INT: 421 return GL_RGBA16; 422 423 case GL_BYTE: 424 return 425 ctx->Extensions.EXT_texture_snorm ? GL_RGBA8_SNORM : GL_RGBA8; 426 427 case GL_SHORT: 428 case GL_INT: 429 return 430 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16; 431 432 case GL_HALF_FLOAT_ARB: 433 return 434 ctx->Extensions.ARB_texture_float ? GL_RGBA16F : 435 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16; 436 437 case GL_FLOAT: 438 case GL_DOUBLE: 439 return 440 ctx->Extensions.ARB_texture_float ? GL_RGBA32F : 441 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16; 442 443 case GL_UNSIGNED_INT_5_9_9_9_REV: 444 assert(ctx->Extensions.EXT_texture_shared_exponent); 445 return GL_RGB9_E5; 446 447 case GL_UNSIGNED_INT_10F_11F_11F_REV: 448 assert(ctx->Extensions.EXT_packed_float); 449 return GL_R11F_G11F_B10F; 450 } 451 } 452 } 453} 454 455 456/** 457 * Create a temporary texture to hold an image of the given size. 458 * If width, height are not POT and the driver only handles POT textures, 459 * allocate the next larger size of texture that is POT. 460 */ 461static struct pipe_resource * 462alloc_texture(struct st_context *st, GLsizei width, GLsizei height, 463 enum pipe_format texFormat) 464{ 465 struct pipe_resource *pt; 466 467 pt = st_texture_create(st, st->internal_target, texFormat, 0, 468 width, height, 1, 1, PIPE_BIND_SAMPLER_VIEW); 469 470 return pt; 471} 472 473 474/** 475 * Make texture containing an image for glDrawPixels image. 476 * If 'pixels' is NULL, leave the texture image data undefined. 477 */ 478static struct pipe_resource * 479make_texture(struct st_context *st, 480 GLsizei width, GLsizei height, GLenum format, GLenum type, 481 const struct gl_pixelstore_attrib *unpack, 482 const GLvoid *pixels) 483{ 484 struct gl_context *ctx = st->ctx; 485 struct pipe_context *pipe = st->pipe; 486 gl_format mformat; 487 struct pipe_resource *pt; 488 enum pipe_format pipeFormat; 489 GLenum baseInternalFormat, intFormat; 490 491 intFormat = internal_format(ctx, format, type); 492 baseInternalFormat = _mesa_base_tex_format(ctx, intFormat); 493 494 mformat = st_ChooseTextureFormat_renderable(ctx, intFormat, 495 format, type, GL_FALSE); 496 assert(mformat); 497 498 pipeFormat = st_mesa_format_to_pipe_format(mformat); 499 assert(pipeFormat); 500 501 pixels = _mesa_map_pbo_source(ctx, unpack, pixels); 502 if (!pixels) 503 return NULL; 504 505 /* alloc temporary texture */ 506 pt = alloc_texture(st, width, height, pipeFormat); 507 if (!pt) { 508 _mesa_unmap_pbo_source(ctx, unpack); 509 return NULL; 510 } 511 512 { 513 struct pipe_transfer *transfer; 514 GLboolean success; 515 GLubyte *dest; 516 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState; 517 518 /* we'll do pixel transfer in a fragment shader */ 519 ctx->_ImageTransferState = 0x0; 520 521 transfer = pipe_get_transfer(st->pipe, pt, 0, 0, 522 PIPE_TRANSFER_WRITE, 0, 0, 523 width, height); 524 525 /* map texture transfer */ 526 dest = pipe_transfer_map(pipe, transfer); 527 528 529 /* Put image into texture transfer. 530 * Note that the image is actually going to be upside down in 531 * the texture. We deal with that with texcoords. 532 */ 533 success = _mesa_texstore(ctx, 2, /* dims */ 534 baseInternalFormat, /* baseInternalFormat */ 535 mformat, /* gl_format */ 536 transfer->stride, /* dstRowStride, bytes */ 537 &dest, /* destSlices */ 538 width, height, 1, /* size */ 539 format, type, /* src format/type */ 540 pixels, /* data source */ 541 unpack); 542 543 /* unmap */ 544 pipe_transfer_unmap(pipe, transfer); 545 pipe->transfer_destroy(pipe, transfer); 546 547 assert(success); 548 549 /* restore */ 550 ctx->_ImageTransferState = imageTransferStateSave; 551 } 552 553 _mesa_unmap_pbo_source(ctx, unpack); 554 555 return pt; 556} 557 558 559/** 560 * Draw quad with texcoords and optional color. 561 * Coords are gallium window coords with y=0=top. 562 * \param color may be null 563 * \param invertTex if true, flip texcoords vertically 564 */ 565static void 566draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z, 567 GLfloat x1, GLfloat y1, const GLfloat *color, 568 GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord) 569{ 570 struct st_context *st = st_context(ctx); 571 struct pipe_context *pipe = st->pipe; 572 GLfloat (*verts)[3][4]; /* four verts, three attribs, XYZW */ 573 struct pipe_resource *buf = NULL; 574 unsigned offset; 575 576 u_upload_alloc(st->uploader, 0, 4 * sizeof(verts[0]), &offset, &buf, 577 (void**)&verts); 578 if (!buf) { 579 return; 580 } 581 582 /* setup vertex data */ 583 { 584 const struct gl_framebuffer *fb = st->ctx->DrawBuffer; 585 const GLfloat fb_width = (GLfloat) fb->Width; 586 const GLfloat fb_height = (GLfloat) fb->Height; 587 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f; 588 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f; 589 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f; 590 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f; 591 const GLfloat sLeft = 0.0f, sRight = maxXcoord; 592 const GLfloat tTop = invertTex ? maxYcoord : 0.0f; 593 const GLfloat tBot = invertTex ? 0.0f : maxYcoord; 594 GLuint i; 595 596 /* upper-left */ 597 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */ 598 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */ 599 600 /* upper-right */ 601 verts[1][0][0] = clip_x1; 602 verts[1][0][1] = clip_y0; 603 604 /* lower-right */ 605 verts[2][0][0] = clip_x1; 606 verts[2][0][1] = clip_y1; 607 608 /* lower-left */ 609 verts[3][0][0] = clip_x0; 610 verts[3][0][1] = clip_y1; 611 612 verts[0][1][0] = sLeft; /* v[0].attr[1].S */ 613 verts[0][1][1] = tTop; /* v[0].attr[1].T */ 614 verts[1][1][0] = sRight; 615 verts[1][1][1] = tTop; 616 verts[2][1][0] = sRight; 617 verts[2][1][1] = tBot; 618 verts[3][1][0] = sLeft; 619 verts[3][1][1] = tBot; 620 621 /* same for all verts: */ 622 if (color) { 623 for (i = 0; i < 4; i++) { 624 verts[i][0][2] = z; /* v[i].attr[0].z */ 625 verts[i][0][3] = 1.0f; /* v[i].attr[0].w */ 626 verts[i][2][0] = color[0]; /* v[i].attr[2].r */ 627 verts[i][2][1] = color[1]; /* v[i].attr[2].g */ 628 verts[i][2][2] = color[2]; /* v[i].attr[2].b */ 629 verts[i][2][3] = color[3]; /* v[i].attr[2].a */ 630 verts[i][1][2] = 0.0f; /* v[i].attr[1].R */ 631 verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */ 632 } 633 } 634 else { 635 for (i = 0; i < 4; i++) { 636 verts[i][0][2] = z; /*Z*/ 637 verts[i][0][3] = 1.0f; /*W*/ 638 verts[i][1][2] = 0.0f; /*R*/ 639 verts[i][1][3] = 1.0f; /*Q*/ 640 } 641 } 642 } 643 644 u_upload_unmap(st->uploader); 645 util_draw_vertex_buffer(pipe, st->cso_context, buf, offset, 646 PIPE_PRIM_QUADS, 647 4, /* verts */ 648 3); /* attribs/vert */ 649 pipe_resource_reference(&buf, NULL); 650} 651 652 653 654static void 655draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z, 656 GLsizei width, GLsizei height, 657 GLfloat zoomX, GLfloat zoomY, 658 struct pipe_sampler_view **sv, 659 int num_sampler_view, 660 void *driver_vp, 661 void *driver_fp, 662 const GLfloat *color, 663 GLboolean invertTex, 664 GLboolean write_depth, GLboolean write_stencil) 665{ 666 struct st_context *st = st_context(ctx); 667 struct pipe_context *pipe = st->pipe; 668 struct cso_context *cso = st->cso_context; 669 GLfloat x0, y0, x1, y1; 670 GLsizei maxSize; 671 boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT; 672 673 /* limit checks */ 674 /* XXX if DrawPixels image is larger than max texture size, break 675 * it up into chunks. 676 */ 677 maxSize = 1 << (pipe->screen->get_param(pipe->screen, 678 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1); 679 assert(width <= maxSize); 680 assert(height <= maxSize); 681 682 cso_save_rasterizer(cso); 683 cso_save_viewport(cso); 684 cso_save_samplers(cso, PIPE_SHADER_FRAGMENT); 685 cso_save_sampler_views(cso, PIPE_SHADER_FRAGMENT); 686 cso_save_fragment_shader(cso); 687 cso_save_stream_outputs(cso); 688 cso_save_vertex_shader(cso); 689 cso_save_geometry_shader(cso); 690 cso_save_vertex_elements(cso); 691 cso_save_vertex_buffers(cso); 692 if (write_stencil) { 693 cso_save_depth_stencil_alpha(cso); 694 cso_save_blend(cso); 695 } 696 697 /* rasterizer state: just scissor */ 698 { 699 struct pipe_rasterizer_state rasterizer; 700 memset(&rasterizer, 0, sizeof(rasterizer)); 701 rasterizer.clamp_fragment_color = !st->clamp_frag_color_in_shader && 702 ctx->Color._ClampFragmentColor && 703 !ctx->DrawBuffer->_IntegerColor; 704 rasterizer.gl_rasterization_rules = 1; 705 rasterizer.depth_clip = !ctx->Transform.DepthClamp; 706 rasterizer.scissor = ctx->Scissor.Enabled; 707 cso_set_rasterizer(cso, &rasterizer); 708 } 709 710 if (write_stencil) { 711 /* Stencil writing bypasses the normal fragment pipeline to 712 * disable color writing and set stencil test to always pass. 713 */ 714 struct pipe_depth_stencil_alpha_state dsa; 715 struct pipe_blend_state blend; 716 717 /* depth/stencil */ 718 memset(&dsa, 0, sizeof(dsa)); 719 dsa.stencil[0].enabled = 1; 720 dsa.stencil[0].func = PIPE_FUNC_ALWAYS; 721 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff; 722 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE; 723 if (write_depth) { 724 /* writing depth+stencil: depth test always passes */ 725 dsa.depth.enabled = 1; 726 dsa.depth.writemask = ctx->Depth.Mask; 727 dsa.depth.func = PIPE_FUNC_ALWAYS; 728 } 729 cso_set_depth_stencil_alpha(cso, &dsa); 730 731 /* blend (colormask) */ 732 memset(&blend, 0, sizeof(blend)); 733 cso_set_blend(cso, &blend); 734 } 735 736 /* fragment shader state: TEX lookup program */ 737 cso_set_fragment_shader_handle(cso, driver_fp); 738 739 /* vertex shader state: position + texcoord pass-through */ 740 cso_set_vertex_shader_handle(cso, driver_vp); 741 742 /* geometry shader state: disabled */ 743 cso_set_geometry_shader_handle(cso, NULL); 744 745 /* texture sampling state: */ 746 { 747 struct pipe_sampler_state sampler; 748 memset(&sampler, 0, sizeof(sampler)); 749 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP; 750 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP; 751 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP; 752 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 753 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 754 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 755 sampler.normalized_coords = normalized; 756 757 cso_single_sampler(cso, PIPE_SHADER_FRAGMENT, 0, &sampler); 758 if (num_sampler_view > 1) { 759 cso_single_sampler(cso, PIPE_SHADER_FRAGMENT, 1, &sampler); 760 } 761 cso_single_sampler_done(cso, PIPE_SHADER_FRAGMENT); 762 } 763 764 /* viewport state: viewport matching window dims */ 765 { 766 const float w = (float) ctx->DrawBuffer->Width; 767 const float h = (float) ctx->DrawBuffer->Height; 768 struct pipe_viewport_state vp; 769 vp.scale[0] = 0.5f * w; 770 vp.scale[1] = -0.5f * h; 771 vp.scale[2] = 0.5f; 772 vp.scale[3] = 1.0f; 773 vp.translate[0] = 0.5f * w; 774 vp.translate[1] = 0.5f * h; 775 vp.translate[2] = 0.5f; 776 vp.translate[3] = 0.0f; 777 cso_set_viewport(cso, &vp); 778 } 779 780 cso_set_vertex_elements(cso, 3, st->velems_util_draw); 781 cso_set_stream_outputs(st->cso_context, 0, NULL, 0); 782 783 /* texture state: */ 784 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num_sampler_view, sv); 785 786 /* Compute Gallium window coords (y=0=top) with pixel zoom. 787 * Recall that these coords are transformed by the current 788 * vertex shader and viewport transformation. 789 */ 790 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) { 791 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY); 792 invertTex = !invertTex; 793 } 794 795 x0 = (GLfloat) x; 796 x1 = x + width * ctx->Pixel.ZoomX; 797 y0 = (GLfloat) y; 798 y1 = y + height * ctx->Pixel.ZoomY; 799 800 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */ 801 z = z * 2.0 - 1.0; 802 803 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex, 804 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width, 805 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height); 806 807 /* restore state */ 808 cso_restore_rasterizer(cso); 809 cso_restore_viewport(cso); 810 cso_restore_samplers(cso, PIPE_SHADER_FRAGMENT); 811 cso_restore_sampler_views(cso, PIPE_SHADER_FRAGMENT); 812 cso_restore_fragment_shader(cso); 813 cso_restore_vertex_shader(cso); 814 cso_restore_geometry_shader(cso); 815 cso_restore_vertex_elements(cso); 816 cso_restore_vertex_buffers(cso); 817 cso_restore_stream_outputs(cso); 818 if (write_stencil) { 819 cso_restore_depth_stencil_alpha(cso); 820 cso_restore_blend(cso); 821 } 822} 823 824 825/** 826 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we 827 * can't use a fragment shader to write stencil values. 828 */ 829static void 830draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y, 831 GLsizei width, GLsizei height, GLenum format, GLenum type, 832 const struct gl_pixelstore_attrib *unpack, 833 const GLvoid *pixels) 834{ 835 struct st_context *st = st_context(ctx); 836 struct pipe_context *pipe = st->pipe; 837 struct st_renderbuffer *strb; 838 enum pipe_transfer_usage usage; 839 struct pipe_transfer *pt; 840 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0; 841 ubyte *stmap; 842 struct gl_pixelstore_attrib clippedUnpack = *unpack; 843 GLubyte *sValues; 844 GLuint *zValues; 845 846 if (!zoom) { 847 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height, 848 &clippedUnpack)) { 849 /* totally clipped */ 850 return; 851 } 852 } 853 854 strb = st_renderbuffer(ctx->DrawBuffer-> 855 Attachment[BUFFER_STENCIL].Renderbuffer); 856 857 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) { 858 y = ctx->DrawBuffer->Height - y - height; 859 } 860 861 if (format == GL_STENCIL_INDEX && 862 _mesa_is_format_packed_depth_stencil(strb->Base.Format)) { 863 /* writing stencil to a combined depth+stencil buffer */ 864 usage = PIPE_TRANSFER_READ_WRITE; 865 } 866 else { 867 usage = PIPE_TRANSFER_WRITE; 868 } 869 870 pt = pipe_get_transfer(pipe, strb->texture, 871 strb->rtt_level, strb->rtt_face + strb->rtt_slice, 872 usage, x, y, 873 width, height); 874 875 stmap = pipe_transfer_map(pipe, pt); 876 877 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels); 878 assert(pixels); 879 880 sValues = (GLubyte *) malloc(width * sizeof(GLubyte)); 881 zValues = (GLuint *) malloc(width * sizeof(GLuint)); 882 883 if (sValues && zValues) { 884 GLint row; 885 for (row = 0; row < height; row++) { 886 GLfloat *zValuesFloat = (GLfloat*)zValues; 887 GLenum destType = GL_UNSIGNED_BYTE; 888 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels, 889 width, height, 890 format, type, 891 row, 0); 892 _mesa_unpack_stencil_span(ctx, width, destType, sValues, 893 type, source, &clippedUnpack, 894 ctx->_ImageTransferState); 895 896 if (format == GL_DEPTH_STENCIL) { 897 GLenum ztype = 898 pt->resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ? 899 GL_FLOAT : GL_UNSIGNED_INT; 900 901 _mesa_unpack_depth_span(ctx, width, ztype, zValues, 902 (1 << 24) - 1, type, source, 903 &clippedUnpack); 904 } 905 906 if (zoom) { 907 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with " 908 "zoom not complete"); 909 } 910 911 { 912 GLint spanY; 913 914 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) { 915 spanY = height - row - 1; 916 } 917 else { 918 spanY = row; 919 } 920 921 /* now pack the stencil (and Z) values in the dest format */ 922 switch (pt->resource->format) { 923 case PIPE_FORMAT_S8_UINT: 924 { 925 ubyte *dest = stmap + spanY * pt->stride; 926 assert(usage == PIPE_TRANSFER_WRITE); 927 memcpy(dest, sValues, width); 928 } 929 break; 930 case PIPE_FORMAT_Z24_UNORM_S8_UINT: 931 if (format == GL_DEPTH_STENCIL) { 932 uint *dest = (uint *) (stmap + spanY * pt->stride); 933 GLint k; 934 assert(usage == PIPE_TRANSFER_WRITE); 935 for (k = 0; k < width; k++) { 936 dest[k] = zValues[k] | (sValues[k] << 24); 937 } 938 } 939 else { 940 uint *dest = (uint *) (stmap + spanY * pt->stride); 941 GLint k; 942 assert(usage == PIPE_TRANSFER_READ_WRITE); 943 for (k = 0; k < width; k++) { 944 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24); 945 } 946 } 947 break; 948 case PIPE_FORMAT_S8_UINT_Z24_UNORM: 949 if (format == GL_DEPTH_STENCIL) { 950 uint *dest = (uint *) (stmap + spanY * pt->stride); 951 GLint k; 952 assert(usage == PIPE_TRANSFER_WRITE); 953 for (k = 0; k < width; k++) { 954 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff); 955 } 956 } 957 else { 958 uint *dest = (uint *) (stmap + spanY * pt->stride); 959 GLint k; 960 assert(usage == PIPE_TRANSFER_READ_WRITE); 961 for (k = 0; k < width; k++) { 962 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff); 963 } 964 } 965 break; 966 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT: 967 if (format == GL_DEPTH_STENCIL) { 968 uint *dest = (uint *) (stmap + spanY * pt->stride); 969 GLfloat *destf = (GLfloat*)dest; 970 GLint k; 971 assert(usage == PIPE_TRANSFER_WRITE); 972 for (k = 0; k < width; k++) { 973 destf[k*2] = zValuesFloat[k]; 974 dest[k*2+1] = sValues[k] & 0xff; 975 } 976 } 977 else { 978 uint *dest = (uint *) (stmap + spanY * pt->stride); 979 GLint k; 980 assert(usage == PIPE_TRANSFER_READ_WRITE); 981 for (k = 0; k < width; k++) { 982 dest[k*2+1] = sValues[k] & 0xff; 983 } 984 } 985 break; 986 default: 987 assert(0); 988 } 989 } 990 } 991 } 992 else { 993 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels()"); 994 } 995 996 free(sValues); 997 free(zValues); 998 999 _mesa_unmap_pbo_source(ctx, &clippedUnpack); 1000 1001 /* unmap the stencil buffer */ 1002 pipe_transfer_unmap(pipe, pt); 1003 pipe->transfer_destroy(pipe, pt); 1004} 1005 1006 1007/** 1008 * Get fragment program variant for a glDrawPixels or glCopyPixels 1009 * command for RGBA data. 1010 */ 1011static struct st_fp_variant * 1012get_color_fp_variant(struct st_context *st) 1013{ 1014 struct gl_context *ctx = st->ctx; 1015 struct st_fp_variant_key key; 1016 struct st_fp_variant *fpv; 1017 1018 memset(&key, 0, sizeof(key)); 1019 1020 key.st = st; 1021 key.drawpixels = 1; 1022 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 || 1023 ctx->Pixel.RedScale != 1.0 || 1024 ctx->Pixel.GreenBias != 0.0 || 1025 ctx->Pixel.GreenScale != 1.0 || 1026 ctx->Pixel.BlueBias != 0.0 || 1027 ctx->Pixel.BlueScale != 1.0 || 1028 ctx->Pixel.AlphaBias != 0.0 || 1029 ctx->Pixel.AlphaScale != 1.0); 1030 key.pixelMaps = ctx->Pixel.MapColorFlag; 1031 key.clamp_color = st->clamp_frag_color_in_shader && 1032 st->ctx->Color._ClampFragmentColor && 1033 !st->ctx->DrawBuffer->_IntegerColor; 1034 1035 fpv = st_get_fp_variant(st, st->fp, &key); 1036 1037 return fpv; 1038} 1039 1040 1041/** 1042 * Get fragment program variant for a glDrawPixels or glCopyPixels 1043 * command for depth/stencil data. 1044 */ 1045static struct st_fp_variant * 1046get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth, 1047 GLboolean write_stencil) 1048{ 1049 struct st_fp_variant_key key; 1050 struct st_fp_variant *fpv; 1051 1052 memset(&key, 0, sizeof(key)); 1053 1054 key.st = st; 1055 key.drawpixels = 1; 1056 key.drawpixels_z = write_depth; 1057 key.drawpixels_stencil = write_stencil; 1058 1059 fpv = st_get_fp_variant(st, st->fp, &key); 1060 1061 return fpv; 1062} 1063 1064 1065/** 1066 * Clamp glDrawPixels width and height to the maximum texture size. 1067 */ 1068static void 1069clamp_size(struct pipe_context *pipe, GLsizei *width, GLsizei *height, 1070 struct gl_pixelstore_attrib *unpack) 1071{ 1072 const unsigned maxSize = 1073 1 << (pipe->screen->get_param(pipe->screen, 1074 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1); 1075 1076 if (*width > maxSize) { 1077 if (unpack->RowLength == 0) 1078 unpack->RowLength = *width; 1079 *width = maxSize; 1080 } 1081 if (*height > maxSize) { 1082 *height = maxSize; 1083 } 1084} 1085 1086 1087/** 1088 * Called via ctx->Driver.DrawPixels() 1089 */ 1090static void 1091st_DrawPixels(struct gl_context *ctx, GLint x, GLint y, 1092 GLsizei width, GLsizei height, 1093 GLenum format, GLenum type, 1094 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels) 1095{ 1096 void *driver_vp, *driver_fp; 1097 struct st_context *st = st_context(ctx); 1098 const GLfloat *color; 1099 struct pipe_context *pipe = st->pipe; 1100 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE; 1101 struct pipe_sampler_view *sv[2]; 1102 int num_sampler_view = 1; 1103 struct st_fp_variant *fpv; 1104 struct gl_pixelstore_attrib clippedUnpack; 1105 1106 /* Mesa state should be up to date by now */ 1107 assert(ctx->NewState == 0x0); 1108 1109 st_validate_state(st); 1110 1111 /* Limit the size of the glDrawPixels to the max texture size. 1112 * Strictly speaking, that's not correct but since we don't handle 1113 * larger images yet, this is better than crashing. 1114 */ 1115 clippedUnpack = *unpack; 1116 unpack = &clippedUnpack; 1117 clamp_size(st->pipe, &width, &height, &clippedUnpack); 1118 1119 if (format == GL_DEPTH_STENCIL) 1120 write_stencil = write_depth = GL_TRUE; 1121 else if (format == GL_STENCIL_INDEX) 1122 write_stencil = GL_TRUE; 1123 else if (format == GL_DEPTH_COMPONENT) 1124 write_depth = GL_TRUE; 1125 1126 if (write_stencil && 1127 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) { 1128 /* software fallback */ 1129 draw_stencil_pixels(ctx, x, y, width, height, format, type, 1130 unpack, pixels); 1131 return; 1132 } 1133 1134 /* 1135 * Get vertex/fragment shaders 1136 */ 1137 if (write_depth || write_stencil) { 1138 fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil); 1139 1140 driver_fp = fpv->driver_shader; 1141 1142 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE); 1143 1144 color = ctx->Current.RasterColor; 1145 } 1146 else { 1147 fpv = get_color_fp_variant(st); 1148 1149 driver_fp = fpv->driver_shader; 1150 1151 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE); 1152 1153 color = NULL; 1154 if (st->pixel_xfer.pixelmap_enabled) { 1155 sv[1] = st->pixel_xfer.pixelmap_sampler_view; 1156 num_sampler_view++; 1157 } 1158 } 1159 1160 /* update fragment program constants */ 1161 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT); 1162 1163 /* draw with textured quad */ 1164 { 1165 struct pipe_resource *pt 1166 = make_texture(st, width, height, format, type, unpack, pixels); 1167 if (pt) { 1168 sv[0] = st_create_texture_sampler_view(st->pipe, pt); 1169 1170 if (sv[0]) { 1171 /* Create a second sampler view to read stencil. 1172 * The stencil is written using the shader stencil export 1173 * functionality. */ 1174 if (write_stencil) { 1175 enum pipe_format stencil_format = 1176 util_format_stencil_only(pt->format); 1177 1178 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt, 1179 stencil_format); 1180 num_sampler_view++; 1181 } 1182 1183 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2], 1184 width, height, 1185 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY, 1186 sv, 1187 num_sampler_view, 1188 driver_vp, 1189 driver_fp, 1190 color, GL_FALSE, write_depth, write_stencil); 1191 pipe_sampler_view_reference(&sv[0], NULL); 1192 if (num_sampler_view > 1) 1193 pipe_sampler_view_reference(&sv[1], NULL); 1194 } 1195 pipe_resource_reference(&pt, NULL); 1196 } 1197 } 1198} 1199 1200 1201 1202/** 1203 * Software fallback for glCopyPixels(GL_STENCIL). 1204 */ 1205static void 1206copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy, 1207 GLsizei width, GLsizei height, 1208 GLint dstx, GLint dsty) 1209{ 1210 struct st_renderbuffer *rbDraw; 1211 struct pipe_context *pipe = st_context(ctx)->pipe; 1212 enum pipe_transfer_usage usage; 1213 struct pipe_transfer *ptDraw; 1214 ubyte *drawMap; 1215 ubyte *buffer; 1216 int i; 1217 1218 buffer = malloc(width * height * sizeof(ubyte)); 1219 if (!buffer) { 1220 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)"); 1221 return; 1222 } 1223 1224 /* Get the dest renderbuffer */ 1225 rbDraw = st_renderbuffer(ctx->DrawBuffer-> 1226 Attachment[BUFFER_STENCIL].Renderbuffer); 1227 1228 /* this will do stencil pixel transfer ops */ 1229 _mesa_readpixels(ctx, srcx, srcy, width, height, 1230 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, 1231 &ctx->DefaultPacking, buffer); 1232 1233 if (0) { 1234 /* debug code: dump stencil values */ 1235 GLint row, col; 1236 for (row = 0; row < height; row++) { 1237 printf("%3d: ", row); 1238 for (col = 0; col < width; col++) { 1239 printf("%02x ", buffer[col + row * width]); 1240 } 1241 printf("\n"); 1242 } 1243 } 1244 1245 if (_mesa_is_format_packed_depth_stencil(rbDraw->Base.Format)) 1246 usage = PIPE_TRANSFER_READ_WRITE; 1247 else 1248 usage = PIPE_TRANSFER_WRITE; 1249 1250 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) { 1251 dsty = rbDraw->Base.Height - dsty - height; 1252 } 1253 1254 ptDraw = pipe_get_transfer(pipe, 1255 rbDraw->texture, 1256 rbDraw->rtt_level, 1257 rbDraw->rtt_face + rbDraw->rtt_slice, 1258 usage, dstx, dsty, 1259 width, height); 1260 1261 assert(util_format_get_blockwidth(ptDraw->resource->format) == 1); 1262 assert(util_format_get_blockheight(ptDraw->resource->format) == 1); 1263 1264 /* map the stencil buffer */ 1265 drawMap = pipe_transfer_map(pipe, ptDraw); 1266 1267 /* draw */ 1268 /* XXX PixelZoom not handled yet */ 1269 for (i = 0; i < height; i++) { 1270 ubyte *dst; 1271 const ubyte *src; 1272 int y; 1273 1274 y = i; 1275 1276 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) { 1277 y = height - y - 1; 1278 } 1279 1280 dst = drawMap + y * ptDraw->stride; 1281 src = buffer + i * width; 1282 1283 _mesa_pack_ubyte_stencil_row(rbDraw->Base.Format, width, src, dst); 1284 } 1285 1286 free(buffer); 1287 1288 /* unmap the stencil buffer */ 1289 pipe_transfer_unmap(pipe, ptDraw); 1290 pipe->transfer_destroy(pipe, ptDraw); 1291} 1292 1293 1294/** 1295 * Return renderbuffer to use for reading color pixels for glCopyPixels 1296 */ 1297static struct st_renderbuffer * 1298st_get_color_read_renderbuffer(struct gl_context *ctx) 1299{ 1300 struct gl_framebuffer *fb = ctx->ReadBuffer; 1301 struct st_renderbuffer *strb = 1302 st_renderbuffer(fb->_ColorReadBuffer); 1303 1304 return strb; 1305} 1306 1307 1308/** Do the src/dest regions overlap? */ 1309static GLboolean 1310regions_overlap(GLint srcX, GLint srcY, GLint dstX, GLint dstY, 1311 GLsizei width, GLsizei height) 1312{ 1313 if (srcX + width <= dstX || 1314 dstX + width <= srcX || 1315 srcY + height <= dstY || 1316 dstY + height <= srcY) 1317 return GL_FALSE; 1318 else 1319 return GL_TRUE; 1320} 1321 1322 1323/** 1324 * Try to do a glCopyPixels for simple cases with a blit by calling 1325 * pipe->resource_copy_region(). 1326 * 1327 * We can do this when we're copying color pixels (depth/stencil 1328 * eventually) with no pixel zoom, no pixel transfer ops, no 1329 * per-fragment ops, the src/dest regions don't overlap and the 1330 * src/dest pixel formats are the same. 1331 */ 1332static GLboolean 1333blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy, 1334 GLsizei width, GLsizei height, 1335 GLint dstx, GLint dsty, GLenum type) 1336{ 1337 struct st_context *st = st_context(ctx); 1338 struct pipe_context *pipe = st->pipe; 1339 struct gl_pixelstore_attrib pack, unpack; 1340 GLint readX, readY, readW, readH; 1341 1342 if (type == GL_COLOR && 1343 ctx->Pixel.ZoomX == 1.0 && 1344 ctx->Pixel.ZoomY == 1.0 && 1345 ctx->_ImageTransferState == 0x0 && 1346 !ctx->Color.BlendEnabled && 1347 !ctx->Color.AlphaEnabled && 1348 !ctx->Depth.Test && 1349 !ctx->Fog.Enabled && 1350 !ctx->Stencil.Enabled && 1351 !ctx->FragmentProgram.Enabled && 1352 !ctx->VertexProgram.Enabled && 1353 !ctx->Shader.CurrentFragmentProgram && 1354 st_fb_orientation(ctx->ReadBuffer) == st_fb_orientation(ctx->DrawBuffer) && 1355 ctx->DrawBuffer->_NumColorDrawBuffers == 1 && 1356 !ctx->Query.CondRenderQuery) { 1357 struct st_renderbuffer *rbRead, *rbDraw; 1358 GLint drawX, drawY; 1359 1360 /* 1361 * Clip the read region against the src buffer bounds. 1362 * We'll still allocate a temporary buffer/texture for the original 1363 * src region size but we'll only read the region which is on-screen. 1364 * This may mean that we draw garbage pixels into the dest region, but 1365 * that's expected. 1366 */ 1367 readX = srcx; 1368 readY = srcy; 1369 readW = width; 1370 readH = height; 1371 pack = ctx->DefaultPacking; 1372 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) 1373 return GL_TRUE; /* all done */ 1374 1375 /* clip against dest buffer bounds and scissor box */ 1376 drawX = dstx + pack.SkipPixels; 1377 drawY = dsty + pack.SkipRows; 1378 unpack = pack; 1379 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack)) 1380 return GL_TRUE; /* all done */ 1381 1382 readX = readX - pack.SkipPixels + unpack.SkipPixels; 1383 readY = readY - pack.SkipRows + unpack.SkipRows; 1384 1385 rbRead = st_get_color_read_renderbuffer(ctx); 1386 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]); 1387 1388 if ((rbRead != rbDraw || 1389 !regions_overlap(readX, readY, drawX, drawY, readW, readH)) && 1390 rbRead->Base.Format == rbDraw->Base.Format) { 1391 struct pipe_box srcBox; 1392 1393 /* flip src/dst position if needed */ 1394 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 1395 /* both buffers will have the same orientation */ 1396 readY = ctx->ReadBuffer->Height - readY - readH; 1397 drawY = ctx->DrawBuffer->Height - drawY - readH; 1398 } 1399 1400 u_box_2d(readX, readY, readW, readH, &srcBox); 1401 1402 pipe->resource_copy_region(pipe, 1403 rbDraw->texture, 1404 rbDraw->rtt_level, drawX, drawY, 0, 1405 rbRead->texture, 1406 rbRead->rtt_level, &srcBox); 1407 return GL_TRUE; 1408 } 1409 } 1410 1411 return GL_FALSE; 1412} 1413 1414 1415static void 1416st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy, 1417 GLsizei width, GLsizei height, 1418 GLint dstx, GLint dsty, GLenum type) 1419{ 1420 struct st_context *st = st_context(ctx); 1421 struct pipe_context *pipe = st->pipe; 1422 struct pipe_screen *screen = pipe->screen; 1423 struct st_renderbuffer *rbRead; 1424 void *driver_vp, *driver_fp; 1425 struct pipe_resource *pt; 1426 struct pipe_sampler_view *sv[2]; 1427 int num_sampler_view = 1; 1428 GLfloat *color; 1429 enum pipe_format srcFormat, texFormat; 1430 GLboolean invertTex = GL_FALSE; 1431 GLint readX, readY, readW, readH; 1432 GLuint sample_count; 1433 struct gl_pixelstore_attrib pack = ctx->DefaultPacking; 1434 struct st_fp_variant *fpv; 1435 1436 st_validate_state(st); 1437 1438 if (type == GL_DEPTH_STENCIL) { 1439 /* XXX make this more efficient */ 1440 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL); 1441 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH); 1442 return; 1443 } 1444 1445 if (type == GL_STENCIL) { 1446 /* can't use texturing to do stencil */ 1447 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty); 1448 return; 1449 } 1450 1451 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type)) 1452 return; 1453 1454 /* 1455 * The subsequent code implements glCopyPixels by copying the source 1456 * pixels into a temporary texture that's then applied to a textured quad. 1457 * When we draw the textured quad, all the usual per-fragment operations 1458 * are handled. 1459 */ 1460 1461 1462 /* 1463 * Get vertex/fragment shaders 1464 */ 1465 if (type == GL_COLOR) { 1466 rbRead = st_get_color_read_renderbuffer(ctx); 1467 color = NULL; 1468 1469 fpv = get_color_fp_variant(st); 1470 driver_fp = fpv->driver_shader; 1471 1472 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE); 1473 1474 if (st->pixel_xfer.pixelmap_enabled) { 1475 sv[1] = st->pixel_xfer.pixelmap_sampler_view; 1476 num_sampler_view++; 1477 } 1478 } 1479 else { 1480 assert(type == GL_DEPTH); 1481 rbRead = st_renderbuffer(ctx->ReadBuffer-> 1482 Attachment[BUFFER_DEPTH].Renderbuffer); 1483 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0]; 1484 1485 fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE); 1486 driver_fp = fpv->driver_shader; 1487 1488 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE); 1489 } 1490 1491 /* update fragment program constants */ 1492 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT); 1493 1494 sample_count = rbRead->texture->nr_samples; 1495 /* I believe this would be legal, presumably would need to do a resolve 1496 for color, and for depth/stencil spec says to just use one of the 1497 depth/stencil samples per pixel? Need some transfer clarifications. */ 1498 assert(sample_count < 2); 1499 1500 srcFormat = rbRead->texture->format; 1501 1502 if (screen->is_format_supported(screen, srcFormat, st->internal_target, 1503 sample_count, 1504 PIPE_BIND_SAMPLER_VIEW)) { 1505 texFormat = srcFormat; 1506 } 1507 else { 1508 /* srcFormat can't be used as a texture format */ 1509 if (type == GL_DEPTH) { 1510 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT, 1511 GL_NONE, GL_NONE, st->internal_target, 1512 sample_count, PIPE_BIND_DEPTH_STENCIL); 1513 assert(texFormat != PIPE_FORMAT_NONE); 1514 } 1515 else { 1516 /* default color format */ 1517 texFormat = st_choose_format(screen, GL_RGBA, 1518 GL_NONE, GL_NONE, st->internal_target, 1519 sample_count, PIPE_BIND_SAMPLER_VIEW); 1520 assert(texFormat != PIPE_FORMAT_NONE); 1521 } 1522 } 1523 1524 /* Invert src region if needed */ 1525 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 1526 srcy = ctx->ReadBuffer->Height - srcy - height; 1527 invertTex = !invertTex; 1528 } 1529 1530 /* Clip the read region against the src buffer bounds. 1531 * We'll still allocate a temporary buffer/texture for the original 1532 * src region size but we'll only read the region which is on-screen. 1533 * This may mean that we draw garbage pixels into the dest region, but 1534 * that's expected. 1535 */ 1536 readX = srcx; 1537 readY = srcy; 1538 readW = width; 1539 readH = height; 1540 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) { 1541 /* The source region is completely out of bounds. Do nothing. 1542 * The GL spec says "Results of copies from outside the window, 1543 * or from regions of the window that are not exposed, are 1544 * hardware dependent and undefined." 1545 */ 1546 return; 1547 } 1548 1549 readW = MAX2(0, readW); 1550 readH = MAX2(0, readH); 1551 1552 /* alloc temporary texture */ 1553 pt = alloc_texture(st, width, height, texFormat); 1554 if (!pt) 1555 return; 1556 1557 sv[0] = st_create_texture_sampler_view(st->pipe, pt); 1558 if (!sv[0]) { 1559 pipe_resource_reference(&pt, NULL); 1560 return; 1561 } 1562 1563 /* Make temporary texture which is a copy of the src region. 1564 */ 1565 if (srcFormat == texFormat) { 1566 struct pipe_box src_box; 1567 u_box_2d(readX, readY, readW, readH, &src_box); 1568 /* copy source framebuffer surface into mipmap/texture */ 1569 pipe->resource_copy_region(pipe, 1570 pt, /* dest tex */ 1571 0, /* dest lvl */ 1572 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */ 1573 rbRead->texture, /* src tex */ 1574 rbRead->rtt_level, /* src lvl */ 1575 &src_box); 1576 1577 } 1578 else { 1579 /* CPU-based fallback/conversion */ 1580 struct pipe_transfer *ptRead = 1581 pipe_get_transfer(st->pipe, rbRead->texture, 1582 rbRead->rtt_level, 1583 rbRead->rtt_face + rbRead->rtt_slice, 1584 PIPE_TRANSFER_READ, 1585 readX, readY, readW, readH); 1586 struct pipe_transfer *ptTex; 1587 enum pipe_transfer_usage transfer_usage; 1588 1589 if (ST_DEBUG & DEBUG_FALLBACK) 1590 debug_printf("%s: fallback processing\n", __FUNCTION__); 1591 1592 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format)) 1593 transfer_usage = PIPE_TRANSFER_READ_WRITE; 1594 else 1595 transfer_usage = PIPE_TRANSFER_WRITE; 1596 1597 ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage, 1598 0, 0, width, height); 1599 1600 /* copy image from ptRead surface to ptTex surface */ 1601 if (type == GL_COLOR) { 1602 /* alternate path using get/put_tile() */ 1603 GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat)); 1604 enum pipe_format readFormat, drawFormat; 1605 readFormat = util_format_linear(rbRead->texture->format); 1606 drawFormat = util_format_linear(pt->format); 1607 pipe_get_tile_rgba_format(pipe, ptRead, 0, 0, readW, readH, 1608 readFormat, buf); 1609 pipe_put_tile_rgba_format(pipe, ptTex, pack.SkipPixels, pack.SkipRows, 1610 readW, readH, drawFormat, buf); 1611 free(buf); 1612 } 1613 else { 1614 /* GL_DEPTH */ 1615 GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint)); 1616 pipe_get_tile_z(pipe, ptRead, 0, 0, readW, readH, buf); 1617 pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows, 1618 readW, readH, buf); 1619 free(buf); 1620 } 1621 1622 pipe->transfer_destroy(pipe, ptRead); 1623 pipe->transfer_destroy(pipe, ptTex); 1624 } 1625 1626 /* OK, the texture 'pt' contains the src image/pixels. Now draw a 1627 * textured quad with that texture. 1628 */ 1629 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2], 1630 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY, 1631 sv, 1632 num_sampler_view, 1633 driver_vp, 1634 driver_fp, 1635 color, invertTex, GL_FALSE, GL_FALSE); 1636 1637 pipe_resource_reference(&pt, NULL); 1638 pipe_sampler_view_reference(&sv[0], NULL); 1639} 1640 1641 1642 1643void st_init_drawpixels_functions(struct dd_function_table *functions) 1644{ 1645 functions->DrawPixels = st_DrawPixels; 1646 functions->CopyPixels = st_CopyPixels; 1647} 1648 1649 1650void 1651st_destroy_drawpix(struct st_context *st) 1652{ 1653 GLuint i; 1654 1655 for (i = 0; i < Elements(st->drawpix.shaders); i++) { 1656 if (st->drawpix.shaders[i]) 1657 _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL); 1658 } 1659 1660 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL); 1661 if (st->drawpix.vert_shaders[0]) 1662 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[0]); 1663 if (st->drawpix.vert_shaders[1]) 1664 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[1]); 1665} 1666 1667#endif /* FEATURE_drawpix */ 1668