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