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