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