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/macros.h"
37 #include "main/mfeatures.h"
38 #include "main/mtypes.h"
39 #include "main/pack.h"
41 #include "main/texformat.h"
42 #include "main/teximage.h"
43 #include "main/texstore.h"
44 #include "program/program.h"
45 #include "program/prog_print.h"
46 #include "program/prog_instruction.h"
49 #include "st_atom_constbuf.h"
50 #include "st_cb_drawpixels.h"
51 #include "st_cb_readpixels.h"
52 #include "st_cb_fbo.h"
53 #include "st_context.h"
55 #include "st_format.h"
56 #include "st_program.h"
57 #include "st_texture.h"
59 #include "pipe/p_context.h"
60 #include "pipe/p_defines.h"
61 #include "tgsi/tgsi_ureg.h"
62 #include "util/u_draw_quad.h"
63 #include "util/u_format.h"
64 #include "util/u_inlines.h"
65 #include "util/u_math.h"
66 #include "util/u_tile.h"
67 #include "cso_cache/cso_context.h"
73 * Check if the given program is:
74 * 0: MOVE result.color, fragment.color;
78 is_passthrough_program(const struct gl_fragment_program *prog)
80 if (prog->Base.NumInstructions == 2) {
81 const struct prog_instruction *inst = prog->Base.Instructions;
82 if (inst[0].Opcode == OPCODE_MOV &&
83 inst[1].Opcode == OPCODE_END &&
84 inst[0].DstReg.File == PROGRAM_OUTPUT &&
85 inst[0].DstReg.Index == FRAG_RESULT_COLOR &&
86 inst[0].DstReg.WriteMask == WRITEMASK_XYZW &&
87 inst[0].SrcReg[0].File == PROGRAM_INPUT &&
88 inst[0].SrcReg[0].Index == FRAG_ATTRIB_COL0 &&
89 inst[0].SrcReg[0].Swizzle == SWIZZLE_XYZW) {
98 * Returns a fragment program which implements the current pixel transfer ops.
100 static struct gl_fragment_program *
101 get_glsl_pixel_transfer_program(struct st_context *st,
102 struct st_fragment_program *orig)
104 int pixelMaps = 0, scaleAndBias = 0;
105 struct gl_context *ctx = st->ctx;
106 struct st_fragment_program *fp = (struct st_fragment_program *)
107 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
112 if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 ||
113 ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 ||
114 ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 ||
115 ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) {
119 pixelMaps = ctx->Pixel.MapColorFlag;
122 /* create the colormap/texture now if not already done */
123 if (!st->pixel_xfer.pixelmap_texture) {
124 st->pixel_xfer.pixelmap_texture = st_create_color_map_texture(ctx);
125 st->pixel_xfer.pixelmap_sampler_view =
126 st_create_texture_sampler_view(st->pipe,
127 st->pixel_xfer.pixelmap_texture);
131 get_pixel_transfer_visitor(fp, orig->glsl_to_tgsi,
132 scaleAndBias, pixelMaps);
139 * Make fragment shader for glDraw/CopyPixels. This shader is made
140 * by combining the pixel transfer shader with the user-defined shader.
141 * \param fpIn the current/incoming fragment program
142 * \param fpOut returns the combined fragment program
145 st_make_drawpix_fragment_program(struct st_context *st,
146 struct gl_fragment_program *fpIn,
147 struct gl_fragment_program **fpOut)
149 struct gl_program *newProg;
150 struct st_fragment_program *stfp = (struct st_fragment_program *) fpIn;
152 if (is_passthrough_program(fpIn)) {
153 newProg = (struct gl_program *) _mesa_clone_fragment_program(st->ctx,
154 &st->pixel_xfer.program->Base);
156 else if (stfp->glsl_to_tgsi != NULL) {
157 newProg = (struct gl_program *) get_glsl_pixel_transfer_program(st, stfp);
162 printf("Base program:\n");
163 _mesa_print_program(&fpIn->Base);
164 printf("DrawPix program:\n");
165 _mesa_print_program(&st->pixel_xfer.program->Base.Base);
167 newProg = _mesa_combine_programs(st->ctx,
168 &st->pixel_xfer.program->Base.Base,
174 printf("Combined DrawPixels program:\n");
175 _mesa_print_program(newProg);
176 printf("InputsRead: 0x%x\n", newProg->InputsRead);
177 printf("OutputsWritten: 0x%x\n", newProg->OutputsWritten);
178 _mesa_print_parameter_list(newProg->Parameters);
181 *fpOut = (struct gl_fragment_program *) newProg;
186 * Create fragment program that does a TEX() instruction to get a Z and/or
187 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
188 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
189 * Pass fragment color through as-is.
190 * \return pointer to the gl_fragment program
192 struct gl_fragment_program *
193 st_make_drawpix_z_stencil_program(struct st_context *st,
194 GLboolean write_depth,
195 GLboolean write_stencil)
197 struct gl_context *ctx = st->ctx;
198 struct gl_program *p;
199 struct gl_fragment_program *fp;
201 const GLuint shaderIndex = write_depth * 2 + write_stencil;
203 assert(shaderIndex < Elements(st->drawpix.shaders));
205 if (st->drawpix.shaders[shaderIndex]) {
206 /* already have the proper shader */
207 return st->drawpix.shaders[shaderIndex];
213 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
217 p->NumInstructions = write_depth ? 2 : 1;
218 p->NumInstructions += write_stencil ? 1 : 0;
220 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
221 if (!p->Instructions) {
222 ctx->Driver.DeleteProgram(ctx, p);
225 _mesa_init_instructions(p->Instructions, p->NumInstructions);
228 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
229 p->Instructions[ic].Opcode = OPCODE_TEX;
230 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
231 p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH;
232 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z;
233 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
234 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
235 p->Instructions[ic].TexSrcUnit = 0;
236 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
241 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
242 p->Instructions[ic].Opcode = OPCODE_TEX;
243 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
244 p->Instructions[ic].DstReg.Index = FRAG_RESULT_STENCIL;
245 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Y;
246 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
247 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
248 p->Instructions[ic].TexSrcUnit = 1;
249 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
254 p->Instructions[ic++].Opcode = OPCODE_END;
256 assert(ic == p->NumInstructions);
258 p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0;
259 p->OutputsWritten = 0;
261 p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_DEPTH);
263 p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_STENCIL);
265 p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
267 p->SamplersUsed |= 1 << 1;
269 fp = (struct gl_fragment_program *) p;
271 /* save the new shader */
272 st->drawpix.shaders[shaderIndex] = fp;
279 * Create a simple vertex shader that just passes through the
280 * vertex position and texcoord (and optionally, color).
283 make_passthrough_vertex_shader(struct st_context *st,
286 if (!st->drawpix.vert_shaders[passColor]) {
287 struct ureg_program *ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
292 /* MOV result.pos, vertex.pos; */
294 ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ),
295 ureg_DECL_vs_input( ureg, 0 ));
297 /* MOV result.texcoord0, vertex.attr[1]; */
299 ureg_DECL_output( ureg, TGSI_SEMANTIC_GENERIC, 0 ),
300 ureg_DECL_vs_input( ureg, 1 ));
303 /* MOV result.color0, vertex.attr[2]; */
305 ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ),
306 ureg_DECL_vs_input( ureg, 2 ));
311 st->drawpix.vert_shaders[passColor] =
312 ureg_create_shader_and_destroy( ureg, st->pipe );
315 return st->drawpix.vert_shaders[passColor];
320 * Return a texture internalFormat for drawing/copying an image
321 * of the given format and type.
324 internal_format(struct gl_context *ctx, GLenum format, GLenum type)
327 case GL_DEPTH_COMPONENT:
329 case GL_UNSIGNED_SHORT:
330 return GL_DEPTH_COMPONENT16;
332 case GL_UNSIGNED_INT:
333 return GL_DEPTH_COMPONENT32;
336 if (ctx->Extensions.ARB_depth_buffer_float)
337 return GL_DEPTH_COMPONENT32F;
339 return GL_DEPTH_COMPONENT;
342 return GL_DEPTH_COMPONENT;
345 case GL_DEPTH_STENCIL:
347 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
348 return GL_DEPTH32F_STENCIL8;
350 case GL_UNSIGNED_INT_24_8:
352 return GL_DEPTH24_STENCIL8;
355 case GL_STENCIL_INDEX:
356 return GL_STENCIL_INDEX;
359 if (_mesa_is_integer_format(format)) {
363 case GL_UNSIGNED_BYTE:
367 case GL_UNSIGNED_SHORT:
371 case GL_UNSIGNED_INT:
374 assert(0 && "Unexpected type in internal_format()");
375 return GL_RGBA_INTEGER;
380 case GL_UNSIGNED_BYTE:
381 case GL_UNSIGNED_INT_8_8_8_8:
382 case GL_UNSIGNED_INT_8_8_8_8_REV:
386 case GL_UNSIGNED_BYTE_3_3_2:
387 case GL_UNSIGNED_BYTE_2_3_3_REV:
388 case GL_UNSIGNED_SHORT_4_4_4_4:
389 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
392 case GL_UNSIGNED_SHORT_5_6_5:
393 case GL_UNSIGNED_SHORT_5_6_5_REV:
394 case GL_UNSIGNED_SHORT_5_5_5_1:
395 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
398 case GL_UNSIGNED_INT_10_10_10_2:
399 case GL_UNSIGNED_INT_2_10_10_10_REV:
402 case GL_UNSIGNED_SHORT:
403 case GL_UNSIGNED_INT:
408 ctx->Extensions.EXT_texture_snorm ? GL_RGBA8_SNORM : GL_RGBA8;
413 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
415 case GL_HALF_FLOAT_ARB:
417 ctx->Extensions.ARB_texture_float ? GL_RGBA16F :
418 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
423 ctx->Extensions.ARB_texture_float ? GL_RGBA32F :
424 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
426 case GL_UNSIGNED_INT_5_9_9_9_REV:
427 assert(ctx->Extensions.EXT_texture_shared_exponent);
430 case GL_UNSIGNED_INT_10F_11F_11F_REV:
431 assert(ctx->Extensions.EXT_packed_float);
432 return GL_R11F_G11F_B10F;
440 * Create a temporary texture to hold an image of the given size.
441 * If width, height are not POT and the driver only handles POT textures,
442 * allocate the next larger size of texture that is POT.
444 static struct pipe_resource *
445 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
446 enum pipe_format texFormat)
448 struct pipe_resource *pt;
450 pt = st_texture_create(st, st->internal_target, texFormat, 0,
451 width, height, 1, 1, PIPE_BIND_SAMPLER_VIEW);
458 * Make texture containing an image for glDrawPixels image.
459 * If 'pixels' is NULL, leave the texture image data undefined.
461 static struct pipe_resource *
462 make_texture(struct st_context *st,
463 GLsizei width, GLsizei height, GLenum format, GLenum type,
464 const struct gl_pixelstore_attrib *unpack,
465 const GLvoid *pixels)
467 struct gl_context *ctx = st->ctx;
468 struct pipe_context *pipe = st->pipe;
470 struct pipe_resource *pt;
471 enum pipe_format pipeFormat;
472 GLenum baseInternalFormat, intFormat;
474 intFormat = internal_format(ctx, format, type);
475 baseInternalFormat = _mesa_base_tex_format(ctx, intFormat);
477 mformat = st_ChooseTextureFormat_renderable(ctx, intFormat,
478 format, type, GL_FALSE);
481 pipeFormat = st_mesa_format_to_pipe_format(mformat);
484 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
488 /* alloc temporary texture */
489 pt = alloc_texture(st, width, height, pipeFormat);
491 _mesa_unmap_pbo_source(ctx, unpack);
496 struct pipe_transfer *transfer;
497 static const GLuint dstImageOffsets = 0;
500 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
502 /* we'll do pixel transfer in a fragment shader */
503 ctx->_ImageTransferState = 0x0;
505 transfer = pipe_get_transfer(st->pipe, pt, 0, 0,
506 PIPE_TRANSFER_WRITE, 0, 0,
509 /* map texture transfer */
510 dest = pipe_transfer_map(pipe, transfer);
513 /* Put image into texture transfer.
514 * Note that the image is actually going to be upside down in
515 * the texture. We deal with that with texcoords.
517 success = _mesa_texstore(ctx, 2, /* dims */
518 baseInternalFormat, /* baseInternalFormat */
519 mformat, /* gl_format */
521 0, 0, 0, /* dstX/Y/Zoffset */
522 transfer->stride, /* dstRowStride, bytes */
523 &dstImageOffsets, /* dstImageOffsets */
524 width, height, 1, /* size */
525 format, type, /* src format/type */
526 pixels, /* data source */
530 pipe_transfer_unmap(pipe, transfer);
531 pipe->transfer_destroy(pipe, transfer);
536 ctx->_ImageTransferState = imageTransferStateSave;
539 _mesa_unmap_pbo_source(ctx, unpack);
546 * Draw quad with texcoords and optional color.
547 * Coords are gallium window coords with y=0=top.
548 * \param color may be null
549 * \param invertTex if true, flip texcoords vertically
552 draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z,
553 GLfloat x1, GLfloat y1, const GLfloat *color,
554 GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
556 struct st_context *st = st_context(ctx);
557 struct pipe_context *pipe = st->pipe;
558 GLfloat verts[4][3][4]; /* four verts, three attribs, XYZW */
560 /* setup vertex data */
562 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
563 const GLfloat fb_width = (GLfloat) fb->Width;
564 const GLfloat fb_height = (GLfloat) fb->Height;
565 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
566 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
567 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
568 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
569 const GLfloat sLeft = 0.0f, sRight = maxXcoord;
570 const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
571 const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
575 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
576 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
579 verts[1][0][0] = clip_x1;
580 verts[1][0][1] = clip_y0;
583 verts[2][0][0] = clip_x1;
584 verts[2][0][1] = clip_y1;
587 verts[3][0][0] = clip_x0;
588 verts[3][0][1] = clip_y1;
590 verts[0][1][0] = sLeft; /* v[0].attr[1].S */
591 verts[0][1][1] = tTop; /* v[0].attr[1].T */
592 verts[1][1][0] = sRight;
593 verts[1][1][1] = tTop;
594 verts[2][1][0] = sRight;
595 verts[2][1][1] = tBot;
596 verts[3][1][0] = sLeft;
597 verts[3][1][1] = tBot;
599 /* same for all verts: */
601 for (i = 0; i < 4; i++) {
602 verts[i][0][2] = z; /* v[i].attr[0].z */
603 verts[i][0][3] = 1.0f; /* v[i].attr[0].w */
604 verts[i][2][0] = color[0]; /* v[i].attr[2].r */
605 verts[i][2][1] = color[1]; /* v[i].attr[2].g */
606 verts[i][2][2] = color[2]; /* v[i].attr[2].b */
607 verts[i][2][3] = color[3]; /* v[i].attr[2].a */
608 verts[i][1][2] = 0.0f; /* v[i].attr[1].R */
609 verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */
613 for (i = 0; i < 4; i++) {
614 verts[i][0][2] = z; /*Z*/
615 verts[i][0][3] = 1.0f; /*W*/
616 verts[i][1][2] = 0.0f; /*R*/
617 verts[i][1][3] = 1.0f; /*Q*/
623 struct pipe_resource *buf;
625 /* allocate/load buffer object with vertex data */
626 buf = pipe_buffer_create(pipe->screen,
627 PIPE_BIND_VERTEX_BUFFER,
630 pipe_buffer_write(st->pipe, buf, 0, sizeof(verts), verts);
632 util_draw_vertex_buffer(pipe, st->cso_context, buf, 0,
635 3); /* attribs/vert */
636 pipe_resource_reference(&buf, NULL);
643 draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
644 GLsizei width, GLsizei height,
645 GLfloat zoomX, GLfloat zoomY,
646 struct pipe_sampler_view **sv,
647 int num_sampler_view,
650 const GLfloat *color,
652 GLboolean write_depth, GLboolean write_stencil)
654 struct st_context *st = st_context(ctx);
655 struct pipe_context *pipe = st->pipe;
656 struct cso_context *cso = st->cso_context;
657 GLfloat x0, y0, x1, y1;
659 boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT;
662 /* XXX if DrawPixels image is larger than max texture size, break
665 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
666 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
667 assert(width <= maxSize);
668 assert(height <= maxSize);
670 cso_save_rasterizer(cso);
671 cso_save_viewport(cso);
672 cso_save_samplers(cso);
673 cso_save_fragment_sampler_views(cso);
674 cso_save_fragment_shader(cso);
675 cso_save_vertex_shader(cso);
676 cso_save_vertex_elements(cso);
677 cso_save_vertex_buffers(cso);
679 cso_save_depth_stencil_alpha(cso);
683 /* rasterizer state: just scissor */
685 struct pipe_rasterizer_state rasterizer;
686 memset(&rasterizer, 0, sizeof(rasterizer));
687 rasterizer.clamp_fragment_color = ctx->Color._ClampFragmentColor;
688 rasterizer.gl_rasterization_rules = 1;
689 rasterizer.scissor = ctx->Scissor.Enabled;
690 cso_set_rasterizer(cso, &rasterizer);
694 /* Stencil writing bypasses the normal fragment pipeline to
695 * disable color writing and set stencil test to always pass.
697 struct pipe_depth_stencil_alpha_state dsa;
698 struct pipe_blend_state blend;
701 memset(&dsa, 0, sizeof(dsa));
702 dsa.stencil[0].enabled = 1;
703 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
704 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
705 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
707 /* writing depth+stencil: depth test always passes */
708 dsa.depth.enabled = 1;
709 dsa.depth.writemask = ctx->Depth.Mask;
710 dsa.depth.func = PIPE_FUNC_ALWAYS;
712 cso_set_depth_stencil_alpha(cso, &dsa);
714 /* blend (colormask) */
715 memset(&blend, 0, sizeof(blend));
716 cso_set_blend(cso, &blend);
719 /* fragment shader state: TEX lookup program */
720 cso_set_fragment_shader_handle(cso, driver_fp);
722 /* vertex shader state: position + texcoord pass-through */
723 cso_set_vertex_shader_handle(cso, driver_vp);
726 /* texture sampling state: */
728 struct pipe_sampler_state sampler;
729 memset(&sampler, 0, sizeof(sampler));
730 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
731 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
732 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
733 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
734 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
735 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
736 sampler.normalized_coords = normalized;
738 cso_single_sampler(cso, 0, &sampler);
739 if (num_sampler_view > 1) {
740 cso_single_sampler(cso, 1, &sampler);
742 cso_single_sampler_done(cso);
745 /* viewport state: viewport matching window dims */
747 const float w = (float) ctx->DrawBuffer->Width;
748 const float h = (float) ctx->DrawBuffer->Height;
749 struct pipe_viewport_state vp;
750 vp.scale[0] = 0.5f * w;
751 vp.scale[1] = -0.5f * h;
754 vp.translate[0] = 0.5f * w;
755 vp.translate[1] = 0.5f * h;
756 vp.translate[2] = 0.5f;
757 vp.translate[3] = 0.0f;
758 cso_set_viewport(cso, &vp);
761 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
764 cso_set_fragment_sampler_views(cso, num_sampler_view, sv);
766 /* Compute Gallium window coords (y=0=top) with pixel zoom.
767 * Recall that these coords are transformed by the current
768 * vertex shader and viewport transformation.
770 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
771 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
772 invertTex = !invertTex;
776 x1 = x + width * ctx->Pixel.ZoomX;
778 y1 = y + height * ctx->Pixel.ZoomY;
780 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
783 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
784 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
785 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
788 cso_restore_rasterizer(cso);
789 cso_restore_viewport(cso);
790 cso_restore_samplers(cso);
791 cso_restore_fragment_sampler_views(cso);
792 cso_restore_fragment_shader(cso);
793 cso_restore_vertex_shader(cso);
794 cso_restore_vertex_elements(cso);
795 cso_restore_vertex_buffers(cso);
797 cso_restore_depth_stencil_alpha(cso);
798 cso_restore_blend(cso);
804 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
805 * can't use a fragment shader to write stencil values.
808 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
809 GLsizei width, GLsizei height, GLenum format, GLenum type,
810 const struct gl_pixelstore_attrib *unpack,
811 const GLvoid *pixels)
813 struct st_context *st = st_context(ctx);
814 struct pipe_context *pipe = st->pipe;
815 struct st_renderbuffer *strb;
816 enum pipe_transfer_usage usage;
817 struct pipe_transfer *pt;
818 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
821 struct gl_pixelstore_attrib clippedUnpack = *unpack;
824 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
826 /* totally clipped */
831 strb = st_renderbuffer(ctx->DrawBuffer->
832 Attachment[BUFFER_STENCIL].Renderbuffer);
834 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
835 y = ctx->DrawBuffer->Height - y - height;
838 if(format != GL_DEPTH_STENCIL &&
839 util_format_get_component_bits(strb->format,
840 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
841 usage = PIPE_TRANSFER_READ_WRITE;
843 usage = PIPE_TRANSFER_WRITE;
845 pt = pipe_get_transfer(pipe, strb->texture,
846 strb->rtt_level, strb->rtt_face + strb->rtt_slice,
850 stmap = pipe_transfer_map(pipe, pt);
852 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
855 /* if width > MAX_WIDTH, have to process image in chunks */
857 while (skipPixels < width) {
858 const GLint spanX = skipPixels;
859 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
861 for (row = 0; row < height; row++) {
862 GLubyte sValues[MAX_WIDTH];
863 GLuint zValues[MAX_WIDTH];
864 GLfloat *zValuesFloat = (GLfloat*)zValues;
865 GLenum destType = GL_UNSIGNED_BYTE;
866 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
870 _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
871 type, source, &clippedUnpack,
872 ctx->_ImageTransferState);
874 if (format == GL_DEPTH_STENCIL) {
876 pt->resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED ?
877 GL_FLOAT : GL_UNSIGNED_INT;
879 _mesa_unpack_depth_span(ctx, spanWidth, ztype, zValues,
880 (1 << 24) - 1, type, source,
885 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
886 "zoom not complete");
892 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
893 spanY = height - row - 1;
899 /* now pack the stencil (and Z) values in the dest format */
900 switch (pt->resource->format) {
901 case PIPE_FORMAT_S8_USCALED:
903 ubyte *dest = stmap + spanY * pt->stride + spanX;
904 assert(usage == PIPE_TRANSFER_WRITE);
905 memcpy(dest, sValues, spanWidth);
908 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
909 if (format == GL_DEPTH_STENCIL) {
910 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
912 assert(usage == PIPE_TRANSFER_WRITE);
913 for (k = 0; k < spanWidth; k++) {
914 dest[k] = zValues[k] | (sValues[k] << 24);
918 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
920 assert(usage == PIPE_TRANSFER_READ_WRITE);
921 for (k = 0; k < spanWidth; k++) {
922 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
926 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
927 if (format == GL_DEPTH_STENCIL) {
928 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
930 assert(usage == PIPE_TRANSFER_WRITE);
931 for (k = 0; k < spanWidth; k++) {
932 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
936 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
938 assert(usage == PIPE_TRANSFER_READ_WRITE);
939 for (k = 0; k < spanWidth; k++) {
940 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
944 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED:
945 if (format == GL_DEPTH_STENCIL) {
946 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
947 GLfloat *destf = (GLfloat*)dest;
949 assert(usage == PIPE_TRANSFER_WRITE);
950 for (k = 0; k < spanWidth; k++) {
951 destf[k*2] = zValuesFloat[k];
952 dest[k*2+1] = sValues[k] & 0xff;
956 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
958 assert(usage == PIPE_TRANSFER_READ_WRITE);
959 for (k = 0; k < spanWidth; k++) {
960 dest[k*2+1] = sValues[k] & 0xff;
969 skipPixels += spanWidth;
972 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
974 /* unmap the stencil buffer */
975 pipe_transfer_unmap(pipe, pt);
976 pipe->transfer_destroy(pipe, pt);
981 * Get fragment program variant for a glDrawPixels or glCopyPixels
982 * command for RGBA data.
984 static struct st_fp_variant *
985 get_color_fp_variant(struct st_context *st)
987 struct gl_context *ctx = st->ctx;
988 struct st_fp_variant_key key;
989 struct st_fp_variant *fpv;
991 memset(&key, 0, sizeof(key));
995 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
996 ctx->Pixel.RedScale != 1.0 ||
997 ctx->Pixel.GreenBias != 0.0 ||
998 ctx->Pixel.GreenScale != 1.0 ||
999 ctx->Pixel.BlueBias != 0.0 ||
1000 ctx->Pixel.BlueScale != 1.0 ||
1001 ctx->Pixel.AlphaBias != 0.0 ||
1002 ctx->Pixel.AlphaScale != 1.0);
1003 key.pixelMaps = ctx->Pixel.MapColorFlag;
1005 fpv = st_get_fp_variant(st, st->fp, &key);
1012 * Get fragment program variant for a glDrawPixels or glCopyPixels
1013 * command for depth/stencil data.
1015 static struct st_fp_variant *
1016 get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth,
1017 GLboolean write_stencil)
1019 struct st_fp_variant_key key;
1020 struct st_fp_variant *fpv;
1022 memset(&key, 0, sizeof(key));
1026 key.drawpixels_z = write_depth;
1027 key.drawpixels_stencil = write_stencil;
1029 fpv = st_get_fp_variant(st, st->fp, &key);
1036 * Called via ctx->Driver.DrawPixels()
1039 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
1040 GLsizei width, GLsizei height,
1041 GLenum format, GLenum type,
1042 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
1044 void *driver_vp, *driver_fp;
1045 struct st_context *st = st_context(ctx);
1046 const GLfloat *color;
1047 struct pipe_context *pipe = st->pipe;
1048 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
1049 struct pipe_sampler_view *sv[2];
1050 int num_sampler_view = 1;
1051 struct st_fp_variant *fpv;
1053 if (format == GL_DEPTH_STENCIL)
1054 write_stencil = write_depth = GL_TRUE;
1055 else if (format == GL_STENCIL_INDEX)
1056 write_stencil = GL_TRUE;
1057 else if (format == GL_DEPTH_COMPONENT)
1058 write_depth = GL_TRUE;
1060 if (write_stencil &&
1061 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) {
1062 /* software fallback */
1063 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1068 /* Mesa state should be up to date by now */
1069 assert(ctx->NewState == 0x0);
1071 st_validate_state(st);
1074 * Get vertex/fragment shaders
1076 if (write_depth || write_stencil) {
1077 fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil);
1079 driver_fp = fpv->driver_shader;
1081 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1083 color = ctx->Current.RasterColor;
1086 fpv = get_color_fp_variant(st);
1088 driver_fp = fpv->driver_shader;
1090 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1093 if (st->pixel_xfer.pixelmap_enabled) {
1094 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1099 /* update fragment program constants */
1100 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1102 /* draw with textured quad */
1104 struct pipe_resource *pt
1105 = make_texture(st, width, height, format, type, unpack, pixels);
1107 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1110 /* Create a second sampler view to read stencil.
1111 * The stencil is written using the shader stencil export
1113 if (write_stencil) {
1114 enum pipe_format stencil_format = PIPE_FORMAT_NONE;
1116 switch (pt->format) {
1117 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
1118 case PIPE_FORMAT_X24S8_USCALED:
1119 stencil_format = PIPE_FORMAT_X24S8_USCALED;
1121 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
1122 case PIPE_FORMAT_S8X24_USCALED:
1123 stencil_format = PIPE_FORMAT_S8X24_USCALED;
1125 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED:
1126 case PIPE_FORMAT_X32_S8X24_USCALED:
1127 stencil_format = PIPE_FORMAT_X32_S8X24_USCALED;
1129 case PIPE_FORMAT_S8_USCALED:
1130 stencil_format = PIPE_FORMAT_S8_USCALED;
1136 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1141 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1143 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1148 color, GL_FALSE, write_depth, write_stencil);
1149 pipe_sampler_view_reference(&sv[0], NULL);
1150 if (num_sampler_view > 1)
1151 pipe_sampler_view_reference(&sv[1], NULL);
1153 pipe_resource_reference(&pt, NULL);
1161 * Software fallback for glCopyPixels(GL_STENCIL).
1164 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1165 GLsizei width, GLsizei height,
1166 GLint dstx, GLint dsty)
1168 struct st_renderbuffer *rbDraw;
1169 struct pipe_context *pipe = st_context(ctx)->pipe;
1170 enum pipe_transfer_usage usage;
1171 struct pipe_transfer *ptDraw;
1176 buffer = malloc(width * height * sizeof(ubyte));
1178 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1182 /* Get the dest renderbuffer. If there's a wrapper, use the
1183 * underlying renderbuffer.
1185 rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
1186 if (rbDraw->Base.Wrapped)
1187 rbDraw = st_renderbuffer(rbDraw->Base.Wrapped);
1189 /* this will do stencil pixel transfer ops */
1190 st_read_stencil_pixels(ctx, srcx, srcy, width, height,
1191 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1192 &ctx->DefaultPacking, buffer);
1195 /* debug code: dump stencil values */
1197 for (row = 0; row < height; row++) {
1198 printf("%3d: ", row);
1199 for (col = 0; col < width; col++) {
1200 printf("%02x ", buffer[col + row * width]);
1206 if (util_format_get_component_bits(rbDraw->format,
1207 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
1208 usage = PIPE_TRANSFER_READ_WRITE;
1210 usage = PIPE_TRANSFER_WRITE;
1212 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1213 dsty = rbDraw->Base.Height - dsty - height;
1216 ptDraw = pipe_get_transfer(pipe,
1219 rbDraw->rtt_face + rbDraw->rtt_slice,
1223 assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
1224 assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
1226 /* map the stencil buffer */
1227 drawMap = pipe_transfer_map(pipe, ptDraw);
1230 /* XXX PixelZoom not handled yet */
1231 for (i = 0; i < height; i++) {
1238 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1242 dst = drawMap + y * ptDraw->stride;
1243 src = buffer + i * width;
1245 switch (ptDraw->resource->format) {
1246 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
1248 uint *dst4 = (uint *) dst;
1250 assert(usage == PIPE_TRANSFER_READ_WRITE);
1251 for (j = 0; j < width; j++) {
1252 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
1257 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
1259 uint *dst4 = (uint *) dst;
1261 assert(usage == PIPE_TRANSFER_READ_WRITE);
1262 for (j = 0; j < width; j++) {
1263 *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
1268 case PIPE_FORMAT_S8_USCALED:
1269 assert(usage == PIPE_TRANSFER_WRITE);
1270 memcpy(dst, src, width);
1272 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED:
1274 uint *dst4 = (uint *) dst;
1277 assert(usage == PIPE_TRANSFER_READ_WRITE);
1278 for (j = 0; j < width; j++) {
1279 *dst4 = src[j] & 0xff;
1291 /* unmap the stencil buffer */
1292 pipe_transfer_unmap(pipe, ptDraw);
1293 pipe->transfer_destroy(pipe, ptDraw);
1297 /** Do the src/dest regions overlap? */
1299 regions_overlap(GLint srcX, GLint srcY, GLint dstX, GLint dstY,
1300 GLsizei width, GLsizei height)
1302 if (srcX + width <= dstX ||
1303 dstX + width <= srcX ||
1304 srcY + height <= dstY ||
1305 dstY + height <= srcY)
1313 * Try to do a glCopyPixels for simple cases with a blit by calling
1314 * pipe->resource_copy_region().
1316 * We can do this when we're copying color pixels (depth/stencil
1317 * eventually) with no pixel zoom, no pixel transfer ops, no
1318 * per-fragment ops, the src/dest regions don't overlap and the
1319 * src/dest pixel formats are the same.
1322 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1323 GLsizei width, GLsizei height,
1324 GLint dstx, GLint dsty, GLenum type)
1326 struct st_context *st = st_context(ctx);
1327 struct pipe_context *pipe = st->pipe;
1328 struct gl_pixelstore_attrib pack, unpack;
1329 GLint readX, readY, readW, readH;
1331 if (type == GL_COLOR &&
1332 ctx->Pixel.ZoomX == 1.0 &&
1333 ctx->Pixel.ZoomY == 1.0 &&
1334 ctx->_ImageTransferState == 0x0 &&
1335 !ctx->Color.BlendEnabled &&
1336 !ctx->Color.AlphaEnabled &&
1338 !ctx->Fog.Enabled &&
1339 !ctx->Stencil.Enabled &&
1340 !ctx->FragmentProgram.Enabled &&
1341 !ctx->VertexProgram.Enabled &&
1342 !ctx->Shader.CurrentFragmentProgram &&
1343 st_fb_orientation(ctx->ReadBuffer) == st_fb_orientation(ctx->DrawBuffer) &&
1344 ctx->DrawBuffer->_NumColorDrawBuffers == 1 &&
1345 !ctx->Query.CondRenderQuery) {
1346 struct st_renderbuffer *rbRead, *rbDraw;
1350 * Clip the read region against the src buffer bounds.
1351 * We'll still allocate a temporary buffer/texture for the original
1352 * src region size but we'll only read the region which is on-screen.
1353 * This may mean that we draw garbage pixels into the dest region, but
1360 pack = ctx->DefaultPacking;
1361 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1362 return GL_TRUE; /* all done */
1364 /* clip against dest buffer bounds and scissor box */
1365 drawX = dstx + pack.SkipPixels;
1366 drawY = dsty + pack.SkipRows;
1368 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1369 return GL_TRUE; /* all done */
1371 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1372 readY = readY - pack.SkipRows + unpack.SkipRows;
1374 rbRead = st_get_color_read_renderbuffer(ctx);
1375 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1377 if ((rbRead != rbDraw ||
1378 !regions_overlap(readX, readY, drawX, drawY, readW, readH)) &&
1379 rbRead->Base.Format == rbDraw->Base.Format) {
1380 struct pipe_box srcBox;
1382 /* flip src/dst position if needed */
1383 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1384 /* both buffers will have the same orientation */
1385 readY = ctx->ReadBuffer->Height - readY - readH;
1386 drawY = ctx->DrawBuffer->Height - drawY - readH;
1389 u_box_2d(readX, readY, readW, readH, &srcBox);
1391 pipe->resource_copy_region(pipe,
1393 rbDraw->rtt_level, drawX, drawY, 0,
1395 rbRead->rtt_level, &srcBox);
1405 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1406 GLsizei width, GLsizei height,
1407 GLint dstx, GLint dsty, GLenum type)
1409 struct st_context *st = st_context(ctx);
1410 struct pipe_context *pipe = st->pipe;
1411 struct pipe_screen *screen = pipe->screen;
1412 struct st_renderbuffer *rbRead;
1413 void *driver_vp, *driver_fp;
1414 struct pipe_resource *pt;
1415 struct pipe_sampler_view *sv[2];
1416 int num_sampler_view = 1;
1418 enum pipe_format srcFormat, texFormat;
1419 GLboolean invertTex = GL_FALSE;
1420 GLint readX, readY, readW, readH;
1421 GLuint sample_count;
1422 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1423 struct st_fp_variant *fpv;
1425 st_validate_state(st);
1427 if (type == GL_STENCIL) {
1428 /* can't use texturing to do stencil */
1429 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1433 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1437 * The subsequent code implements glCopyPixels by copying the source
1438 * pixels into a temporary texture that's then applied to a textured quad.
1439 * When we draw the textured quad, all the usual per-fragment operations
1445 * Get vertex/fragment shaders
1447 if (type == GL_COLOR) {
1448 rbRead = st_get_color_read_renderbuffer(ctx);
1451 fpv = get_color_fp_variant(st);
1452 driver_fp = fpv->driver_shader;
1454 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1456 if (st->pixel_xfer.pixelmap_enabled) {
1457 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1462 assert(type == GL_DEPTH);
1463 rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
1464 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1466 fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE);
1467 driver_fp = fpv->driver_shader;
1469 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1472 /* update fragment program constants */
1473 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1476 if (rbRead->Base.Wrapped)
1477 rbRead = st_renderbuffer(rbRead->Base.Wrapped);
1479 sample_count = rbRead->texture->nr_samples;
1480 /* I believe this would be legal, presumably would need to do a resolve
1481 for color, and for depth/stencil spec says to just use one of the
1482 depth/stencil samples per pixel? Need some transfer clarifications. */
1483 assert(sample_count < 2);
1485 srcFormat = rbRead->texture->format;
1487 if (screen->is_format_supported(screen, srcFormat, st->internal_target,
1489 PIPE_BIND_SAMPLER_VIEW)) {
1490 texFormat = srcFormat;
1493 /* srcFormat can't be used as a texture format */
1494 if (type == GL_DEPTH) {
1495 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
1496 GL_NONE, GL_NONE, st->internal_target,
1497 sample_count, PIPE_BIND_DEPTH_STENCIL);
1498 assert(texFormat != PIPE_FORMAT_NONE);
1501 /* default color format */
1502 texFormat = st_choose_format(screen, GL_RGBA,
1503 GL_NONE, GL_NONE, st->internal_target,
1504 sample_count, PIPE_BIND_SAMPLER_VIEW);
1505 assert(texFormat != PIPE_FORMAT_NONE);
1509 /* Invert src region if needed */
1510 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1511 srcy = ctx->ReadBuffer->Height - srcy - height;
1512 invertTex = !invertTex;
1515 /* Clip the read region against the src buffer bounds.
1516 * We'll still allocate a temporary buffer/texture for the original
1517 * src region size but we'll only read the region which is on-screen.
1518 * This may mean that we draw garbage pixels into the dest region, but
1525 _mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack);
1526 readW = MAX2(0, readW);
1527 readH = MAX2(0, readH);
1529 /* alloc temporary texture */
1530 pt = alloc_texture(st, width, height, texFormat);
1534 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1536 pipe_resource_reference(&pt, NULL);
1540 /* Make temporary texture which is a copy of the src region.
1542 if (srcFormat == texFormat) {
1543 struct pipe_box src_box;
1544 u_box_2d(readX, readY, readW, readH, &src_box);
1545 /* copy source framebuffer surface into mipmap/texture */
1546 pipe->resource_copy_region(pipe,
1549 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
1550 rbRead->texture, /* src tex */
1551 rbRead->rtt_level, /* src lvl */
1556 /* CPU-based fallback/conversion */
1557 struct pipe_transfer *ptRead =
1558 pipe_get_transfer(st->pipe, rbRead->texture,
1560 rbRead->rtt_face + rbRead->rtt_slice,
1562 readX, readY, readW, readH);
1563 struct pipe_transfer *ptTex;
1564 enum pipe_transfer_usage transfer_usage;
1566 if (ST_DEBUG & DEBUG_FALLBACK)
1567 debug_printf("%s: fallback processing\n", __FUNCTION__);
1569 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
1570 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1572 transfer_usage = PIPE_TRANSFER_WRITE;
1574 ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage,
1575 0, 0, width, height);
1577 /* copy image from ptRead surface to ptTex surface */
1578 if (type == GL_COLOR) {
1579 /* alternate path using get/put_tile() */
1580 GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1581 enum pipe_format readFormat, drawFormat;
1582 readFormat = util_format_linear(rbRead->texture->format);
1583 drawFormat = util_format_linear(pt->format);
1584 pipe_get_tile_rgba_format(pipe, ptRead, 0, 0, readW, readH,
1586 pipe_put_tile_rgba_format(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1587 readW, readH, drawFormat, buf);
1592 GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint));
1593 pipe_get_tile_z(pipe, ptRead, 0, 0, readW, readH, buf);
1594 pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1599 pipe->transfer_destroy(pipe, ptRead);
1600 pipe->transfer_destroy(pipe, ptTex);
1603 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1604 * textured quad with that texture.
1606 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1607 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1612 color, invertTex, GL_FALSE, GL_FALSE);
1614 pipe_resource_reference(&pt, NULL);
1615 pipe_sampler_view_reference(&sv[0], NULL);
1620 void st_init_drawpixels_functions(struct dd_function_table *functions)
1622 functions->DrawPixels = st_DrawPixels;
1623 functions->CopyPixels = st_CopyPixels;
1628 st_destroy_drawpix(struct st_context *st)
1632 for (i = 0; i < Elements(st->drawpix.shaders); i++) {
1633 if (st->drawpix.shaders[i])
1634 _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL);
1637 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1638 if (st->drawpix.vert_shaders[0])
1639 ureg_free_tokens(st->drawpix.vert_shaders[0]);
1640 if (st->drawpix.vert_shaders[1])
1641 ureg_free_tokens(st->drawpix.vert_shaders[1]);
1644 #endif /* FEATURE_drawpix */