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