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"
40 #include "main/texformat.h"
41 #include "main/texstore.h"
42 #include "program/program.h"
43 #include "program/prog_print.h"
44 #include "program/prog_instruction.h"
47 #include "st_atom_constbuf.h"
48 #include "st_cb_drawpixels.h"
49 #include "st_cb_readpixels.h"
50 #include "st_cb_fbo.h"
51 #include "st_context.h"
53 #include "st_format.h"
54 #include "st_program.h"
55 #include "st_texture.h"
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "tgsi/tgsi_ureg.h"
60 #include "util/u_draw_quad.h"
61 #include "util/u_format.h"
62 #include "util/u_inlines.h"
63 #include "util/u_math.h"
64 #include "util/u_tile.h"
65 #include "cso_cache/cso_context.h"
71 * Check if the given program is:
72 * 0: MOVE result.color, fragment.color;
76 is_passthrough_program(const struct gl_fragment_program *prog)
78 if (prog->Base.NumInstructions == 2) {
79 const struct prog_instruction *inst = prog->Base.Instructions;
80 if (inst[0].Opcode == OPCODE_MOV &&
81 inst[1].Opcode == OPCODE_END &&
82 inst[0].DstReg.File == PROGRAM_OUTPUT &&
83 inst[0].DstReg.Index == FRAG_RESULT_COLOR &&
84 inst[0].DstReg.WriteMask == WRITEMASK_XYZW &&
85 inst[0].SrcReg[0].File == PROGRAM_INPUT &&
86 inst[0].SrcReg[0].Index == FRAG_ATTRIB_COL0 &&
87 inst[0].SrcReg[0].Swizzle == SWIZZLE_XYZW) {
97 * Make fragment shader for glDraw/CopyPixels. This shader is made
98 * by combining the pixel transfer shader with the user-defined shader.
99 * \param fpIn the current/incoming fragment program
100 * \param fpOut returns the combined fragment program
103 st_make_drawpix_fragment_program(struct st_context *st,
104 struct gl_fragment_program *fpIn,
105 struct gl_fragment_program **fpOut)
107 struct gl_program *newProg;
109 if (is_passthrough_program(fpIn)) {
110 newProg = (struct gl_program *) _mesa_clone_fragment_program(st->ctx,
111 &st->pixel_xfer.program->Base);
116 printf("Base program:\n");
117 _mesa_print_program(&fpIn->Base);
118 printf("DrawPix program:\n");
119 _mesa_print_program(&st->pixel_xfer.program->Base.Base);
121 newProg = _mesa_combine_programs(st->ctx,
122 &st->pixel_xfer.program->Base.Base,
128 printf("Combined DrawPixels program:\n");
129 _mesa_print_program(newProg);
130 printf("InputsRead: 0x%x\n", newProg->InputsRead);
131 printf("OutputsWritten: 0x%x\n", newProg->OutputsWritten);
132 _mesa_print_parameter_list(newProg->Parameters);
135 *fpOut = (struct gl_fragment_program *) newProg;
140 * Create fragment program that does a TEX() instruction to get a Z and/or
141 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
142 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
143 * Pass fragment color through as-is.
144 * \return pointer to the gl_fragment program
146 struct gl_fragment_program *
147 st_make_drawpix_z_stencil_program(struct st_context *st,
148 GLboolean write_depth,
149 GLboolean write_stencil)
151 struct gl_context *ctx = st->ctx;
152 struct gl_program *p;
153 struct gl_fragment_program *fp;
155 const GLuint shaderIndex = write_depth * 2 + write_stencil;
157 assert(shaderIndex < Elements(st->drawpix.shaders));
159 if (st->drawpix.shaders[shaderIndex]) {
160 /* already have the proper shader */
161 return st->drawpix.shaders[shaderIndex];
167 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
171 p->NumInstructions = write_depth ? 2 : 1;
172 p->NumInstructions += write_stencil ? 1 : 0;
174 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
175 if (!p->Instructions) {
176 ctx->Driver.DeleteProgram(ctx, p);
179 _mesa_init_instructions(p->Instructions, p->NumInstructions);
182 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
183 p->Instructions[ic].Opcode = OPCODE_TEX;
184 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
185 p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH;
186 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z;
187 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
188 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
189 p->Instructions[ic].TexSrcUnit = 0;
190 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
195 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
196 p->Instructions[ic].Opcode = OPCODE_TEX;
197 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
198 p->Instructions[ic].DstReg.Index = FRAG_RESULT_STENCIL;
199 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Y;
200 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
201 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
202 p->Instructions[ic].TexSrcUnit = 1;
203 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
208 p->Instructions[ic++].Opcode = OPCODE_END;
210 assert(ic == p->NumInstructions);
212 p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0;
213 p->OutputsWritten = 0;
215 p->OutputsWritten |= (1 << FRAG_RESULT_DEPTH);
217 p->OutputsWritten |= (1 << FRAG_RESULT_STENCIL);
219 p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
221 p->SamplersUsed |= 1 << 1;
223 fp = (struct gl_fragment_program *) p;
225 /* save the new shader */
226 st->drawpix.shaders[shaderIndex] = fp;
233 * Create a simple vertex shader that just passes through the
234 * vertex position and texcoord (and optionally, color).
237 make_passthrough_vertex_shader(struct st_context *st,
240 if (!st->drawpix.vert_shaders[passColor]) {
241 struct ureg_program *ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
246 /* MOV result.pos, vertex.pos; */
248 ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ),
249 ureg_DECL_vs_input( ureg, 0 ));
251 /* MOV result.texcoord0, vertex.attr[1]; */
253 ureg_DECL_output( ureg, TGSI_SEMANTIC_GENERIC, 0 ),
254 ureg_DECL_vs_input( ureg, 1 ));
257 /* MOV result.color0, vertex.attr[2]; */
259 ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ),
260 ureg_DECL_vs_input( ureg, 2 ));
265 st->drawpix.vert_shaders[passColor] =
266 ureg_create_shader_and_destroy( ureg, st->pipe );
269 return st->drawpix.vert_shaders[passColor];
274 * Return a texture base format for drawing/copying an image
275 * of the given format.
278 base_format(GLenum format)
281 case GL_DEPTH_COMPONENT:
282 return GL_DEPTH_COMPONENT;
283 case GL_DEPTH_STENCIL:
284 return GL_DEPTH_STENCIL;
285 case GL_STENCIL_INDEX:
286 return GL_STENCIL_INDEX;
294 * Return a texture internalFormat for drawing/copying an image
295 * of the given format and type.
298 internal_format(GLenum format, GLenum type)
301 case GL_DEPTH_COMPONENT:
302 return GL_DEPTH_COMPONENT;
303 case GL_DEPTH_STENCIL:
304 return GL_DEPTH_STENCIL;
305 case GL_STENCIL_INDEX:
306 return GL_STENCIL_INDEX;
308 if (_mesa_is_integer_format(format)) {
312 case GL_UNSIGNED_BYTE:
316 case GL_UNSIGNED_SHORT:
320 case GL_UNSIGNED_INT:
323 assert(0 && "Unexpected type in internal_format()");
324 return GL_RGBA_INTEGER;
335 * Create a temporary texture to hold an image of the given size.
336 * If width, height are not POT and the driver only handles POT textures,
337 * allocate the next larger size of texture that is POT.
339 static struct pipe_resource *
340 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
341 enum pipe_format texFormat)
343 struct pipe_resource *pt;
345 pt = st_texture_create(st, st->internal_target, texFormat, 0,
346 width, height, 1, 1, PIPE_BIND_SAMPLER_VIEW);
353 * Make texture containing an image for glDrawPixels image.
354 * If 'pixels' is NULL, leave the texture image data undefined.
356 static struct pipe_resource *
357 make_texture(struct st_context *st,
358 GLsizei width, GLsizei height, GLenum format, GLenum type,
359 const struct gl_pixelstore_attrib *unpack,
360 const GLvoid *pixels)
362 struct gl_context *ctx = st->ctx;
363 struct pipe_context *pipe = st->pipe;
365 struct pipe_resource *pt;
366 enum pipe_format pipeFormat;
368 GLenum baseFormat, intFormat;
370 baseFormat = base_format(format);
371 intFormat = internal_format(format, type);
373 mformat = st_ChooseTextureFormat_renderable(ctx, intFormat,
374 format, type, GL_FALSE);
377 pipeFormat = st_mesa_format_to_pipe_format(mformat);
379 cpp = util_format_get_blocksize(pipeFormat);
381 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
385 /* alloc temporary texture */
386 pt = alloc_texture(st, width, height, pipeFormat);
388 _mesa_unmap_pbo_source(ctx, unpack);
393 struct pipe_transfer *transfer;
394 static const GLuint dstImageOffsets = 0;
397 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
399 /* we'll do pixel transfer in a fragment shader */
400 ctx->_ImageTransferState = 0x0;
402 transfer = pipe_get_transfer(st->pipe, pt, 0, 0,
403 PIPE_TRANSFER_WRITE, 0, 0,
406 /* map texture transfer */
407 dest = pipe_transfer_map(pipe, transfer);
410 /* Put image into texture transfer.
411 * Note that the image is actually going to be upside down in
412 * the texture. We deal with that with texcoords.
414 success = _mesa_texstore(ctx, 2, /* dims */
415 baseFormat, /* baseInternalFormat */
416 mformat, /* gl_format */
418 0, 0, 0, /* dstX/Y/Zoffset */
419 transfer->stride, /* dstRowStride, bytes */
420 &dstImageOffsets, /* dstImageOffsets */
421 width, height, 1, /* size */
422 format, type, /* src format/type */
423 pixels, /* data source */
427 pipe_transfer_unmap(pipe, transfer);
428 pipe->transfer_destroy(pipe, transfer);
433 ctx->_ImageTransferState = imageTransferStateSave;
436 _mesa_unmap_pbo_source(ctx, unpack);
443 * Draw quad with texcoords and optional color.
444 * Coords are gallium window coords with y=0=top.
445 * \param color may be null
446 * \param invertTex if true, flip texcoords vertically
449 draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z,
450 GLfloat x1, GLfloat y1, const GLfloat *color,
451 GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
453 struct st_context *st = st_context(ctx);
454 struct pipe_context *pipe = st->pipe;
455 GLfloat verts[4][3][4]; /* four verts, three attribs, XYZW */
457 /* setup vertex data */
459 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
460 const GLfloat fb_width = (GLfloat) fb->Width;
461 const GLfloat fb_height = (GLfloat) fb->Height;
462 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
463 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
464 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
465 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
466 const GLfloat sLeft = 0.0f, sRight = maxXcoord;
467 const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
468 const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
472 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
473 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
476 verts[1][0][0] = clip_x1;
477 verts[1][0][1] = clip_y0;
480 verts[2][0][0] = clip_x1;
481 verts[2][0][1] = clip_y1;
484 verts[3][0][0] = clip_x0;
485 verts[3][0][1] = clip_y1;
487 verts[0][1][0] = sLeft; /* v[0].attr[1].S */
488 verts[0][1][1] = tTop; /* v[0].attr[1].T */
489 verts[1][1][0] = sRight;
490 verts[1][1][1] = tTop;
491 verts[2][1][0] = sRight;
492 verts[2][1][1] = tBot;
493 verts[3][1][0] = sLeft;
494 verts[3][1][1] = tBot;
496 /* same for all verts: */
498 for (i = 0; i < 4; i++) {
499 verts[i][0][2] = z; /* v[i].attr[0].z */
500 verts[i][0][3] = 1.0f; /* v[i].attr[0].w */
501 verts[i][2][0] = color[0]; /* v[i].attr[2].r */
502 verts[i][2][1] = color[1]; /* v[i].attr[2].g */
503 verts[i][2][2] = color[2]; /* v[i].attr[2].b */
504 verts[i][2][3] = color[3]; /* v[i].attr[2].a */
505 verts[i][1][2] = 0.0f; /* v[i].attr[1].R */
506 verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */
510 for (i = 0; i < 4; i++) {
511 verts[i][0][2] = z; /*Z*/
512 verts[i][0][3] = 1.0f; /*W*/
513 verts[i][1][2] = 0.0f; /*R*/
514 verts[i][1][3] = 1.0f; /*Q*/
520 struct pipe_resource *buf;
522 /* allocate/load buffer object with vertex data */
523 buf = pipe_buffer_create(pipe->screen,
524 PIPE_BIND_VERTEX_BUFFER,
527 pipe_buffer_write(st->pipe, buf, 0, sizeof(verts), verts);
529 util_draw_vertex_buffer(pipe, st->cso_context, buf, 0,
532 3); /* attribs/vert */
533 pipe_resource_reference(&buf, NULL);
540 draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
541 GLsizei width, GLsizei height,
542 GLfloat zoomX, GLfloat zoomY,
543 struct pipe_sampler_view **sv,
544 int num_sampler_view,
547 const GLfloat *color,
549 GLboolean write_depth, GLboolean write_stencil)
551 struct st_context *st = st_context(ctx);
552 struct pipe_context *pipe = st->pipe;
553 struct cso_context *cso = st->cso_context;
554 GLfloat x0, y0, x1, y1;
556 boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT;
559 /* XXX if DrawPixels image is larger than max texture size, break
562 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
563 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
564 assert(width <= maxSize);
565 assert(height <= maxSize);
567 cso_save_rasterizer(cso);
568 cso_save_viewport(cso);
569 cso_save_samplers(cso);
570 cso_save_fragment_sampler_views(cso);
571 cso_save_fragment_shader(cso);
572 cso_save_vertex_shader(cso);
573 cso_save_vertex_elements(cso);
574 cso_save_vertex_buffers(cso);
576 cso_save_depth_stencil_alpha(cso);
580 /* rasterizer state: just scissor */
582 struct pipe_rasterizer_state rasterizer;
583 memset(&rasterizer, 0, sizeof(rasterizer));
584 rasterizer.gl_rasterization_rules = 1;
585 rasterizer.scissor = ctx->Scissor.Enabled;
586 cso_set_rasterizer(cso, &rasterizer);
590 /* Stencil writing bypasses the normal fragment pipeline to
591 * disable color writing and set stencil test to always pass.
593 struct pipe_depth_stencil_alpha_state dsa;
594 struct pipe_blend_state blend;
597 memset(&dsa, 0, sizeof(dsa));
598 dsa.stencil[0].enabled = 1;
599 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
600 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
601 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
603 /* writing depth+stencil: depth test always passes */
604 dsa.depth.enabled = 1;
605 dsa.depth.writemask = ctx->Depth.Mask;
606 dsa.depth.func = PIPE_FUNC_ALWAYS;
608 cso_set_depth_stencil_alpha(cso, &dsa);
610 /* blend (colormask) */
611 memset(&blend, 0, sizeof(blend));
612 cso_set_blend(cso, &blend);
615 /* fragment shader state: TEX lookup program */
616 cso_set_fragment_shader_handle(cso, driver_fp);
618 /* vertex shader state: position + texcoord pass-through */
619 cso_set_vertex_shader_handle(cso, driver_vp);
622 /* texture sampling state: */
624 struct pipe_sampler_state sampler;
625 memset(&sampler, 0, sizeof(sampler));
626 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
627 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
628 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
629 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
630 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
631 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
632 sampler.normalized_coords = normalized;
634 cso_single_sampler(cso, 0, &sampler);
635 if (num_sampler_view > 1) {
636 cso_single_sampler(cso, 1, &sampler);
638 cso_single_sampler_done(cso);
641 /* viewport state: viewport matching window dims */
643 const float w = (float) ctx->DrawBuffer->Width;
644 const float h = (float) ctx->DrawBuffer->Height;
645 struct pipe_viewport_state vp;
646 vp.scale[0] = 0.5f * w;
647 vp.scale[1] = -0.5f * h;
650 vp.translate[0] = 0.5f * w;
651 vp.translate[1] = 0.5f * h;
652 vp.translate[2] = 0.5f;
653 vp.translate[3] = 0.0f;
654 cso_set_viewport(cso, &vp);
657 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
660 cso_set_fragment_sampler_views(cso, num_sampler_view, sv);
662 /* Compute Gallium window coords (y=0=top) with pixel zoom.
663 * Recall that these coords are transformed by the current
664 * vertex shader and viewport transformation.
666 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
667 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
668 invertTex = !invertTex;
672 x1 = x + width * ctx->Pixel.ZoomX;
674 y1 = y + height * ctx->Pixel.ZoomY;
676 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
679 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
680 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
681 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
684 cso_restore_rasterizer(cso);
685 cso_restore_viewport(cso);
686 cso_restore_samplers(cso);
687 cso_restore_fragment_sampler_views(cso);
688 cso_restore_fragment_shader(cso);
689 cso_restore_vertex_shader(cso);
690 cso_restore_vertex_elements(cso);
691 cso_restore_vertex_buffers(cso);
693 cso_restore_depth_stencil_alpha(cso);
694 cso_restore_blend(cso);
700 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
701 * can't use a fragment shader to write stencil values.
704 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
705 GLsizei width, GLsizei height, GLenum format, GLenum type,
706 const struct gl_pixelstore_attrib *unpack,
707 const GLvoid *pixels)
709 struct st_context *st = st_context(ctx);
710 struct pipe_context *pipe = st->pipe;
711 struct st_renderbuffer *strb;
712 enum pipe_transfer_usage usage;
713 struct pipe_transfer *pt;
714 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
717 struct gl_pixelstore_attrib clippedUnpack = *unpack;
720 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
722 /* totally clipped */
727 strb = st_renderbuffer(ctx->DrawBuffer->
728 Attachment[BUFFER_STENCIL].Renderbuffer);
730 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
731 y = ctx->DrawBuffer->Height - y - height;
734 if(format != GL_DEPTH_STENCIL &&
735 util_format_get_component_bits(strb->format,
736 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
737 usage = PIPE_TRANSFER_READ_WRITE;
739 usage = PIPE_TRANSFER_WRITE;
741 pt = pipe_get_transfer(st_context(ctx)->pipe, strb->texture, 0, 0,
745 stmap = pipe_transfer_map(pipe, pt);
747 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
750 /* if width > MAX_WIDTH, have to process image in chunks */
752 while (skipPixels < width) {
753 const GLint spanX = skipPixels;
754 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
756 for (row = 0; row < height; row++) {
757 GLubyte sValues[MAX_WIDTH];
758 GLuint zValues[MAX_WIDTH];
759 GLenum destType = GL_UNSIGNED_BYTE;
760 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
764 _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
765 type, source, &clippedUnpack,
766 ctx->_ImageTransferState);
768 if (format == GL_DEPTH_STENCIL) {
769 _mesa_unpack_depth_span(ctx, spanWidth, GL_UNSIGNED_INT, zValues,
770 (1 << 24) - 1, type, source,
775 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
776 "zoom not complete");
782 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
783 spanY = height - row - 1;
789 /* now pack the stencil (and Z) values in the dest format */
790 switch (pt->resource->format) {
791 case PIPE_FORMAT_S8_USCALED:
793 ubyte *dest = stmap + spanY * pt->stride + spanX;
794 assert(usage == PIPE_TRANSFER_WRITE);
795 memcpy(dest, sValues, spanWidth);
798 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
799 if (format == GL_DEPTH_STENCIL) {
800 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
802 assert(usage == PIPE_TRANSFER_WRITE);
803 for (k = 0; k < spanWidth; k++) {
804 dest[k] = zValues[k] | (sValues[k] << 24);
808 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
810 assert(usage == PIPE_TRANSFER_READ_WRITE);
811 for (k = 0; k < spanWidth; k++) {
812 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
816 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
817 if (format == GL_DEPTH_STENCIL) {
818 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
820 assert(usage == PIPE_TRANSFER_WRITE);
821 for (k = 0; k < spanWidth; k++) {
822 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
826 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
828 assert(usage == PIPE_TRANSFER_READ_WRITE);
829 for (k = 0; k < spanWidth; k++) {
830 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
839 skipPixels += spanWidth;
842 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
844 /* unmap the stencil buffer */
845 pipe_transfer_unmap(pipe, pt);
846 pipe->transfer_destroy(pipe, pt);
851 * Get fragment program variant for a glDrawPixels or glCopyPixels
852 * command for RGBA data.
854 static struct st_fp_variant *
855 get_color_fp_variant(struct st_context *st)
857 struct gl_context *ctx = st->ctx;
858 struct st_fp_variant_key key;
859 struct st_fp_variant *fpv;
861 memset(&key, 0, sizeof(key));
865 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
866 ctx->Pixel.RedScale != 1.0 ||
867 ctx->Pixel.GreenBias != 0.0 ||
868 ctx->Pixel.GreenScale != 1.0 ||
869 ctx->Pixel.BlueBias != 0.0 ||
870 ctx->Pixel.BlueScale != 1.0 ||
871 ctx->Pixel.AlphaBias != 0.0 ||
872 ctx->Pixel.AlphaScale != 1.0);
873 key.pixelMaps = ctx->Pixel.MapColorFlag;
875 fpv = st_get_fp_variant(st, st->fp, &key);
882 * Get fragment program variant for a glDrawPixels or glCopyPixels
883 * command for depth/stencil data.
885 static struct st_fp_variant *
886 get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth,
887 GLboolean write_stencil)
889 struct st_fp_variant_key key;
890 struct st_fp_variant *fpv;
892 memset(&key, 0, sizeof(key));
896 key.drawpixels_z = write_depth;
897 key.drawpixels_stencil = write_stencil;
899 fpv = st_get_fp_variant(st, st->fp, &key);
906 * Called via ctx->Driver.DrawPixels()
909 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
910 GLsizei width, GLsizei height,
911 GLenum format, GLenum type,
912 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
914 void *driver_vp, *driver_fp;
915 struct st_context *st = st_context(ctx);
916 const GLfloat *color;
917 struct pipe_context *pipe = st->pipe;
918 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
919 struct pipe_sampler_view *sv[2];
920 int num_sampler_view = 1;
921 enum pipe_format stencil_format = PIPE_FORMAT_NONE;
922 struct st_fp_variant *fpv;
924 if (format == GL_DEPTH_STENCIL)
925 write_stencil = write_depth = GL_TRUE;
926 else if (format == GL_STENCIL_INDEX)
927 write_stencil = GL_TRUE;
928 else if (format == GL_DEPTH_COMPONENT)
929 write_depth = GL_TRUE;
932 enum pipe_format tex_format;
933 /* can we write to stencil if not fallback */
934 if (!pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT))
935 goto stencil_fallback;
937 tex_format = st_choose_format(st->pipe->screen, base_format(format),
939 0, PIPE_BIND_SAMPLER_VIEW);
940 if (tex_format == PIPE_FORMAT_Z24_UNORM_S8_USCALED)
941 stencil_format = PIPE_FORMAT_X24S8_USCALED;
942 else if (tex_format == PIPE_FORMAT_S8_USCALED_Z24_UNORM)
943 stencil_format = PIPE_FORMAT_S8X24_USCALED;
945 stencil_format = PIPE_FORMAT_S8_USCALED;
946 if (stencil_format == PIPE_FORMAT_NONE)
947 goto stencil_fallback;
950 /* Mesa state should be up to date by now */
951 assert(ctx->NewState == 0x0);
953 st_validate_state(st);
956 * Get vertex/fragment shaders
958 if (write_depth || write_stencil) {
959 fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil);
961 driver_fp = fpv->driver_shader;
963 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
965 color = ctx->Current.RasterColor;
968 fpv = get_color_fp_variant(st);
970 driver_fp = fpv->driver_shader;
972 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
975 if (st->pixel_xfer.pixelmap_enabled) {
976 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
981 /* update fragment program constants */
982 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
984 /* draw with textured quad */
986 struct pipe_resource *pt
987 = make_texture(st, width, height, format, type, unpack, pixels);
989 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
993 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
998 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1000 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1005 color, GL_FALSE, write_depth, write_stencil);
1006 pipe_sampler_view_reference(&sv[0], NULL);
1007 if (num_sampler_view > 1)
1008 pipe_sampler_view_reference(&sv[1], NULL);
1010 pipe_resource_reference(&pt, NULL);
1016 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1023 * Software fallback for glCopyPixels(GL_STENCIL).
1026 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1027 GLsizei width, GLsizei height,
1028 GLint dstx, GLint dsty)
1030 struct st_renderbuffer *rbDraw;
1031 struct pipe_context *pipe = st_context(ctx)->pipe;
1032 enum pipe_transfer_usage usage;
1033 struct pipe_transfer *ptDraw;
1038 buffer = malloc(width * height * sizeof(ubyte));
1040 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1044 /* Get the dest renderbuffer. If there's a wrapper, use the
1045 * underlying renderbuffer.
1047 rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
1048 if (rbDraw->Base.Wrapped)
1049 rbDraw = st_renderbuffer(rbDraw->Base.Wrapped);
1051 /* this will do stencil pixel transfer ops */
1052 st_read_stencil_pixels(ctx, srcx, srcy, width, height,
1053 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1054 &ctx->DefaultPacking, buffer);
1057 /* debug code: dump stencil values */
1059 for (row = 0; row < height; row++) {
1060 printf("%3d: ", row);
1061 for (col = 0; col < width; col++) {
1062 printf("%02x ", buffer[col + row * width]);
1068 if (util_format_get_component_bits(rbDraw->format,
1069 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
1070 usage = PIPE_TRANSFER_READ_WRITE;
1072 usage = PIPE_TRANSFER_WRITE;
1074 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1075 dsty = rbDraw->Base.Height - dsty - height;
1078 ptDraw = pipe_get_transfer(st_context(ctx)->pipe,
1079 rbDraw->texture, 0, 0,
1083 assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
1084 assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
1086 /* map the stencil buffer */
1087 drawMap = pipe_transfer_map(pipe, ptDraw);
1090 /* XXX PixelZoom not handled yet */
1091 for (i = 0; i < height; i++) {
1098 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1102 dst = drawMap + y * ptDraw->stride;
1103 src = buffer + i * width;
1105 switch (ptDraw->resource->format) {
1106 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
1108 uint *dst4 = (uint *) dst;
1110 assert(usage == PIPE_TRANSFER_READ_WRITE);
1111 for (j = 0; j < width; j++) {
1112 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
1117 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
1119 uint *dst4 = (uint *) dst;
1121 assert(usage == PIPE_TRANSFER_READ_WRITE);
1122 for (j = 0; j < width; j++) {
1123 *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
1128 case PIPE_FORMAT_S8_USCALED:
1129 assert(usage == PIPE_TRANSFER_WRITE);
1130 memcpy(dst, src, width);
1139 /* unmap the stencil buffer */
1140 pipe_transfer_unmap(pipe, ptDraw);
1141 pipe->transfer_destroy(pipe, ptDraw);
1145 /** Do the src/dest regions overlap? */
1147 regions_overlap(GLint srcX, GLint srcY, GLint dstX, GLint dstY,
1148 GLsizei width, GLsizei height)
1150 if (srcX + width <= dstX ||
1151 dstX + width <= srcX ||
1152 srcY + height <= dstY ||
1153 dstY + height <= srcY)
1161 * Try to do a glCopyPixels for simple cases with a blit by calling
1162 * pipe->resource_copy_region().
1164 * We can do this when we're copying color pixels (depth/stencil
1165 * eventually) with no pixel zoom, no pixel transfer ops, no
1166 * per-fragment ops, the src/dest regions don't overlap and the
1167 * src/dest pixel formats are the same.
1170 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1171 GLsizei width, GLsizei height,
1172 GLint dstx, GLint dsty, GLenum type)
1174 struct st_context *st = st_context(ctx);
1175 struct pipe_context *pipe = st->pipe;
1176 struct gl_pixelstore_attrib pack, unpack;
1177 GLint readX, readY, readW, readH;
1179 if (type == GL_COLOR &&
1180 ctx->Pixel.ZoomX == 1.0 &&
1181 ctx->Pixel.ZoomY == 1.0 &&
1182 ctx->_ImageTransferState == 0x0 &&
1183 !ctx->Color.BlendEnabled &&
1184 !ctx->Color.AlphaEnabled &&
1186 !ctx->Fog.Enabled &&
1187 !ctx->Stencil.Enabled &&
1188 !ctx->FragmentProgram.Enabled &&
1189 !ctx->VertexProgram.Enabled &&
1190 !ctx->Shader.CurrentFragmentProgram &&
1191 st_fb_orientation(ctx->ReadBuffer) == st_fb_orientation(ctx->DrawBuffer) &&
1192 ctx->DrawBuffer->_NumColorDrawBuffers == 1) {
1193 struct st_renderbuffer *rbRead, *rbDraw;
1197 * Clip the read region against the src buffer bounds.
1198 * We'll still allocate a temporary buffer/texture for the original
1199 * src region size but we'll only read the region which is on-screen.
1200 * This may mean that we draw garbage pixels into the dest region, but
1207 pack = ctx->DefaultPacking;
1208 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1209 return GL_TRUE; /* all done */
1211 /* clip against dest buffer bounds and scissor box */
1212 drawX = dstx + pack.SkipPixels;
1213 drawY = dsty + pack.SkipRows;
1215 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1216 return GL_TRUE; /* all done */
1218 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1219 readY = readY - pack.SkipRows + unpack.SkipRows;
1221 rbRead = st_get_color_read_renderbuffer(ctx);
1222 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1224 if ((rbRead != rbDraw ||
1225 !regions_overlap(readX, readY, drawX, drawY, readW, readH)) &&
1226 rbRead->Base.Format == rbDraw->Base.Format) {
1227 struct pipe_box srcBox;
1229 /* flip src/dst position if needed */
1230 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1231 /* both buffers will have the same orientation */
1232 readY = ctx->ReadBuffer->Height - readY - readH;
1233 drawY = ctx->DrawBuffer->Height - drawY - readH;
1236 u_box_2d(readX, readY, readW, readH, &srcBox);
1238 pipe->resource_copy_region(pipe,
1239 rbDraw->texture, 0, drawX, drawY, 0,
1240 rbRead->texture, 0, &srcBox);
1250 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1251 GLsizei width, GLsizei height,
1252 GLint dstx, GLint dsty, GLenum type)
1254 struct st_context *st = st_context(ctx);
1255 struct pipe_context *pipe = st->pipe;
1256 struct pipe_screen *screen = pipe->screen;
1257 struct st_renderbuffer *rbRead;
1258 void *driver_vp, *driver_fp;
1259 struct pipe_resource *pt;
1260 struct pipe_sampler_view *sv[2];
1261 int num_sampler_view = 1;
1263 enum pipe_format srcFormat, texFormat;
1264 GLboolean invertTex = GL_FALSE;
1265 GLint readX, readY, readW, readH;
1266 GLuint sample_count;
1267 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1268 struct st_fp_variant *fpv;
1270 st_validate_state(st);
1272 if (type == GL_STENCIL) {
1273 /* can't use texturing to do stencil */
1274 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1278 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1282 * The subsequent code implements glCopyPixels by copying the source
1283 * pixels into a temporary texture that's then applied to a textured quad.
1284 * When we draw the textured quad, all the usual per-fragment operations
1290 * Get vertex/fragment shaders
1292 if (type == GL_COLOR) {
1293 rbRead = st_get_color_read_renderbuffer(ctx);
1296 fpv = get_color_fp_variant(st);
1297 driver_fp = fpv->driver_shader;
1299 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1301 if (st->pixel_xfer.pixelmap_enabled) {
1302 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1307 assert(type == GL_DEPTH);
1308 rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
1309 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1311 fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE);
1312 driver_fp = fpv->driver_shader;
1314 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1317 /* update fragment program constants */
1318 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1321 if (rbRead->Base.Wrapped)
1322 rbRead = st_renderbuffer(rbRead->Base.Wrapped);
1324 sample_count = rbRead->texture->nr_samples;
1325 /* I believe this would be legal, presumably would need to do a resolve
1326 for color, and for depth/stencil spec says to just use one of the
1327 depth/stencil samples per pixel? Need some transfer clarifications. */
1328 assert(sample_count < 2);
1330 srcFormat = rbRead->texture->format;
1332 if (screen->is_format_supported(screen, srcFormat, st->internal_target,
1334 PIPE_BIND_SAMPLER_VIEW, 0)) {
1335 texFormat = srcFormat;
1338 /* srcFormat can't be used as a texture format */
1339 if (type == GL_DEPTH) {
1340 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
1341 st->internal_target, sample_count,
1342 PIPE_BIND_DEPTH_STENCIL);
1343 assert(texFormat != PIPE_FORMAT_NONE);
1346 /* default color format */
1347 texFormat = st_choose_format(screen, GL_RGBA, st->internal_target,
1348 sample_count, PIPE_BIND_SAMPLER_VIEW);
1349 assert(texFormat != PIPE_FORMAT_NONE);
1353 /* Invert src region if needed */
1354 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1355 srcy = ctx->ReadBuffer->Height - srcy - height;
1356 invertTex = !invertTex;
1359 /* Clip the read region against the src buffer bounds.
1360 * We'll still allocate a temporary buffer/texture for the original
1361 * src region size but we'll only read the region which is on-screen.
1362 * This may mean that we draw garbage pixels into the dest region, but
1369 _mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack);
1370 readW = MAX2(0, readW);
1371 readH = MAX2(0, readH);
1373 /* alloc temporary texture */
1374 pt = alloc_texture(st, width, height, texFormat);
1378 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1380 pipe_resource_reference(&pt, NULL);
1384 /* Make temporary texture which is a copy of the src region.
1386 if (srcFormat == texFormat) {
1387 struct pipe_box src_box;
1388 u_box_2d(readX, readY, readW, readH, &src_box);
1389 /* copy source framebuffer surface into mipmap/texture */
1390 pipe->resource_copy_region(pipe,
1393 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
1394 rbRead->texture, /* src tex */
1400 /* CPU-based fallback/conversion */
1401 struct pipe_transfer *ptRead =
1402 pipe_get_transfer(st->pipe, rbRead->texture,
1403 0, 0, /* level, layer */
1405 readX, readY, readW, readH);
1406 struct pipe_transfer *ptTex;
1407 enum pipe_transfer_usage transfer_usage;
1409 if (ST_DEBUG & DEBUG_FALLBACK)
1410 debug_printf("%s: fallback processing\n", __FUNCTION__);
1412 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
1413 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1415 transfer_usage = PIPE_TRANSFER_WRITE;
1417 ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage,
1418 0, 0, width, height);
1420 /* copy image from ptRead surface to ptTex surface */
1421 if (type == GL_COLOR) {
1422 /* alternate path using get/put_tile() */
1423 GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1424 enum pipe_format readFormat, drawFormat;
1425 readFormat = util_format_linear(rbRead->texture->format);
1426 drawFormat = util_format_linear(pt->format);
1427 pipe_get_tile_rgba_format(pipe, ptRead, 0, 0, readW, readH,
1429 pipe_put_tile_rgba_format(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1430 readW, readH, drawFormat, buf);
1435 GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint));
1436 pipe_get_tile_z(pipe, ptRead, 0, 0, readW, readH, buf);
1437 pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1442 pipe->transfer_destroy(pipe, ptRead);
1443 pipe->transfer_destroy(pipe, ptTex);
1446 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1447 * textured quad with that texture.
1449 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1450 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1455 color, invertTex, GL_FALSE, GL_FALSE);
1457 pipe_resource_reference(&pt, NULL);
1458 pipe_sampler_view_reference(&sv[0], NULL);
1463 void st_init_drawpixels_functions(struct dd_function_table *functions)
1465 functions->DrawPixels = st_DrawPixels;
1466 functions->CopyPixels = st_CopyPixels;
1471 st_destroy_drawpix(struct st_context *st)
1475 for (i = 0; i < Elements(st->drawpix.shaders); i++) {
1476 if (st->drawpix.shaders[i])
1477 _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL);
1480 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1481 if (st->drawpix.vert_shaders[0])
1482 ureg_free_tokens(st->drawpix.vert_shaders[0]);
1483 if (st->drawpix.vert_shaders[1])
1484 ureg_free_tokens(st->drawpix.vert_shaders[1]);
1487 #endif /* FEATURE_drawpix */