1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
42 #include "main/readpix.h"
43 #include "main/texformat.h"
44 #include "main/teximage.h"
45 #include "main/texstore.h"
46 #include "program/program.h"
47 #include "program/prog_print.h"
48 #include "program/prog_instruction.h"
51 #include "st_atom_constbuf.h"
52 #include "st_cb_drawpixels.h"
53 #include "st_cb_readpixels.h"
54 #include "st_cb_fbo.h"
55 #include "st_context.h"
57 #include "st_format.h"
58 #include "st_program.h"
59 #include "st_texture.h"
61 #include "pipe/p_context.h"
62 #include "pipe/p_defines.h"
63 #include "tgsi/tgsi_ureg.h"
64 #include "util/u_draw_quad.h"
65 #include "util/u_format.h"
66 #include "util/u_inlines.h"
67 #include "util/u_math.h"
68 #include "util/u_tile.h"
69 #include "util/u_upload_mgr.h"
70 #include "cso_cache/cso_context.h"
76 * Check if the given program is:
77 * 0: MOVE result.color, fragment.color;
81 is_passthrough_program(const struct gl_fragment_program *prog)
83 if (prog->Base.NumInstructions == 2) {
84 const struct prog_instruction *inst = prog->Base.Instructions;
85 if (inst[0].Opcode == OPCODE_MOV &&
86 inst[1].Opcode == OPCODE_END &&
87 inst[0].DstReg.File == PROGRAM_OUTPUT &&
88 inst[0].DstReg.Index == FRAG_RESULT_COLOR &&
89 inst[0].DstReg.WriteMask == WRITEMASK_XYZW &&
90 inst[0].SrcReg[0].File == PROGRAM_INPUT &&
91 inst[0].SrcReg[0].Index == FRAG_ATTRIB_COL0 &&
92 inst[0].SrcReg[0].Swizzle == SWIZZLE_XYZW) {
101 * Returns a fragment program which implements the current pixel transfer ops.
103 static struct gl_fragment_program *
104 get_glsl_pixel_transfer_program(struct st_context *st,
105 struct st_fragment_program *orig)
107 int pixelMaps = 0, scaleAndBias = 0;
108 struct gl_context *ctx = st->ctx;
109 struct st_fragment_program *fp = (struct st_fragment_program *)
110 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
115 if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 ||
116 ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 ||
117 ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 ||
118 ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) {
122 pixelMaps = ctx->Pixel.MapColorFlag;
125 /* create the colormap/texture now if not already done */
126 if (!st->pixel_xfer.pixelmap_texture) {
127 st->pixel_xfer.pixelmap_texture = st_create_color_map_texture(ctx);
128 st->pixel_xfer.pixelmap_sampler_view =
129 st_create_texture_sampler_view(st->pipe,
130 st->pixel_xfer.pixelmap_texture);
134 get_pixel_transfer_visitor(fp, orig->glsl_to_tgsi,
135 scaleAndBias, pixelMaps);
142 * Make fragment shader for glDraw/CopyPixels. This shader is made
143 * by combining the pixel transfer shader with the user-defined shader.
144 * \param fpIn the current/incoming fragment program
145 * \param fpOut returns the combined fragment program
148 st_make_drawpix_fragment_program(struct st_context *st,
149 struct gl_fragment_program *fpIn,
150 struct gl_fragment_program **fpOut)
152 struct gl_program *newProg;
153 struct st_fragment_program *stfp = (struct st_fragment_program *) fpIn;
155 if (is_passthrough_program(fpIn)) {
156 newProg = (struct gl_program *) _mesa_clone_fragment_program(st->ctx,
157 &st->pixel_xfer.program->Base);
159 else if (stfp->glsl_to_tgsi != NULL) {
160 newProg = (struct gl_program *) get_glsl_pixel_transfer_program(st, stfp);
165 printf("Base program:\n");
166 _mesa_print_program(&fpIn->Base);
167 printf("DrawPix program:\n");
168 _mesa_print_program(&st->pixel_xfer.program->Base.Base);
170 newProg = _mesa_combine_programs(st->ctx,
171 &st->pixel_xfer.program->Base.Base,
177 printf("Combined DrawPixels program:\n");
178 _mesa_print_program(newProg);
179 printf("InputsRead: 0x%x\n", newProg->InputsRead);
180 printf("OutputsWritten: 0x%x\n", newProg->OutputsWritten);
181 _mesa_print_parameter_list(newProg->Parameters);
184 *fpOut = (struct gl_fragment_program *) newProg;
189 * Create fragment program that does a TEX() instruction to get a Z and/or
190 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
191 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
192 * Pass fragment color through as-is.
193 * \return pointer to the gl_fragment program
195 struct gl_fragment_program *
196 st_make_drawpix_z_stencil_program(struct st_context *st,
197 GLboolean write_depth,
198 GLboolean write_stencil)
200 struct gl_context *ctx = st->ctx;
201 struct gl_program *p;
202 struct gl_fragment_program *fp;
204 const GLuint shaderIndex = write_depth * 2 + write_stencil;
206 assert(shaderIndex < Elements(st->drawpix.shaders));
208 if (st->drawpix.shaders[shaderIndex]) {
209 /* already have the proper shader */
210 return st->drawpix.shaders[shaderIndex];
216 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
220 p->NumInstructions = write_depth ? 3 : 1;
221 p->NumInstructions += write_stencil ? 1 : 0;
223 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
224 if (!p->Instructions) {
225 ctx->Driver.DeleteProgram(ctx, p);
228 _mesa_init_instructions(p->Instructions, p->NumInstructions);
231 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
232 p->Instructions[ic].Opcode = OPCODE_TEX;
233 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
234 p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH;
235 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z;
236 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
237 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
238 p->Instructions[ic].TexSrcUnit = 0;
239 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
241 /* MOV result.color, fragment.color; */
242 p->Instructions[ic].Opcode = OPCODE_MOV;
243 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
244 p->Instructions[ic].DstReg.Index = FRAG_RESULT_COLOR;
245 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
246 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_COL0;
251 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
252 p->Instructions[ic].Opcode = OPCODE_TEX;
253 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
254 p->Instructions[ic].DstReg.Index = FRAG_RESULT_STENCIL;
255 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Y;
256 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
257 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
258 p->Instructions[ic].TexSrcUnit = 1;
259 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
264 p->Instructions[ic++].Opcode = OPCODE_END;
266 assert(ic == p->NumInstructions);
268 p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0;
269 p->OutputsWritten = 0;
271 p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_DEPTH);
272 p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_COLOR);
275 p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_STENCIL);
277 p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
279 p->SamplersUsed |= 1 << 1;
281 fp = (struct gl_fragment_program *) p;
283 /* save the new shader */
284 st->drawpix.shaders[shaderIndex] = fp;
291 * Create a simple vertex shader that just passes through the
292 * vertex position and texcoord (and optionally, color).
295 make_passthrough_vertex_shader(struct st_context *st,
298 if (!st->drawpix.vert_shaders[passColor]) {
299 struct ureg_program *ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
304 /* MOV result.pos, vertex.pos; */
306 ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ),
307 ureg_DECL_vs_input( ureg, 0 ));
309 /* MOV result.texcoord0, vertex.attr[1]; */
311 ureg_DECL_output( ureg, TGSI_SEMANTIC_GENERIC, 0 ),
312 ureg_DECL_vs_input( ureg, 1 ));
315 /* MOV result.color0, vertex.attr[2]; */
317 ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ),
318 ureg_DECL_vs_input( ureg, 2 ));
323 st->drawpix.vert_shaders[passColor] =
324 ureg_create_shader_and_destroy( ureg, st->pipe );
327 return st->drawpix.vert_shaders[passColor];
332 * Return a texture internalFormat for drawing/copying an image
333 * of the given format and type.
336 internal_format(struct gl_context *ctx, GLenum format, GLenum type)
339 case GL_DEPTH_COMPONENT:
341 case GL_UNSIGNED_SHORT:
342 return GL_DEPTH_COMPONENT16;
344 case GL_UNSIGNED_INT:
345 return GL_DEPTH_COMPONENT32;
348 if (ctx->Extensions.ARB_depth_buffer_float)
349 return GL_DEPTH_COMPONENT32F;
351 return GL_DEPTH_COMPONENT;
354 return GL_DEPTH_COMPONENT;
357 case GL_DEPTH_STENCIL:
359 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
360 return GL_DEPTH32F_STENCIL8;
362 case GL_UNSIGNED_INT_24_8:
364 return GL_DEPTH24_STENCIL8;
367 case GL_STENCIL_INDEX:
368 return GL_STENCIL_INDEX;
371 if (_mesa_is_integer_format(format)) {
375 case GL_UNSIGNED_BYTE:
379 case GL_UNSIGNED_SHORT:
383 case GL_UNSIGNED_INT:
386 assert(0 && "Unexpected type in internal_format()");
387 return GL_RGBA_INTEGER;
392 case GL_UNSIGNED_BYTE:
393 case GL_UNSIGNED_INT_8_8_8_8:
394 case GL_UNSIGNED_INT_8_8_8_8_REV:
398 case GL_UNSIGNED_BYTE_3_3_2:
399 case GL_UNSIGNED_BYTE_2_3_3_REV:
400 case GL_UNSIGNED_SHORT_4_4_4_4:
401 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
404 case GL_UNSIGNED_SHORT_5_6_5:
405 case GL_UNSIGNED_SHORT_5_6_5_REV:
406 case GL_UNSIGNED_SHORT_5_5_5_1:
407 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
410 case GL_UNSIGNED_INT_10_10_10_2:
411 case GL_UNSIGNED_INT_2_10_10_10_REV:
414 case GL_UNSIGNED_SHORT:
415 case GL_UNSIGNED_INT:
420 ctx->Extensions.EXT_texture_snorm ? GL_RGBA8_SNORM : GL_RGBA8;
425 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
427 case GL_HALF_FLOAT_ARB:
429 ctx->Extensions.ARB_texture_float ? GL_RGBA16F :
430 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
435 ctx->Extensions.ARB_texture_float ? GL_RGBA32F :
436 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
438 case GL_UNSIGNED_INT_5_9_9_9_REV:
439 assert(ctx->Extensions.EXT_texture_shared_exponent);
442 case GL_UNSIGNED_INT_10F_11F_11F_REV:
443 assert(ctx->Extensions.EXT_packed_float);
444 return GL_R11F_G11F_B10F;
452 * Create a temporary texture to hold an image of the given size.
453 * If width, height are not POT and the driver only handles POT textures,
454 * allocate the next larger size of texture that is POT.
456 static struct pipe_resource *
457 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
458 enum pipe_format texFormat)
460 struct pipe_resource *pt;
462 pt = st_texture_create(st, st->internal_target, texFormat, 0,
463 width, height, 1, 1, PIPE_BIND_SAMPLER_VIEW);
470 * Make texture containing an image for glDrawPixels image.
471 * If 'pixels' is NULL, leave the texture image data undefined.
473 static struct pipe_resource *
474 make_texture(struct st_context *st,
475 GLsizei width, GLsizei height, GLenum format, GLenum type,
476 const struct gl_pixelstore_attrib *unpack,
477 const GLvoid *pixels)
479 struct gl_context *ctx = st->ctx;
480 struct pipe_context *pipe = st->pipe;
482 struct pipe_resource *pt;
483 enum pipe_format pipeFormat;
484 GLenum baseInternalFormat, intFormat;
486 intFormat = internal_format(ctx, format, type);
487 baseInternalFormat = _mesa_base_tex_format(ctx, intFormat);
489 mformat = st_ChooseTextureFormat_renderable(ctx, intFormat,
490 format, type, GL_FALSE);
493 pipeFormat = st_mesa_format_to_pipe_format(mformat);
496 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
500 /* alloc temporary texture */
501 pt = alloc_texture(st, width, height, pipeFormat);
503 _mesa_unmap_pbo_source(ctx, unpack);
508 struct pipe_transfer *transfer;
511 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
513 /* we'll do pixel transfer in a fragment shader */
514 ctx->_ImageTransferState = 0x0;
516 transfer = pipe_get_transfer(st->pipe, pt, 0, 0,
517 PIPE_TRANSFER_WRITE, 0, 0,
520 /* map texture transfer */
521 dest = pipe_transfer_map(pipe, transfer);
524 /* Put image into texture transfer.
525 * Note that the image is actually going to be upside down in
526 * the texture. We deal with that with texcoords.
528 success = _mesa_texstore(ctx, 2, /* dims */
529 baseInternalFormat, /* baseInternalFormat */
530 mformat, /* gl_format */
531 transfer->stride, /* dstRowStride, bytes */
532 &dest, /* destSlices */
533 width, height, 1, /* size */
534 format, type, /* src format/type */
535 pixels, /* data source */
539 pipe_transfer_unmap(pipe, transfer);
540 pipe->transfer_destroy(pipe, transfer);
545 ctx->_ImageTransferState = imageTransferStateSave;
548 _mesa_unmap_pbo_source(ctx, unpack);
555 * Draw quad with texcoords and optional color.
556 * Coords are gallium window coords with y=0=top.
557 * \param color may be null
558 * \param invertTex if true, flip texcoords vertically
561 draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z,
562 GLfloat x1, GLfloat y1, const GLfloat *color,
563 GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
565 struct st_context *st = st_context(ctx);
566 struct pipe_context *pipe = st->pipe;
567 GLfloat (*verts)[3][4]; /* four verts, three attribs, XYZW */
568 struct pipe_resource *buf = NULL;
571 u_upload_alloc(st->uploader, 0, 4 * sizeof(verts[0]), &offset, &buf,
577 /* setup vertex data */
579 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
580 const GLfloat fb_width = (GLfloat) fb->Width;
581 const GLfloat fb_height = (GLfloat) fb->Height;
582 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
583 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
584 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
585 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
586 const GLfloat sLeft = 0.0f, sRight = maxXcoord;
587 const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
588 const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
592 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
593 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
596 verts[1][0][0] = clip_x1;
597 verts[1][0][1] = clip_y0;
600 verts[2][0][0] = clip_x1;
601 verts[2][0][1] = clip_y1;
604 verts[3][0][0] = clip_x0;
605 verts[3][0][1] = clip_y1;
607 verts[0][1][0] = sLeft; /* v[0].attr[1].S */
608 verts[0][1][1] = tTop; /* v[0].attr[1].T */
609 verts[1][1][0] = sRight;
610 verts[1][1][1] = tTop;
611 verts[2][1][0] = sRight;
612 verts[2][1][1] = tBot;
613 verts[3][1][0] = sLeft;
614 verts[3][1][1] = tBot;
616 /* same for all verts: */
618 for (i = 0; i < 4; i++) {
619 verts[i][0][2] = z; /* v[i].attr[0].z */
620 verts[i][0][3] = 1.0f; /* v[i].attr[0].w */
621 verts[i][2][0] = color[0]; /* v[i].attr[2].r */
622 verts[i][2][1] = color[1]; /* v[i].attr[2].g */
623 verts[i][2][2] = color[2]; /* v[i].attr[2].b */
624 verts[i][2][3] = color[3]; /* v[i].attr[2].a */
625 verts[i][1][2] = 0.0f; /* v[i].attr[1].R */
626 verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */
630 for (i = 0; i < 4; i++) {
631 verts[i][0][2] = z; /*Z*/
632 verts[i][0][3] = 1.0f; /*W*/
633 verts[i][1][2] = 0.0f; /*R*/
634 verts[i][1][3] = 1.0f; /*Q*/
639 u_upload_unmap(st->uploader);
640 util_draw_vertex_buffer(pipe, st->cso_context, buf, offset,
643 3); /* attribs/vert */
644 pipe_resource_reference(&buf, NULL);
650 draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
651 GLsizei width, GLsizei height,
652 GLfloat zoomX, GLfloat zoomY,
653 struct pipe_sampler_view **sv,
654 int num_sampler_view,
657 const GLfloat *color,
659 GLboolean write_depth, GLboolean write_stencil)
661 struct st_context *st = st_context(ctx);
662 struct pipe_context *pipe = st->pipe;
663 struct cso_context *cso = st->cso_context;
664 GLfloat x0, y0, x1, y1;
666 boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT;
669 /* XXX if DrawPixels image is larger than max texture size, break
672 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
673 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
674 assert(width <= maxSize);
675 assert(height <= maxSize);
677 cso_save_rasterizer(cso);
678 cso_save_viewport(cso);
679 cso_save_samplers(cso);
680 cso_save_fragment_sampler_views(cso);
681 cso_save_fragment_shader(cso);
682 cso_save_stream_outputs(cso);
683 cso_save_vertex_shader(cso);
684 cso_save_geometry_shader(cso);
685 cso_save_vertex_elements(cso);
686 cso_save_vertex_buffers(cso);
688 cso_save_depth_stencil_alpha(cso);
692 /* rasterizer state: just scissor */
694 struct pipe_rasterizer_state rasterizer;
695 memset(&rasterizer, 0, sizeof(rasterizer));
696 rasterizer.clamp_fragment_color = !st->clamp_frag_color_in_shader &&
697 ctx->Color._ClampFragmentColor;
698 rasterizer.gl_rasterization_rules = 1;
699 rasterizer.depth_clip = !ctx->Transform.DepthClamp;
700 rasterizer.scissor = ctx->Scissor.Enabled;
701 cso_set_rasterizer(cso, &rasterizer);
705 /* Stencil writing bypasses the normal fragment pipeline to
706 * disable color writing and set stencil test to always pass.
708 struct pipe_depth_stencil_alpha_state dsa;
709 struct pipe_blend_state blend;
712 memset(&dsa, 0, sizeof(dsa));
713 dsa.stencil[0].enabled = 1;
714 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
715 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
716 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
718 /* writing depth+stencil: depth test always passes */
719 dsa.depth.enabled = 1;
720 dsa.depth.writemask = ctx->Depth.Mask;
721 dsa.depth.func = PIPE_FUNC_ALWAYS;
723 cso_set_depth_stencil_alpha(cso, &dsa);
725 /* blend (colormask) */
726 memset(&blend, 0, sizeof(blend));
727 cso_set_blend(cso, &blend);
730 /* fragment shader state: TEX lookup program */
731 cso_set_fragment_shader_handle(cso, driver_fp);
733 /* vertex shader state: position + texcoord pass-through */
734 cso_set_vertex_shader_handle(cso, driver_vp);
736 /* geometry shader state: disabled */
737 cso_set_geometry_shader_handle(cso, NULL);
739 /* texture sampling state: */
741 struct pipe_sampler_state sampler;
742 memset(&sampler, 0, sizeof(sampler));
743 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
744 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
745 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
746 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
747 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
748 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
749 sampler.normalized_coords = normalized;
751 cso_single_sampler(cso, 0, &sampler);
752 if (num_sampler_view > 1) {
753 cso_single_sampler(cso, 1, &sampler);
755 cso_single_sampler_done(cso);
758 /* viewport state: viewport matching window dims */
760 const float w = (float) ctx->DrawBuffer->Width;
761 const float h = (float) ctx->DrawBuffer->Height;
762 struct pipe_viewport_state vp;
763 vp.scale[0] = 0.5f * w;
764 vp.scale[1] = -0.5f * h;
767 vp.translate[0] = 0.5f * w;
768 vp.translate[1] = 0.5f * h;
769 vp.translate[2] = 0.5f;
770 vp.translate[3] = 0.0f;
771 cso_set_viewport(cso, &vp);
774 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
775 cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
778 cso_set_fragment_sampler_views(cso, num_sampler_view, sv);
780 /* Compute Gallium window coords (y=0=top) with pixel zoom.
781 * Recall that these coords are transformed by the current
782 * vertex shader and viewport transformation.
784 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
785 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
786 invertTex = !invertTex;
790 x1 = x + width * ctx->Pixel.ZoomX;
792 y1 = y + height * ctx->Pixel.ZoomY;
794 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
797 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
798 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
799 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
802 cso_restore_rasterizer(cso);
803 cso_restore_viewport(cso);
804 cso_restore_samplers(cso);
805 cso_restore_fragment_sampler_views(cso);
806 cso_restore_fragment_shader(cso);
807 cso_restore_vertex_shader(cso);
808 cso_restore_geometry_shader(cso);
809 cso_restore_vertex_elements(cso);
810 cso_restore_vertex_buffers(cso);
811 cso_restore_stream_outputs(cso);
813 cso_restore_depth_stencil_alpha(cso);
814 cso_restore_blend(cso);
820 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
821 * can't use a fragment shader to write stencil values.
824 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
825 GLsizei width, GLsizei height, GLenum format, GLenum type,
826 const struct gl_pixelstore_attrib *unpack,
827 const GLvoid *pixels)
829 struct st_context *st = st_context(ctx);
830 struct pipe_context *pipe = st->pipe;
831 struct st_renderbuffer *strb;
832 enum pipe_transfer_usage usage;
833 struct pipe_transfer *pt;
834 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
836 struct gl_pixelstore_attrib clippedUnpack = *unpack;
841 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
843 /* totally clipped */
848 strb = st_renderbuffer(ctx->DrawBuffer->
849 Attachment[BUFFER_STENCIL].Renderbuffer);
851 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
852 y = ctx->DrawBuffer->Height - y - height;
855 if (format == GL_STENCIL_INDEX &&
856 _mesa_is_format_packed_depth_stencil(strb->Base.Format)) {
857 /* writing stencil to a combined depth+stencil buffer */
858 usage = PIPE_TRANSFER_READ_WRITE;
861 usage = PIPE_TRANSFER_WRITE;
864 pt = pipe_get_transfer(pipe, strb->texture,
865 strb->rtt_level, strb->rtt_face + strb->rtt_slice,
869 stmap = pipe_transfer_map(pipe, pt);
871 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
874 sValues = (GLubyte *) malloc(width * sizeof(GLubyte));
875 zValues = (GLuint *) malloc(width * sizeof(GLuint));
877 if (sValues && zValues) {
879 for (row = 0; row < height; row++) {
880 GLfloat *zValuesFloat = (GLfloat*)zValues;
881 GLenum destType = GL_UNSIGNED_BYTE;
882 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
886 _mesa_unpack_stencil_span(ctx, width, destType, sValues,
887 type, source, &clippedUnpack,
888 ctx->_ImageTransferState);
890 if (format == GL_DEPTH_STENCIL) {
892 pt->resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ?
893 GL_FLOAT : GL_UNSIGNED_INT;
895 _mesa_unpack_depth_span(ctx, width, ztype, zValues,
896 (1 << 24) - 1, type, source,
901 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
902 "zoom not complete");
908 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
909 spanY = height - row - 1;
915 /* now pack the stencil (and Z) values in the dest format */
916 switch (pt->resource->format) {
917 case PIPE_FORMAT_S8_UINT:
919 ubyte *dest = stmap + spanY * pt->stride;
920 assert(usage == PIPE_TRANSFER_WRITE);
921 memcpy(dest, sValues, width);
924 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
925 if (format == GL_DEPTH_STENCIL) {
926 uint *dest = (uint *) (stmap + spanY * pt->stride);
928 assert(usage == PIPE_TRANSFER_WRITE);
929 for (k = 0; k < width; k++) {
930 dest[k] = zValues[k] | (sValues[k] << 24);
934 uint *dest = (uint *) (stmap + spanY * pt->stride);
936 assert(usage == PIPE_TRANSFER_READ_WRITE);
937 for (k = 0; k < width; k++) {
938 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
942 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
943 if (format == GL_DEPTH_STENCIL) {
944 uint *dest = (uint *) (stmap + spanY * pt->stride);
946 assert(usage == PIPE_TRANSFER_WRITE);
947 for (k = 0; k < width; k++) {
948 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
952 uint *dest = (uint *) (stmap + spanY * pt->stride);
954 assert(usage == PIPE_TRANSFER_READ_WRITE);
955 for (k = 0; k < width; k++) {
956 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
960 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
961 if (format == GL_DEPTH_STENCIL) {
962 uint *dest = (uint *) (stmap + spanY * pt->stride);
963 GLfloat *destf = (GLfloat*)dest;
965 assert(usage == PIPE_TRANSFER_WRITE);
966 for (k = 0; k < width; k++) {
967 destf[k*2] = zValuesFloat[k];
968 dest[k*2+1] = sValues[k] & 0xff;
972 uint *dest = (uint *) (stmap + spanY * pt->stride);
974 assert(usage == PIPE_TRANSFER_READ_WRITE);
975 for (k = 0; k < width; k++) {
976 dest[k*2+1] = sValues[k] & 0xff;
987 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels()");
993 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
995 /* unmap the stencil buffer */
996 pipe_transfer_unmap(pipe, pt);
997 pipe->transfer_destroy(pipe, pt);
1002 * Get fragment program variant for a glDrawPixels or glCopyPixels
1003 * command for RGBA data.
1005 static struct st_fp_variant *
1006 get_color_fp_variant(struct st_context *st)
1008 struct gl_context *ctx = st->ctx;
1009 struct st_fp_variant_key key;
1010 struct st_fp_variant *fpv;
1012 memset(&key, 0, sizeof(key));
1016 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
1017 ctx->Pixel.RedScale != 1.0 ||
1018 ctx->Pixel.GreenBias != 0.0 ||
1019 ctx->Pixel.GreenScale != 1.0 ||
1020 ctx->Pixel.BlueBias != 0.0 ||
1021 ctx->Pixel.BlueScale != 1.0 ||
1022 ctx->Pixel.AlphaBias != 0.0 ||
1023 ctx->Pixel.AlphaScale != 1.0);
1024 key.pixelMaps = ctx->Pixel.MapColorFlag;
1025 key.clamp_color = st->clamp_frag_color_in_shader &&
1026 st->ctx->Color._ClampFragmentColor;
1028 fpv = st_get_fp_variant(st, st->fp, &key);
1035 * Get fragment program variant for a glDrawPixels or glCopyPixels
1036 * command for depth/stencil data.
1038 static struct st_fp_variant *
1039 get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth,
1040 GLboolean write_stencil)
1042 struct st_fp_variant_key key;
1043 struct st_fp_variant *fpv;
1045 memset(&key, 0, sizeof(key));
1049 key.drawpixels_z = write_depth;
1050 key.drawpixels_stencil = write_stencil;
1052 fpv = st_get_fp_variant(st, st->fp, &key);
1059 * Clamp glDrawPixels width and height to the maximum texture size.
1062 clamp_size(struct pipe_context *pipe, GLsizei *width, GLsizei *height,
1063 struct gl_pixelstore_attrib *unpack)
1065 const unsigned maxSize =
1066 1 << (pipe->screen->get_param(pipe->screen,
1067 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
1069 if (*width > maxSize) {
1070 if (unpack->RowLength == 0)
1071 unpack->RowLength = *width;
1074 if (*height > maxSize) {
1081 * Called via ctx->Driver.DrawPixels()
1084 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
1085 GLsizei width, GLsizei height,
1086 GLenum format, GLenum type,
1087 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
1089 void *driver_vp, *driver_fp;
1090 struct st_context *st = st_context(ctx);
1091 const GLfloat *color;
1092 struct pipe_context *pipe = st->pipe;
1093 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
1094 struct pipe_sampler_view *sv[2];
1095 int num_sampler_view = 1;
1096 struct st_fp_variant *fpv;
1097 struct gl_pixelstore_attrib clippedUnpack;
1099 /* Mesa state should be up to date by now */
1100 assert(ctx->NewState == 0x0);
1102 st_validate_state(st);
1104 /* Limit the size of the glDrawPixels to the max texture size.
1105 * Strictly speaking, that's not correct but since we don't handle
1106 * larger images yet, this is better than crashing.
1108 clippedUnpack = *unpack;
1109 unpack = &clippedUnpack;
1110 clamp_size(st->pipe, &width, &height, &clippedUnpack);
1112 if (format == GL_DEPTH_STENCIL)
1113 write_stencil = write_depth = GL_TRUE;
1114 else if (format == GL_STENCIL_INDEX)
1115 write_stencil = GL_TRUE;
1116 else if (format == GL_DEPTH_COMPONENT)
1117 write_depth = GL_TRUE;
1119 if (write_stencil &&
1120 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) {
1121 /* software fallback */
1122 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1128 * Get vertex/fragment shaders
1130 if (write_depth || write_stencil) {
1131 fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil);
1133 driver_fp = fpv->driver_shader;
1135 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1137 color = ctx->Current.RasterColor;
1140 fpv = get_color_fp_variant(st);
1142 driver_fp = fpv->driver_shader;
1144 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1147 if (st->pixel_xfer.pixelmap_enabled) {
1148 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1153 /* update fragment program constants */
1154 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1156 /* draw with textured quad */
1158 struct pipe_resource *pt
1159 = make_texture(st, width, height, format, type, unpack, pixels);
1161 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1164 /* Create a second sampler view to read stencil.
1165 * The stencil is written using the shader stencil export
1167 if (write_stencil) {
1168 enum pipe_format stencil_format =
1169 util_format_stencil_only(pt->format);
1171 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1176 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1178 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1183 color, GL_FALSE, write_depth, write_stencil);
1184 pipe_sampler_view_reference(&sv[0], NULL);
1185 if (num_sampler_view > 1)
1186 pipe_sampler_view_reference(&sv[1], NULL);
1188 pipe_resource_reference(&pt, NULL);
1196 * Software fallback for glCopyPixels(GL_STENCIL).
1199 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1200 GLsizei width, GLsizei height,
1201 GLint dstx, GLint dsty)
1203 struct st_renderbuffer *rbDraw;
1204 struct pipe_context *pipe = st_context(ctx)->pipe;
1205 enum pipe_transfer_usage usage;
1206 struct pipe_transfer *ptDraw;
1211 buffer = malloc(width * height * sizeof(ubyte));
1213 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1217 /* Get the dest renderbuffer */
1218 rbDraw = st_renderbuffer(ctx->DrawBuffer->
1219 Attachment[BUFFER_STENCIL].Renderbuffer);
1221 /* this will do stencil pixel transfer ops */
1222 _mesa_readpixels(ctx, srcx, srcy, width, height,
1223 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1224 &ctx->DefaultPacking, buffer);
1227 /* debug code: dump stencil values */
1229 for (row = 0; row < height; row++) {
1230 printf("%3d: ", row);
1231 for (col = 0; col < width; col++) {
1232 printf("%02x ", buffer[col + row * width]);
1238 if (_mesa_is_format_packed_depth_stencil(rbDraw->Base.Format))
1239 usage = PIPE_TRANSFER_READ_WRITE;
1241 usage = PIPE_TRANSFER_WRITE;
1243 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1244 dsty = rbDraw->Base.Height - dsty - height;
1247 ptDraw = pipe_get_transfer(pipe,
1250 rbDraw->rtt_face + rbDraw->rtt_slice,
1254 assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
1255 assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
1257 /* map the stencil buffer */
1258 drawMap = pipe_transfer_map(pipe, ptDraw);
1261 /* XXX PixelZoom not handled yet */
1262 for (i = 0; i < height; i++) {
1269 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1273 dst = drawMap + y * ptDraw->stride;
1274 src = buffer + i * width;
1276 _mesa_pack_ubyte_stencil_row(rbDraw->Base.Format, width, src, dst);
1281 /* unmap the stencil buffer */
1282 pipe_transfer_unmap(pipe, ptDraw);
1283 pipe->transfer_destroy(pipe, ptDraw);
1288 * Return renderbuffer to use for reading color pixels for glCopyPixels
1290 static struct st_renderbuffer *
1291 st_get_color_read_renderbuffer(struct gl_context *ctx)
1293 struct gl_framebuffer *fb = ctx->ReadBuffer;
1294 struct st_renderbuffer *strb =
1295 st_renderbuffer(fb->_ColorReadBuffer);
1301 /** Do the src/dest regions overlap? */
1303 regions_overlap(GLint srcX, GLint srcY, GLint dstX, GLint dstY,
1304 GLsizei width, GLsizei height)
1306 if (srcX + width <= dstX ||
1307 dstX + width <= srcX ||
1308 srcY + height <= dstY ||
1309 dstY + height <= srcY)
1317 * Try to do a glCopyPixels for simple cases with a blit by calling
1318 * pipe->resource_copy_region().
1320 * We can do this when we're copying color pixels (depth/stencil
1321 * eventually) with no pixel zoom, no pixel transfer ops, no
1322 * per-fragment ops, the src/dest regions don't overlap and the
1323 * src/dest pixel formats are the same.
1326 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1327 GLsizei width, GLsizei height,
1328 GLint dstx, GLint dsty, GLenum type)
1330 struct st_context *st = st_context(ctx);
1331 struct pipe_context *pipe = st->pipe;
1332 struct gl_pixelstore_attrib pack, unpack;
1333 GLint readX, readY, readW, readH;
1335 if (type == GL_COLOR &&
1336 ctx->Pixel.ZoomX == 1.0 &&
1337 ctx->Pixel.ZoomY == 1.0 &&
1338 ctx->_ImageTransferState == 0x0 &&
1339 !ctx->Color.BlendEnabled &&
1340 !ctx->Color.AlphaEnabled &&
1342 !ctx->Fog.Enabled &&
1343 !ctx->Stencil.Enabled &&
1344 !ctx->FragmentProgram.Enabled &&
1345 !ctx->VertexProgram.Enabled &&
1346 !ctx->Shader.CurrentFragmentProgram &&
1347 st_fb_orientation(ctx->ReadBuffer) == st_fb_orientation(ctx->DrawBuffer) &&
1348 ctx->DrawBuffer->_NumColorDrawBuffers == 1 &&
1349 !ctx->Query.CondRenderQuery) {
1350 struct st_renderbuffer *rbRead, *rbDraw;
1354 * Clip the read region against the src buffer bounds.
1355 * We'll still allocate a temporary buffer/texture for the original
1356 * src region size but we'll only read the region which is on-screen.
1357 * This may mean that we draw garbage pixels into the dest region, but
1364 pack = ctx->DefaultPacking;
1365 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1366 return GL_TRUE; /* all done */
1368 /* clip against dest buffer bounds and scissor box */
1369 drawX = dstx + pack.SkipPixels;
1370 drawY = dsty + pack.SkipRows;
1372 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1373 return GL_TRUE; /* all done */
1375 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1376 readY = readY - pack.SkipRows + unpack.SkipRows;
1378 rbRead = st_get_color_read_renderbuffer(ctx);
1379 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1381 if ((rbRead != rbDraw ||
1382 !regions_overlap(readX, readY, drawX, drawY, readW, readH)) &&
1383 rbRead->Base.Format == rbDraw->Base.Format) {
1384 struct pipe_box srcBox;
1386 /* flip src/dst position if needed */
1387 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1388 /* both buffers will have the same orientation */
1389 readY = ctx->ReadBuffer->Height - readY - readH;
1390 drawY = ctx->DrawBuffer->Height - drawY - readH;
1393 u_box_2d(readX, readY, readW, readH, &srcBox);
1395 pipe->resource_copy_region(pipe,
1397 rbDraw->rtt_level, drawX, drawY, 0,
1399 rbRead->rtt_level, &srcBox);
1409 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1410 GLsizei width, GLsizei height,
1411 GLint dstx, GLint dsty, GLenum type)
1413 struct st_context *st = st_context(ctx);
1414 struct pipe_context *pipe = st->pipe;
1415 struct pipe_screen *screen = pipe->screen;
1416 struct st_renderbuffer *rbRead;
1417 void *driver_vp, *driver_fp;
1418 struct pipe_resource *pt;
1419 struct pipe_sampler_view *sv[2];
1420 int num_sampler_view = 1;
1422 enum pipe_format srcFormat, texFormat;
1423 GLboolean invertTex = GL_FALSE;
1424 GLint readX, readY, readW, readH;
1425 GLuint sample_count;
1426 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1427 struct st_fp_variant *fpv;
1429 st_validate_state(st);
1431 if (type == GL_DEPTH_STENCIL) {
1432 /* XXX make this more efficient */
1433 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL);
1434 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH);
1438 if (type == GL_STENCIL) {
1439 /* can't use texturing to do stencil */
1440 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1444 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1448 * The subsequent code implements glCopyPixels by copying the source
1449 * pixels into a temporary texture that's then applied to a textured quad.
1450 * When we draw the textured quad, all the usual per-fragment operations
1456 * Get vertex/fragment shaders
1458 if (type == GL_COLOR) {
1459 rbRead = st_get_color_read_renderbuffer(ctx);
1462 fpv = get_color_fp_variant(st);
1463 driver_fp = fpv->driver_shader;
1465 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1467 if (st->pixel_xfer.pixelmap_enabled) {
1468 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1473 assert(type == GL_DEPTH);
1474 rbRead = st_renderbuffer(ctx->ReadBuffer->
1475 Attachment[BUFFER_DEPTH].Renderbuffer);
1476 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1478 fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE);
1479 driver_fp = fpv->driver_shader;
1481 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1484 /* update fragment program constants */
1485 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1487 sample_count = rbRead->texture->nr_samples;
1488 /* I believe this would be legal, presumably would need to do a resolve
1489 for color, and for depth/stencil spec says to just use one of the
1490 depth/stencil samples per pixel? Need some transfer clarifications. */
1491 assert(sample_count < 2);
1493 srcFormat = rbRead->texture->format;
1495 if (screen->is_format_supported(screen, srcFormat, st->internal_target,
1497 PIPE_BIND_SAMPLER_VIEW)) {
1498 texFormat = srcFormat;
1501 /* srcFormat can't be used as a texture format */
1502 if (type == GL_DEPTH) {
1503 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
1504 GL_NONE, GL_NONE, st->internal_target,
1505 sample_count, PIPE_BIND_DEPTH_STENCIL);
1506 assert(texFormat != PIPE_FORMAT_NONE);
1509 /* default color format */
1510 texFormat = st_choose_format(screen, GL_RGBA,
1511 GL_NONE, GL_NONE, st->internal_target,
1512 sample_count, PIPE_BIND_SAMPLER_VIEW);
1513 assert(texFormat != PIPE_FORMAT_NONE);
1517 /* Invert src region if needed */
1518 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1519 srcy = ctx->ReadBuffer->Height - srcy - height;
1520 invertTex = !invertTex;
1523 /* Clip the read region against the src buffer bounds.
1524 * We'll still allocate a temporary buffer/texture for the original
1525 * src region size but we'll only read the region which is on-screen.
1526 * This may mean that we draw garbage pixels into the dest region, but
1533 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) {
1534 /* The source region is completely out of bounds. Do nothing.
1535 * The GL spec says "Results of copies from outside the window,
1536 * or from regions of the window that are not exposed, are
1537 * hardware dependent and undefined."
1542 readW = MAX2(0, readW);
1543 readH = MAX2(0, readH);
1545 /* alloc temporary texture */
1546 pt = alloc_texture(st, width, height, texFormat);
1550 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1552 pipe_resource_reference(&pt, NULL);
1556 /* Make temporary texture which is a copy of the src region.
1558 if (srcFormat == texFormat) {
1559 struct pipe_box src_box;
1560 u_box_2d(readX, readY, readW, readH, &src_box);
1561 /* copy source framebuffer surface into mipmap/texture */
1562 pipe->resource_copy_region(pipe,
1565 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
1566 rbRead->texture, /* src tex */
1567 rbRead->rtt_level, /* src lvl */
1572 /* CPU-based fallback/conversion */
1573 struct pipe_transfer *ptRead =
1574 pipe_get_transfer(st->pipe, rbRead->texture,
1576 rbRead->rtt_face + rbRead->rtt_slice,
1578 readX, readY, readW, readH);
1579 struct pipe_transfer *ptTex;
1580 enum pipe_transfer_usage transfer_usage;
1582 if (ST_DEBUG & DEBUG_FALLBACK)
1583 debug_printf("%s: fallback processing\n", __FUNCTION__);
1585 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
1586 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1588 transfer_usage = PIPE_TRANSFER_WRITE;
1590 ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage,
1591 0, 0, width, height);
1593 /* copy image from ptRead surface to ptTex surface */
1594 if (type == GL_COLOR) {
1595 /* alternate path using get/put_tile() */
1596 GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1597 enum pipe_format readFormat, drawFormat;
1598 readFormat = util_format_linear(rbRead->texture->format);
1599 drawFormat = util_format_linear(pt->format);
1600 pipe_get_tile_rgba_format(pipe, ptRead, 0, 0, readW, readH,
1602 pipe_put_tile_rgba_format(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1603 readW, readH, drawFormat, buf);
1608 GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint));
1609 pipe_get_tile_z(pipe, ptRead, 0, 0, readW, readH, buf);
1610 pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1615 pipe->transfer_destroy(pipe, ptRead);
1616 pipe->transfer_destroy(pipe, ptTex);
1619 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1620 * textured quad with that texture.
1622 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1623 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1628 color, invertTex, GL_FALSE, GL_FALSE);
1630 pipe_resource_reference(&pt, NULL);
1631 pipe_sampler_view_reference(&sv[0], NULL);
1636 void st_init_drawpixels_functions(struct dd_function_table *functions)
1638 functions->DrawPixels = st_DrawPixels;
1639 functions->CopyPixels = st_CopyPixels;
1644 st_destroy_drawpix(struct st_context *st)
1648 for (i = 0; i < Elements(st->drawpix.shaders); i++) {
1649 if (st->drawpix.shaders[i])
1650 _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL);
1653 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1654 if (st->drawpix.vert_shaders[0])
1655 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[0]);
1656 if (st->drawpix.vert_shaders[1])
1657 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[1]);
1660 #endif /* FEATURE_drawpix */