st/mesa: Drop !passColor optimization in drawpixels shaders.
[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, texcoord, and color.
195 */
196 static void
197 make_passthrough_vertex_shader(struct st_context *st)
198 {
199 const enum tgsi_semantic texcoord_semantic = st->needs_texcoord_semantic ?
200 TGSI_SEMANTIC_TEXCOORD : TGSI_SEMANTIC_GENERIC;
201
202 if (!st->drawpix.vert_shader) {
203 struct ureg_program *ureg = ureg_create( PIPE_SHADER_VERTEX );
204
205 if (ureg == NULL)
206 return;
207
208 /* MOV result.pos, vertex.pos; */
209 ureg_MOV(ureg,
210 ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ),
211 ureg_DECL_vs_input( ureg, 0 ));
212
213 /* MOV result.color0, vertex.attr[1]; */
214 ureg_MOV(ureg,
215 ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ),
216 ureg_DECL_vs_input( ureg, 1 ));
217
218 /* MOV result.texcoord0, vertex.attr[2]; */
219 ureg_MOV(ureg,
220 ureg_DECL_output( ureg, texcoord_semantic, 0 ),
221 ureg_DECL_vs_input( ureg, 2 ));
222
223 ureg_END( ureg );
224
225 st->drawpix.vert_shader =
226 ureg_create_shader_and_destroy( ureg, st->pipe );
227 }
228 }
229
230
231 /**
232 * Return a texture internalFormat for drawing/copying an image
233 * of the given format and type.
234 */
235 static GLenum
236 internal_format(struct gl_context *ctx, GLenum format, GLenum type)
237 {
238 switch (format) {
239 case GL_DEPTH_COMPONENT:
240 switch (type) {
241 case GL_UNSIGNED_SHORT:
242 return GL_DEPTH_COMPONENT16;
243
244 case GL_UNSIGNED_INT:
245 return GL_DEPTH_COMPONENT32;
246
247 case GL_FLOAT:
248 if (ctx->Extensions.ARB_depth_buffer_float)
249 return GL_DEPTH_COMPONENT32F;
250 else
251 return GL_DEPTH_COMPONENT;
252
253 default:
254 return GL_DEPTH_COMPONENT;
255 }
256
257 case GL_DEPTH_STENCIL:
258 switch (type) {
259 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
260 return GL_DEPTH32F_STENCIL8;
261
262 case GL_UNSIGNED_INT_24_8:
263 default:
264 return GL_DEPTH24_STENCIL8;
265 }
266
267 case GL_STENCIL_INDEX:
268 return GL_STENCIL_INDEX;
269
270 default:
271 if (_mesa_is_enum_format_integer(format)) {
272 switch (type) {
273 case GL_BYTE:
274 return GL_RGBA8I;
275 case GL_UNSIGNED_BYTE:
276 return GL_RGBA8UI;
277 case GL_SHORT:
278 return GL_RGBA16I;
279 case GL_UNSIGNED_SHORT:
280 return GL_RGBA16UI;
281 case GL_INT:
282 return GL_RGBA32I;
283 case GL_UNSIGNED_INT:
284 return GL_RGBA32UI;
285 default:
286 assert(0 && "Unexpected type in internal_format()");
287 return GL_RGBA_INTEGER;
288 }
289 }
290 else {
291 switch (type) {
292 case GL_UNSIGNED_BYTE:
293 case GL_UNSIGNED_INT_8_8_8_8:
294 case GL_UNSIGNED_INT_8_8_8_8_REV:
295 default:
296 return GL_RGBA8;
297
298 case GL_UNSIGNED_BYTE_3_3_2:
299 case GL_UNSIGNED_BYTE_2_3_3_REV:
300 return GL_R3_G3_B2;
301
302 case GL_UNSIGNED_SHORT_4_4_4_4:
303 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
304 return GL_RGBA4;
305
306 case GL_UNSIGNED_SHORT_5_6_5:
307 case GL_UNSIGNED_SHORT_5_6_5_REV:
308 return GL_RGB565;
309
310 case GL_UNSIGNED_SHORT_5_5_5_1:
311 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
312 return GL_RGB5_A1;
313
314 case GL_UNSIGNED_INT_10_10_10_2:
315 case GL_UNSIGNED_INT_2_10_10_10_REV:
316 return GL_RGB10_A2;
317
318 case GL_UNSIGNED_SHORT:
319 case GL_UNSIGNED_INT:
320 return GL_RGBA16;
321
322 case GL_BYTE:
323 return
324 ctx->Extensions.EXT_texture_snorm ? GL_RGBA8_SNORM : GL_RGBA8;
325
326 case GL_SHORT:
327 case GL_INT:
328 return
329 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
330
331 case GL_HALF_FLOAT_ARB:
332 return
333 ctx->Extensions.ARB_texture_float ? GL_RGBA16F :
334 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
335
336 case GL_FLOAT:
337 case GL_DOUBLE:
338 return
339 ctx->Extensions.ARB_texture_float ? GL_RGBA32F :
340 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
341
342 case GL_UNSIGNED_INT_5_9_9_9_REV:
343 assert(ctx->Extensions.EXT_texture_shared_exponent);
344 return GL_RGB9_E5;
345
346 case GL_UNSIGNED_INT_10F_11F_11F_REV:
347 assert(ctx->Extensions.EXT_packed_float);
348 return GL_R11F_G11F_B10F;
349 }
350 }
351 }
352 }
353
354
355 /**
356 * Create a temporary texture to hold an image of the given size.
357 * If width, height are not POT and the driver only handles POT textures,
358 * allocate the next larger size of texture that is POT.
359 */
360 static struct pipe_resource *
361 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
362 enum pipe_format texFormat, unsigned bind)
363 {
364 struct pipe_resource *pt;
365
366 pt = st_texture_create(st, st->internal_target, texFormat, 0,
367 width, height, 1, 1, 0, bind);
368
369 return pt;
370 }
371
372
373 /**
374 * Search the cache for an image which matches the given parameters.
375 * \return pipe_resource pointer if found, NULL if not found.
376 */
377 static struct pipe_resource *
378 search_drawpixels_cache(struct st_context *st,
379 GLsizei width, GLsizei height,
380 GLenum format, GLenum type,
381 const struct gl_pixelstore_attrib *unpack,
382 const void *pixels)
383 {
384 struct pipe_resource *pt = NULL;
385 const GLint bpp = _mesa_bytes_per_pixel(format, type);
386 unsigned i;
387
388 if ((unpack->RowLength != 0 && unpack->RowLength != width) ||
389 unpack->SkipPixels != 0 ||
390 unpack->SkipRows != 0 ||
391 unpack->SwapBytes ||
392 _mesa_is_bufferobj(unpack->BufferObj)) {
393 /* we don't allow non-default pixel unpacking values */
394 return NULL;
395 }
396
397 /* Search cache entries for a match */
398 for (i = 0; i < ARRAY_SIZE(st->drawpix_cache.entries); i++) {
399 struct drawpix_cache_entry *entry = &st->drawpix_cache.entries[i];
400
401 if (width == entry->width &&
402 height == entry->height &&
403 format == entry->format &&
404 type == entry->type &&
405 pixels == entry->user_pointer &&
406 entry->image) {
407 assert(entry->texture);
408
409 /* check if the pixel data is the same */
410 if (memcmp(pixels, entry->image, width * height * bpp) == 0) {
411 /* Success - found a cache match */
412 pipe_resource_reference(&pt, entry->texture);
413 /* refcount of returned texture should be at least two here. One
414 * reference for the cache to hold on to, one for the caller (which
415 * it will release), and possibly more held by the driver.
416 */
417 assert(pt->reference.count >= 2);
418
419 /* update the age of this entry */
420 entry->age = ++st->drawpix_cache.age;
421
422 return pt;
423 }
424 }
425 }
426
427 /* no cache match found */
428 return NULL;
429 }
430
431
432 /**
433 * Find the oldest entry in the glDrawPixels cache. We'll replace this
434 * one when we need to store a new image.
435 */
436 static struct drawpix_cache_entry *
437 find_oldest_drawpixels_cache_entry(struct st_context *st)
438 {
439 unsigned oldest_age = ~0u, oldest_index = ~0u;
440 unsigned i;
441
442 /* Find entry with oldest (lowest) age */
443 for (i = 0; i < ARRAY_SIZE(st->drawpix_cache.entries); i++) {
444 const struct drawpix_cache_entry *entry = &st->drawpix_cache.entries[i];
445 if (entry->age < oldest_age) {
446 oldest_age = entry->age;
447 oldest_index = i;
448 }
449 }
450
451 assert(oldest_index != ~0u);
452
453 return &st->drawpix_cache.entries[oldest_index];
454 }
455
456
457 /**
458 * Try to save the given glDrawPixels image in the cache.
459 */
460 static void
461 cache_drawpixels_image(struct st_context *st,
462 GLsizei width, GLsizei height,
463 GLenum format, GLenum type,
464 const struct gl_pixelstore_attrib *unpack,
465 const void *pixels,
466 struct pipe_resource *pt)
467 {
468 if ((unpack->RowLength == 0 || unpack->RowLength == width) &&
469 unpack->SkipPixels == 0 &&
470 unpack->SkipRows == 0) {
471 const GLint bpp = _mesa_bytes_per_pixel(format, type);
472 struct drawpix_cache_entry *entry =
473 find_oldest_drawpixels_cache_entry(st);
474 assert(entry);
475 entry->width = width;
476 entry->height = height;
477 entry->format = format;
478 entry->type = type;
479 entry->user_pointer = pixels;
480 free(entry->image);
481 entry->image = malloc(width * height * bpp);
482 if (entry->image) {
483 memcpy(entry->image, pixels, width * height * bpp);
484 pipe_resource_reference(&entry->texture, pt);
485 entry->age = ++st->drawpix_cache.age;
486 }
487 else {
488 /* out of memory, free/disable cached texture */
489 entry->width = 0;
490 entry->height = 0;
491 pipe_resource_reference(&entry->texture, NULL);
492 }
493 }
494 }
495
496
497 /**
498 * Make texture containing an image for glDrawPixels image.
499 * If 'pixels' is NULL, leave the texture image data undefined.
500 */
501 static struct pipe_resource *
502 make_texture(struct st_context *st,
503 GLsizei width, GLsizei height, GLenum format, GLenum type,
504 const struct gl_pixelstore_attrib *unpack,
505 const void *pixels)
506 {
507 struct gl_context *ctx = st->ctx;
508 struct pipe_context *pipe = st->pipe;
509 mesa_format mformat;
510 struct pipe_resource *pt = NULL;
511 enum pipe_format pipeFormat;
512 GLenum baseInternalFormat;
513
514 #if USE_DRAWPIXELS_CACHE
515 pt = search_drawpixels_cache(st, width, height, format, type,
516 unpack, pixels);
517 if (pt) {
518 return pt;
519 }
520 #endif
521
522 /* Choose a pixel format for the temp texture which will hold the
523 * image to draw.
524 */
525 pipeFormat = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
526 format, type, unpack->SwapBytes);
527
528 if (pipeFormat == PIPE_FORMAT_NONE) {
529 /* Use the generic approach. */
530 GLenum intFormat = internal_format(ctx, format, type);
531
532 pipeFormat = st_choose_format(st, intFormat, format, type,
533 st->internal_target, 0, 0,
534 PIPE_BIND_SAMPLER_VIEW, FALSE);
535 assert(pipeFormat != PIPE_FORMAT_NONE);
536 }
537
538 mformat = st_pipe_format_to_mesa_format(pipeFormat);
539 baseInternalFormat = _mesa_get_format_base_format(mformat);
540
541 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
542 if (!pixels)
543 return NULL;
544
545 /* alloc temporary texture */
546 pt = alloc_texture(st, width, height, pipeFormat, PIPE_BIND_SAMPLER_VIEW);
547 if (!pt) {
548 _mesa_unmap_pbo_source(ctx, unpack);
549 return NULL;
550 }
551
552 {
553 struct pipe_transfer *transfer;
554 GLubyte *dest;
555 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
556
557 /* we'll do pixel transfer in a fragment shader */
558 ctx->_ImageTransferState = 0x0;
559
560 /* map texture transfer */
561 dest = pipe_transfer_map(pipe, pt, 0, 0,
562 PIPE_TRANSFER_WRITE, 0, 0,
563 width, height, &transfer);
564 if (!dest) {
565 pipe_resource_reference(&pt, NULL);
566 _mesa_unmap_pbo_source(ctx, unpack);
567 return NULL;
568 }
569
570 /* Put image into texture transfer.
571 * Note that the image is actually going to be upside down in
572 * the texture. We deal with that with texcoords.
573 */
574 if ((format == GL_RGBA || format == GL_BGRA)
575 && type == GL_UNSIGNED_BYTE) {
576 /* Use a memcpy-based texstore to avoid software pixel swizzling.
577 * We'll do the necessary swizzling with the pipe_sampler_view to
578 * give much better performance.
579 * XXX in the future, expand this to accomodate more format and
580 * type combinations.
581 */
582 _mesa_memcpy_texture(ctx, 2,
583 mformat, /* mesa_format */
584 transfer->stride, /* dstRowStride, bytes */
585 &dest, /* destSlices */
586 width, height, 1, /* size */
587 format, type, /* src format/type */
588 pixels, /* data source */
589 unpack);
590 }
591 else {
592 bool MAYBE_UNUSED success;
593 success = _mesa_texstore(ctx, 2, /* dims */
594 baseInternalFormat, /* baseInternalFormat */
595 mformat, /* mesa_format */
596 transfer->stride, /* dstRowStride, bytes */
597 &dest, /* destSlices */
598 width, height, 1, /* size */
599 format, type, /* src format/type */
600 pixels, /* data source */
601 unpack);
602
603 assert(success);
604 }
605
606 /* unmap */
607 pipe_transfer_unmap(pipe, transfer);
608
609 /* restore */
610 ctx->_ImageTransferState = imageTransferStateSave;
611 }
612
613 _mesa_unmap_pbo_source(ctx, unpack);
614
615 #if USE_DRAWPIXELS_CACHE
616 cache_drawpixels_image(st, width, height, format, type, unpack, pixels, pt);
617 #endif
618
619 return pt;
620 }
621
622
623 static void
624 draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
625 GLsizei width, GLsizei height,
626 GLfloat zoomX, GLfloat zoomY,
627 struct pipe_sampler_view **sv,
628 int num_sampler_view,
629 void *driver_vp,
630 void *driver_fp,
631 struct st_fp_variant *fpv,
632 const GLfloat *color,
633 GLboolean invertTex,
634 GLboolean write_depth, GLboolean write_stencil)
635 {
636 struct st_context *st = st_context(ctx);
637 struct pipe_context *pipe = st->pipe;
638 struct cso_context *cso = st->cso_context;
639 const unsigned fb_width = _mesa_geometric_width(ctx->DrawBuffer);
640 const unsigned fb_height = _mesa_geometric_height(ctx->DrawBuffer);
641 GLfloat x0, y0, x1, y1;
642 GLsizei MAYBE_UNUSED maxSize;
643 boolean normalized = sv[0]->texture->target == PIPE_TEXTURE_2D;
644 unsigned cso_state_mask;
645
646 assert(sv[0]->texture->target == st->internal_target);
647
648 /* limit checks */
649 /* XXX if DrawPixels image is larger than max texture size, break
650 * it up into chunks.
651 */
652 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
653 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
654 assert(width <= maxSize);
655 assert(height <= maxSize);
656
657 cso_state_mask = (CSO_BIT_RASTERIZER |
658 CSO_BIT_VIEWPORT |
659 CSO_BIT_FRAGMENT_SAMPLERS |
660 CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
661 CSO_BIT_STREAM_OUTPUTS |
662 CSO_BIT_VERTEX_ELEMENTS |
663 CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
664 CSO_BITS_ALL_SHADERS);
665 if (write_stencil) {
666 cso_state_mask |= (CSO_BIT_DEPTH_STENCIL_ALPHA |
667 CSO_BIT_BLEND);
668 }
669 cso_save_state(cso, cso_state_mask);
670
671 /* rasterizer state: just scissor */
672 {
673 struct pipe_rasterizer_state rasterizer;
674 memset(&rasterizer, 0, sizeof(rasterizer));
675 rasterizer.clamp_fragment_color = !st->clamp_frag_color_in_shader &&
676 ctx->Color._ClampFragmentColor;
677 rasterizer.half_pixel_center = 1;
678 rasterizer.bottom_edge_rule = 1;
679 rasterizer.depth_clip_near = !ctx->Transform.DepthClampNear;
680 rasterizer.depth_clip_far = !ctx->Transform.DepthClampFar;
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_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 /* Put glDrawPixels image into a texture */
1177 pt = make_texture(st, width, height, format, type, unpack, pixels);
1178 if (!pt) {
1179 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1180 return;
1181 }
1182
1183 make_passthrough_vertex_shader(st);
1184
1185 /*
1186 * Get vertex/fragment shaders
1187 */
1188 if (write_depth || write_stencil) {
1189 driver_fp = get_drawpix_z_stencil_program(st, write_depth,
1190 write_stencil);
1191 }
1192 else {
1193 fpv = get_color_fp_variant(st);
1194
1195 driver_fp = fpv->driver_shader;
1196
1197 if (ctx->Pixel.MapColorFlag) {
1198 pipe_sampler_view_reference(&sv[1],
1199 st->pixel_xfer.pixelmap_sampler_view);
1200 num_sampler_view++;
1201 }
1202
1203 /* compiling a new fragment shader variant added new state constants
1204 * into the constant buffer, we need to update them
1205 */
1206 st_upload_constants(st, &st->fp->Base);
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 st->drawpix.vert_shader,
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 blit.src.resource->nr_storage_samples,
1479 PIPE_BIND_SAMPLER_VIEW) &&
1480 screen->is_format_supported(screen, blit.dst.format,
1481 blit.dst.resource->target,
1482 blit.dst.resource->nr_samples,
1483 blit.dst.resource->nr_storage_samples,
1484 PIPE_BIND_RENDER_TARGET)) {
1485 pipe->blit(pipe, &blit);
1486 return GL_TRUE;
1487 }
1488 }
1489 }
1490
1491 return GL_FALSE;
1492 }
1493
1494
1495 static void
1496 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1497 GLsizei width, GLsizei height,
1498 GLint dstx, GLint dsty, GLenum type)
1499 {
1500 struct st_context *st = st_context(ctx);
1501 struct pipe_context *pipe = st->pipe;
1502 struct pipe_screen *screen = pipe->screen;
1503 struct st_renderbuffer *rbRead;
1504 void *driver_fp;
1505 struct pipe_resource *pt;
1506 struct pipe_sampler_view *sv[2] = { NULL };
1507 struct st_fp_variant *fpv = NULL;
1508 int num_sampler_view = 1;
1509 enum pipe_format srcFormat;
1510 unsigned srcBind;
1511 GLboolean invertTex = GL_FALSE;
1512 GLint readX, readY, readW, readH;
1513 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1514
1515 _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
1516
1517 st_flush_bitmap_cache(st);
1518 st_invalidate_readpix_cache(st);
1519
1520 st_validate_state(st, ST_PIPELINE_META);
1521
1522 if (type == GL_DEPTH_STENCIL) {
1523 /* XXX make this more efficient */
1524 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL);
1525 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH);
1526 return;
1527 }
1528
1529 if (type == GL_STENCIL) {
1530 /* can't use texturing to do stencil */
1531 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1532 return;
1533 }
1534
1535 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1536 return;
1537
1538 /*
1539 * The subsequent code implements glCopyPixels by copying the source
1540 * pixels into a temporary texture that's then applied to a textured quad.
1541 * When we draw the textured quad, all the usual per-fragment operations
1542 * are handled.
1543 */
1544
1545 make_passthrough_vertex_shader(st);
1546
1547 /*
1548 * Get vertex/fragment shaders
1549 */
1550 if (type == GL_COLOR) {
1551 fpv = get_color_fp_variant(st);
1552
1553 rbRead = st_get_color_read_renderbuffer(ctx);
1554
1555 driver_fp = fpv->driver_shader;
1556
1557 if (ctx->Pixel.MapColorFlag) {
1558 pipe_sampler_view_reference(&sv[1],
1559 st->pixel_xfer.pixelmap_sampler_view);
1560 num_sampler_view++;
1561 }
1562
1563 /* compiling a new fragment shader variant added new state constants
1564 * into the constant buffer, we need to update them
1565 */
1566 st_upload_constants(st, &st->fp->Base);
1567 }
1568 else {
1569 assert(type == GL_DEPTH);
1570 rbRead = st_renderbuffer(ctx->ReadBuffer->
1571 Attachment[BUFFER_DEPTH].Renderbuffer);
1572
1573 driver_fp = get_drawpix_z_stencil_program(st, GL_TRUE, GL_FALSE);
1574 }
1575
1576 /* Choose the format for the temporary texture. */
1577 srcFormat = rbRead->texture->format;
1578 srcBind = PIPE_BIND_SAMPLER_VIEW |
1579 (type == GL_COLOR ? PIPE_BIND_RENDER_TARGET : PIPE_BIND_DEPTH_STENCIL);
1580
1581 if (!screen->is_format_supported(screen, srcFormat, st->internal_target, 0,
1582 0, srcBind)) {
1583 /* srcFormat is non-renderable. Find a compatible renderable format. */
1584 if (type == GL_DEPTH) {
1585 srcFormat = st_choose_format(st, GL_DEPTH_COMPONENT, GL_NONE,
1586 GL_NONE, st->internal_target, 0, 0,
1587 srcBind, FALSE);
1588 }
1589 else {
1590 assert(type == GL_COLOR);
1591
1592 if (util_format_is_float(srcFormat)) {
1593 srcFormat = st_choose_format(st, GL_RGBA32F, GL_NONE,
1594 GL_NONE, st->internal_target, 0, 0,
1595 srcBind, FALSE);
1596 }
1597 else if (util_format_is_pure_sint(srcFormat)) {
1598 srcFormat = st_choose_format(st, GL_RGBA32I, GL_NONE,
1599 GL_NONE, st->internal_target, 0, 0,
1600 srcBind, FALSE);
1601 }
1602 else if (util_format_is_pure_uint(srcFormat)) {
1603 srcFormat = st_choose_format(st, GL_RGBA32UI, GL_NONE,
1604 GL_NONE, st->internal_target, 0, 0,
1605 srcBind, FALSE);
1606 }
1607 else if (util_format_is_snorm(srcFormat)) {
1608 srcFormat = st_choose_format(st, GL_RGBA16_SNORM, GL_NONE,
1609 GL_NONE, st->internal_target, 0, 0,
1610 srcBind, FALSE);
1611 }
1612 else {
1613 srcFormat = st_choose_format(st, GL_RGBA, GL_NONE,
1614 GL_NONE, st->internal_target, 0, 0,
1615 srcBind, FALSE);
1616 }
1617 }
1618
1619 if (srcFormat == PIPE_FORMAT_NONE) {
1620 assert(0 && "cannot choose a format for src of CopyPixels");
1621 return;
1622 }
1623 }
1624
1625 /* Invert src region if needed */
1626 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1627 srcy = ctx->ReadBuffer->Height - srcy - height;
1628 invertTex = !invertTex;
1629 }
1630
1631 /* Clip the read region against the src buffer bounds.
1632 * We'll still allocate a temporary buffer/texture for the original
1633 * src region size but we'll only read the region which is on-screen.
1634 * This may mean that we draw garbage pixels into the dest region, but
1635 * that's expected.
1636 */
1637 readX = srcx;
1638 readY = srcy;
1639 readW = width;
1640 readH = height;
1641 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) {
1642 /* The source region is completely out of bounds. Do nothing.
1643 * The GL spec says "Results of copies from outside the window,
1644 * or from regions of the window that are not exposed, are
1645 * hardware dependent and undefined."
1646 */
1647 return;
1648 }
1649
1650 readW = MAX2(0, readW);
1651 readH = MAX2(0, readH);
1652
1653 /* Allocate the temporary texture. */
1654 pt = alloc_texture(st, width, height, srcFormat, srcBind);
1655 if (!pt)
1656 return;
1657
1658 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1659 if (!sv[0]) {
1660 pipe_resource_reference(&pt, NULL);
1661 return;
1662 }
1663
1664 /* Copy the src region to the temporary texture. */
1665 {
1666 struct pipe_blit_info blit;
1667
1668 memset(&blit, 0, sizeof(blit));
1669 blit.src.resource = rbRead->texture;
1670 blit.src.level = rbRead->surface->u.tex.level;
1671 blit.src.format = rbRead->texture->format;
1672 blit.src.box.x = readX;
1673 blit.src.box.y = readY;
1674 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1675 blit.src.box.width = readW;
1676 blit.src.box.height = readH;
1677 blit.src.box.depth = 1;
1678 blit.dst.resource = pt;
1679 blit.dst.level = 0;
1680 blit.dst.format = pt->format;
1681 blit.dst.box.x = pack.SkipPixels;
1682 blit.dst.box.y = pack.SkipRows;
1683 blit.dst.box.z = 0;
1684 blit.dst.box.width = readW;
1685 blit.dst.box.height = readH;
1686 blit.dst.box.depth = 1;
1687 blit.mask = util_format_get_mask(pt->format) & ~PIPE_MASK_S;
1688 blit.filter = PIPE_TEX_FILTER_NEAREST;
1689
1690 pipe->blit(pipe, &blit);
1691 }
1692
1693 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1694 * textured quad with that texture.
1695 */
1696 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1697 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1698 sv,
1699 num_sampler_view,
1700 st->drawpix.vert_shader,
1701 driver_fp, fpv,
1702 ctx->Current.Attrib[VERT_ATTRIB_COLOR0],
1703 invertTex, GL_FALSE, GL_FALSE);
1704
1705 pipe_resource_reference(&pt, NULL);
1706 pipe_sampler_view_reference(&sv[0], NULL);
1707 }
1708
1709
1710
1711 void st_init_drawpixels_functions(struct dd_function_table *functions)
1712 {
1713 functions->DrawPixels = st_DrawPixels;
1714 functions->CopyPixels = st_CopyPixels;
1715 }
1716
1717
1718 void
1719 st_destroy_drawpix(struct st_context *st)
1720 {
1721 GLuint i;
1722
1723 for (i = 0; i < ARRAY_SIZE(st->drawpix.zs_shaders); i++) {
1724 if (st->drawpix.zs_shaders[i])
1725 cso_delete_fragment_shader(st->cso_context,
1726 st->drawpix.zs_shaders[i]);
1727 }
1728
1729 if (st->drawpix.vert_shader)
1730 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shader);
1731
1732 /* Free cache data */
1733 for (i = 0; i < ARRAY_SIZE(st->drawpix_cache.entries); i++) {
1734 struct drawpix_cache_entry *entry = &st->drawpix_cache.entries[i];
1735 free(entry->image);
1736 pipe_resource_reference(&entry->texture, NULL);
1737 }
1738 }