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