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